Last active
August 10, 2022 16:20
-
-
Save krzdabrowski/d5c40d162871ed8d01f8dac97b2da007 to your computer and use it in GitHub Desktop.
This file contains 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
@Composable | |
inline fun <reified T> Flow<T>.collectWithLifecycle( | |
key: Any = Unit, | |
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, | |
minActiveState: Lifecycle.State = Lifecycle.State.STARTED, | |
noinline action: suspend (T) -> Unit | |
) { | |
val lifecycleAwareFlow = remember(this, lifecycleOwner) { | |
flowWithLifecycle( | |
lifecycle = lifecycleOwner.lifecycle, | |
minActiveState = minActiveState | |
) | |
} | |
LaunchedEffect(key) { | |
lifecycleAwareFlow.collect { action(it) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment