Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Last active October 6, 2021 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skyyo/e7a3d6359e39251015890f4286cdf254 to your computer and use it in GitHub Desktop.
Save Skyyo/e7a3d6359e39251015890f4286cdf254 to your computer and use it in GitHub Desktop.
@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