Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@Composable
fun LifecycleAwareCurrencyPriceCard(
currencyPrice: CurrencyPrice,
currencyPriceUpdateFlow: Flow<Int>,
onCurrencyUpdated: (progress: Int) -> Unit,
onDisposed: () -> Unit,
) {
val lifecycleOwner = LocalLifecycleOwner.current
val lifecycleAwareCurrencyPriceFlow = remember(currencyPriceUpdateFlow, lifecycleOwner) {
currencyPriceUpdateFlow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
}
LaunchedEffect(Unit) {
lifecycleAwareCurrencyPriceFlow.collect { progress -> onCurrencyUpdated(progress) }
}
DisposableEffect(Unit) { onDispose { onDisposed() } }
CurrencyCard(currencyPrice.name, "${currencyPrice.price}", currencyPrice.priceFluctuation)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment