mirror of
https://github.com/bitwarden/android.git
synced 2026-04-28 11:58:40 -05:00
[PM-21573] Migrate EventsEffect to ui module (#5180)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user