Florian
08/05/2021, 6:05 PMmutableStateOf holder.Colton Idle
08/05/2021, 6:22 PMFlorian
08/05/2021, 6:25 PMColton Idle
08/05/2021, 6:28 PMAlexey Glushkov
08/05/2021, 6:34 PMFlorian
08/05/2021, 6:41 PMFlorian
08/05/2021, 7:05 PMFlorian
08/05/2021, 7:05 PMColton Idle
08/05/2021, 7:23 PMFlorian
08/05/2021, 7:32 PMColton Idle
08/05/2021, 7:37 PMFlorian
08/05/2021, 7:43 PMFlorian
08/05/2021, 7:43 PMmuthuraj
08/05/2021, 8:07 PMFlorian
08/05/2021, 8:08 PMFlorian
08/05/2021, 8:08 PMFlorian
08/05/2021, 8:08 PMmuthuraj
08/05/2021, 8:12 PMChannel into mutableStateOf. Like from the article, I expose the Channel as Flow to the composable. And in the composable, I use LaunchedEffect(Unit) to collect the flow and respond to that. This way I can consume all events emitted to the channel since the flow actually suspends until my task from onEach/collect is completed before emitting next item.
Roughly like this,
fun MyComposable(effectFlow: Flow<Effects>){
LaunchedEffect(Unit){
effectFlow.onEach{
//do thing with it
}.launchIn(this)
}
}Colton Idle
08/05/2021, 8:19 PMmuthuraj
08/05/2021, 8:21 PMFlorian
08/05/2021, 8:51 PMFlorian
08/05/2021, 8:51 PMFlorian
08/05/2021, 8:52 PMvar snackbarMessage by remember { mutableStateOf<String?>(null) }
LaunchedEffect(true) {
viewModel.events.collect { event ->
when (event) {
is TodoListViewModel.Event.ShowTaskSavedConfirmationMessage ->
snackbarMessage = event.msg
}
}Florian
08/05/2021, 8:53 PMmuthuraj
08/05/2021, 8:59 PMscaffoldState.snackbarHostState.showSnackbar.muthuraj
08/05/2021, 9:00 PMshowSnackbar is a suspend function.Florian
08/05/2021, 9:03 PMFlorian
08/05/2021, 9:03 PMmuthuraj
08/05/2021, 9:03 PMFlorian
08/05/2021, 9:04 PMFlorian
08/05/2021, 9:04 PMFlorian
08/05/2021, 9:04 PMFlorian
08/05/2021, 9:06 PMscaffoldState in the outer function and pass it down to the Composable that actually has the Scaffold? Is that correct?muthuraj
08/05/2021, 9:08 PMScafflold in the same function that I handle these events. But passing scaffoldState or even just snackbarHostState should work I think.Florian
08/05/2021, 9:10 PMFlorian
08/05/2021, 9:10 PMFlorian
08/05/2021, 9:15 PMFlorian
08/05/2021, 9:15 PMFlorian
08/05/2021, 9:15 PMFlorian
08/05/2021, 9:26 PMFlorian
08/05/2021, 9:27 PMFlorian
08/05/2021, 9:27 PMFlorian
08/05/2021, 9:27 PMmuthuraj
08/05/2021, 9:30 PMcollectLatest instead of collect.muthuraj
08/05/2021, 9:31 PM*Latest flow operators would cancel previous task when new event is received.Florian
08/05/2021, 9:31 PMFlorian
08/05/2021, 9:31 PMFlorian
08/05/2021, 9:31 PMColton Idle
08/06/2021, 12:02 AMsindrenm
08/06/2021, 8:02 AMsealed class Effect {
// different effects ...
}
class ViewModel {
private val _effect = Channel<Effect>()
val effect: Flow<Effect> = _effect.receiveAsFlow()
}
@Composable
fun Screen(viewModel: ViewModel) {
LaunchedEffect(Unit) {
viewModel.effect.collect { effect ->
when (effect) {
// handle cases ...
}
}
}
}Florian
08/06/2021, 9:17 AM