Skip to content

Instantly share code, notes, and snippets.

View CostaFot's full-sized avatar
🍦

Costa Fotiadis CostaFot

🍦
View GitHub Profile
AppTheme {
var systemBarStyle by remember {
val defaultSystemBarColor = android.graphics.Color.TRANSPARENT
mutableStateOf(
SystemBarStyle.auto(
lightScrim = defaultSystemBarColor,
darkScrim = defaultSystemBarColor
)
)
}
@CostaFot
CostaFot / nono.xml
Last active December 5, 2023 01:17
// don't do this :(
<item name="android:statusBarColor">....</item>
<item name="android:navigationBarColor">.....</item>
<item name="android:windowLightStatusBar">....</item>
<item name="android:windowLightNavigationBar">.....</item>
//....
<style name="AppTheme" parent="Theme.Material3.Light.NoActionBar">
<item name="colorPrimary">@color/md_theme_light_primary</item>
// ....
</style>
data class SingleLiveEvent<out T>(
private val content: T,
private val id: String = UUID.randomUUID().toString()
) {
// same as before
}
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) {
// viewModel layer
class MyViewModel: ViewModel() {
private val _eventWithChannel = Channel<Event>()
val eventFlowFromChannel = _eventWithChannel.receiveAsFlow()
}
sealed class Event {
object NavigateToAnotherScreen: Event()
}
// 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 {
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) {
class SingleLiveEvent<out T>(private val content: T) {
// same as before..
}
// viewModel layer
class MyViewModel: ViewModel() {
private val _eventWithSharedFlow = MutableSharedFlow<Event>()
val eventWithSharedFlow: SharedFlow<Event> = _eventWithSharedFlow
}
// compose layer
LaunchedEffect(lifecycleOwner) {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.eventWithSharedFlow.collect {