Sivan
11/23/2022, 12:31 PMcollectAsStateWithLifecycle
method while consuming state? This was introduced as a part of androidx.lifecycle:lifecycle-runtime-compose:2.6.0-alpha01
. collectAsStateWithLifecycle
uses repeatOnLifecycle
under the hood.
Reference : https://medium.com/androiddevelopers/consuming-flows-safely-in-jetpack-compose-cde014d0d5a3Oleksii Malovanyi
11/23/2022, 12:49 PM/**
* Collect [ContainerHost.container]'s state via [collectAsState] but bounded with the [flowWithLifecycle]
*/
@Composable
fun <STATE : Any, SIDE_EFFECT : Any> ContainerHost<STATE, SIDE_EFFECT>.composableState(
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)
}
Mikolaj Leszczynski
11/23/2022, 1:00 PMMikolaj Leszczynski
11/23/2022, 1:01 PMVishnu Ravi
11/23/2022, 6:37 PMcollectAsState()
https://github.com/orbit-mvi/orbit-mvi/blob/df25e1a2be166e3b1578c28f66322eb04a5624[…]in/kotlin/org/orbitmvi/orbit/compose/ContainerHostExtensions.kt
Please correct me if I'm wrong 🙏Oleksii Malovanyi
11/23/2022, 8:09 PMSivan
11/25/2022, 7:42 PM