Lilly
07/15/2024, 6:55 PM@Composable
fun MyScreen() {
val lifecycleOwner = LocalLifecycleOwner.current
DisposableEffect(Unit) {
myPresenter.collectFlows(lifecycleOwner)
...
}
}
presenter:
class MyPresenter {
...
fun collectFlows(lifecycleOwner: LifecycleOwner) {
lifecycleOwner.lifecycleScope.launch {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
val lifecycleScope = this
/* Filter state */
myFilterObservable()
.onEach { filter ->
myStateMutableFlow.update { prevState ->
prevState.copy(filterState = filter)
}
}
.launchIn(lifecycleScope)
... more flows to collect
}
}
}
}
The reason why I do not use Flow.combine: some flows do not emit an initial value + I have state that is non-flow which I would have to convert to flow to use it with combine. Have a looh please and tell me if this can be done better. Maybe another approach would suit better for this use case. Thanks in advance