[PM-21573] Migrate EventsEffect to ui module (#5180)

This commit is contained in:
Patrick Honkonen
2025-05-13 10:13:57 -04:00
committed by GitHub
parent 11b767c98f
commit da62244000
72 changed files with 88 additions and 99 deletions

View File

@@ -0,0 +1,35 @@
package com.bitwarden.ui.platform.base.util
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import com.bitwarden.ui.platform.base.BackgroundEvent
import com.bitwarden.ui.platform.base.BaseViewModel
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
/**
* Convenience method for observing event flow from [BaseViewModel].
*
* By default, events will only be consumed when the associated screen is
* resumed, to avoid bugs like duplicate navigation calls. To override
* this behavior, a given event type can implement [BackgroundEvent].
*/
@Composable
fun <E> EventsEffect(
viewModel: BaseViewModel<*, E, *>,
lifecycleOwner: Lifecycle = LocalLifecycleOwner.current.lifecycle,
handler: (E) -> Unit,
) {
LaunchedEffect(key1 = Unit) {
viewModel.eventFlow
.filter {
it is BackgroundEvent ||
lifecycleOwner.currentState.isAtLeast(Lifecycle.State.RESUMED)
}
.onEach { handler.invoke(it) }
.launchIn(this)
}
}