Tgo1014
10/12/2021, 7:40 AMSharedFlow
?Csaba Kozák
10/12/2021, 7:41 AMLaunchedEffect
is perfect for this. See this example.Tgo1014
10/12/2021, 7:44 AMCsaba Kozák
10/12/2021, 8:01 AMSnackbarHost()
outside of a Scaffold
as well.Damian Zawadzki
10/12/2021, 8:02 AMLaunchedEffect(key1 = "PackagesStartFormEffects") {
viewModel.effect.collect { effect ->
val message = when (effect) {
PackagesStartFormContract.Effect.FormConfirmed -> {
"Form confirmed!"
}
is PackagesStartFormContract.Effect.UnknownError -> {
effect.message ?: "Unknown error."
}
}
snackbarHostState.showSnackbar(
message = message,
actionLabel = label,
duration = SnackbarDuration.Long
)
}
}
but it feels like ""PackagesStartFormEffects" is not elegant solution to fulfill the key1 parameter requirement. I think that passing null would do same thing.val channel = remember { Channel<Int>(Channel.Factory.CONFLATED) }
LaunchedEffect(channel) {
channel.receiveAsFlow().collect { index ->
val result = snackbarHostState.showSnackbar(
message = "Snackbar # $index",
actionLabel = "Action on $index"
)
here is channel as parameter.Tgo1014
10/12/2021, 8:10 AMcollectAsState()
the example just collect{}
and do what’s needed