https://kotlinlang.org logo
#compose
Title
# compose
c

colintheshots

12/15/2021, 8:51 PM
Let’s say the SnackbarHost isn’t sufficient for some UI element I want to show on messages and errors. Is there a reliable way to add a Composable briefly on the screen the same way as a snackbar would appear, but sometimes called from the Fragment or ViewModel, outside of Compose? I’m trying to add our custom toasts across the app in a variety of status-specific colors with icons included.
🤔 1
Initially, I was using a bottomSheet route with Jetpack Compose Navigation and Accompanist. But this seems problematic when I need to call it from outside of Compose. Also, I worry what happens if I programmatically call navController.popBackStack after a few seconds and the user has already hit back.
I think I might be able to work around the former by adding Jetpack Compose Navigation on every screen with bottom sheet targets. Then add a Flow<String> on the viewModel that we can collect inside the top-level composable to navigate the navController. The latter I could maybe check the currentDestination and backStackEntry and verify they haven’t changed before popping. But this is a pretty messy solution to be sure.
a

Adam Powell

12/16/2021, 4:11 PM
You could reuse
SnackbarHostState
and write your own composable to present the current message https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHostState
If the data structure used for a message isn't sufficient you can have a look at how SnackbarHostState is implemented and write your own similar API
c

colintheshots

12/16/2021, 5:03 PM
I think the main issue is that I need to present an icon with the text, so I’d probably need to write a custom SnackbarHostState and a custom Scaffold to host it.
a

Adam Powell

12/16/2021, 5:43 PM
I don't think you should need a custom Scaffold to host it, just place your own host in the right slot and ignore the snackbarhoststate in the scaffoldstate
🙏 1
2 Views