Oleksii Malovanyi
12/22/2021, 4:06 PM@Composable
fun <STATE : Any, SIDE_EFFECT : Any> ContainerHost<STATE, SIDE_EFFECT>.collectAsState(
minActiveState: Lifecycle.State = Lifecycle.State.STARTED
): State<STATE> {
val lifecycleOwner = LocalLifecycleOwner.current
val lifecycleAwareStateFlow = remember(this, lifecycleOwner) {
container.stateFlow.flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState)
}
return lifecycleAwareStateFlow.collectAsState(currentState)
}
kioba
12/23/2021, 9:18 AMflowWithLifecycle
works only in case of cold Flow
, where the repeat on lifecycle can cancel the `Flow`'s event generator.
--- Blogpost example --
Looking at the example in the post they accept a cold flow stream that provides Location data.
@Composable
fun LocationScreen(locationFlow: Flow<Flow>)
...
Unsubscribing from locationFlow
while compose is in the background might reduce the pressure on location services and we can resubscribe when the app comes to the foreground.
--- ContainerHost ---
ContainerHost contains a StateFlow
which is hot and accepts state regardless the number of subscribers connected to the `StateFlow`'s instance. Adding a repeat won't cancel the generating source as it is providing states to the stateFlow
.Oleksii Malovanyi
12/23/2021, 12:04 PMkioba
12/23/2021, 1:26 PMOleksii Malovanyi
12/23/2021, 1:43 PMA Safe Observer of Events