Lilly
03/08/2023, 11:52 PMcollectAsState
, e.g. when a button is pressed. In a MVC architecture I'm currently doing this:
class Presenter {
val uiState: UiState by lazy { UiState() }
fun startCollection(uiScope: CoroutineScope) {
useCase.myFlow().onEach { counter -> uiState.counter = counter }.launchIn(scope)
}
}
class UiState {
var counter by mutableStateOf(0)
}
@Composable
fun MyScreen(presenter: Presenter) {
val scope = rememberCoroutineScope()
when(action) {
someEvent -> presenter.startCollection(scope)
}
}
Just curious if there are other more concise options?s3rius
03/11/2023, 8:54 PMvar collectionEnabled = false
fun startCollection() { collectionEnabled = true }
init {
useCase.myFlow().filter(collectionEnabled).onEach { ... }.launchIn(..)
}
This comes to mind. Ofc it keeps the flow active from the very start, which may or may not be bad.Lilly
03/11/2023, 10:52 PMmutableStateOf