This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AppTheme { | |
var systemBarStyle by remember { | |
val defaultSystemBarColor = android.graphics.Color.TRANSPARENT | |
mutableStateOf( | |
SystemBarStyle.auto( | |
lightScrim = defaultSystemBarColor, | |
darkScrim = defaultSystemBarColor | |
) | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// don't do this :( | |
<item name="android:statusBarColor">....</item> | |
<item name="android:navigationBarColor">.....</item> | |
<item name="android:windowLightStatusBar">....</item> | |
<item name="android:windowLightNavigationBar">.....</item> | |
//.... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style name="AppTheme" parent="Theme.Material3.Light.NoActionBar"> | |
<item name="colorPrimary">@color/md_theme_light_primary</item> | |
// .... | |
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class SingleLiveEvent<out T>( | |
private val content: T, | |
private val id: String = UUID.randomUUID().toString() | |
) { | |
// same as before | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class SingleLiveEvent<out T>(private val content: T) { | |
var hasBeenHandled = false | |
private set // Allow external read but not write | |
/** | |
* Returns the content and prevents its use again. | |
*/ | |
fun getContentIfNotHandled(): T? { | |
return if (hasBeenHandled) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// viewModel layer | |
class MyViewModel: ViewModel() { | |
private val _eventWithChannel = Channel<Event>() | |
val eventFlowFromChannel = _eventWithChannel.receiveAsFlow() | |
} | |
sealed class Event { | |
object NavigateToAnotherScreen: Event() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// viewModel layer | |
class MyViewModel: ViewModel() { | |
private val _eventWithChannel = Channel<Event>() | |
val eventFlowFromChannel = _eventWithChannel.receiveAsFlow() | |
} | |
// compose layer | |
LaunchedEffect(lifecycleOwner) { | |
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | |
withContext(Dispatchers.Main.immediate) { | |
viewModel.eventFlowFromChannel.collect { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel: ViewModel() { | |
private val _stateflowSingleLiveEvent = MutableStateFlow<SingleLiveEvent<Event>?>(null) | |
val stateflowSingleLiveEvent: StateFlow<SingleLiveEvent<Event>?> = _stateflowSingleLiveEvent | |
} | |
// compose layer | |
LaunchedEffect(lifecycleOwner) { | |
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | |
viewModel.stateflowSingleLiveEvent.collect { singleLiveEvent -> | |
singleLiveEvent?.getContentIfNotHandled()?.let { event -> | |
when (event) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SingleLiveEvent<out T>(private val content: T) { | |
// same as before.. | |
} |