I'm trying to use the snackbar on Android Compose. However, as I'm trying to display 2 messages in a row on the snackbar, I can only see the first message, does anybody know what can cause this?
I'm putting the code involved below:
Component code related to Snackbar:
/*
The state is hoisted. It contains the snackbar message that is not null if
there is something to display
*/
state.snackbarMessage?.let { resourceId ->
val message = stringResource(resourceId)
viewModel.handleEvent(
AuthenticationEvent.SnackbarMessage(
message = message,
type = when {
state.isFailure -> SnackbarType.ERROR
else -> SnackbarType.VALIDATION
}
)
)
}
/* Custom Component using Scaffold to host snackbar events */
Frame(
modifier = modifier,
snackbarState = viewModel.snackbarState,
onShowSnackbar = { viewModel.onSnackbarDisplayed() }
) { innerPadding ->
// UI Content
}
Frame Component:
@Composable
fun Frame(
modifier: Modifier = Modifier,
header: @Composable () -> Unit = {},
snackbarState: StateFlow<MajorSnackbarData?>? = null,
onShowSnackbar: (() -> Unit)? = null,
content: @Composable (PaddingValues) -> Unit
) {
val coroutineScope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState()
val showSnackbar: (MajorSnackbarData) -> Unit = {
coroutineScope.launch {
onShowSnackbar?.invoke()
scaffoldState.snackbarHostState.showSnackbar(
message = it.message,
actionLabel = it.type.name,
)
}
}
snackbarState?.collectAsState()?.value.also { data ->
if (data != null) {
showSnackbar(data)
}
}
Scaffold(
modifier = modifier,
topBar = header,
scaffoldState = scaffoldState,
snackbarHost = {
SnackbarHost(it) { data ->
MajorSnackbar(data)
}
},
content = content,
)
}
ViewModel base that hosts Snackbar:
open class BaseViewModel : ViewModel() {
private val _snackbarState = MutableStateFlow<MajorSnackbarData?>(null)
val snackbarState: StateFlow<MajorSnackbarData?> = _snackbarState
protected fun showSnackBar(
message: String,
type: SnackbarType = SnackbarType.ERROR
) {
_snackbarState.value = MajorSnackbarData(message, type)
}
open fun onSnackbarDisplayed() {
_snackbarState.value = null
}
}
Event handling in a viewmodel:
fun handleEvent(event: Event) {
when (event) {
// ...
is Event.SnackbarMessage -> displayError(event)
}
}
private fun displayError(event: Event.SnackbarMessage) {
showSnackBar(event.message, event.type)
}
state.snackbarMessage
? What updates its value? ViewModel?