I am using compose in Android. For UI states I use stateFlow in viewmodel & for handling bottom sheet show hide state, I use sharedFlow.
The issue is for sharedFlow collect code in UI, it has to be put inside LaunchedEffect which gets executed at the end.
So cases where from init of viewmodel if I emit a value to sharedFlow like showing bottom sheet those value gets missed. I don't want to use repeat 1 as there are many other handlings which I'll have to do.
Is there any easier way to do it?
LaunchedEffect(key1 = true) {
viewModel.showBottomSheet.collect {
when (it) {
true -> scope.launch {
bottomSheetState.show()
}
false -> scope.launch {
bottomSheetState.hide()
}
}
}
}
In viewmodel I have a code something similar to this
private val _showBottomSheet = MutableSharedFlow<Boolean>()
val showBottomSheet = _showBottomSheet.asSharedFlow()
One ugly hack is add some delay before updating the value for such cases. Can someone suggest a better way