Skip to content

Instantly share code, notes, and snippets.

@KwabenBerko
Created August 11, 2021 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KwabenBerko/752d334557ec702bc617c4ae99f7bb20 to your computer and use it in GitHub Desktop.
Save KwabenBerko/752d334557ec702bc617c4ae99f7bb20 to your computer and use it in GitHub Desktop.
Snackbars In Compose
@Composable
fun ConverterScreen(
navController: NavController,
viewModel: ConverterViewModel
) {
val scaffoldState = rememberScaffoldState()
val viewState by viewModel.viewState.collectAsState()
val viewEffects by viewModel.viewEffects.collectAsState(initial = null)
Scaffold(
scaffoldState = scaffoldState,
content = {
LaunchedEffect(true) {
viewModel.onEvent(ConverterViewEvents.OnReady)
when (viewEffects) {
is ConverterViewEffects.ConvertCurrenciesFailed -> {
Timber.e("CONVERT CURRENCIES FAILED")
val error = (viewEffects as ConverterViewEffects.ConvertCurrenciesFailed).error
scaffoldState.snackbarHostState.showSnackbar(
message = when(error){
ConvertCurrenciesError.NetworkError -> "Please check your network connection and try again."
ConvertCurrenciesError.UnknownError -> "An Unexpected Error Occurred."
},
duration = SnackbarDuration.Short
)
}
}
}
when (viewState) {
is ConverterViewState.Initial -> Unit
is ConverterViewState.Loading -> {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
CircularProgressIndicator()
}
}
is ConverterViewState.Content -> {
val contentState = viewState as ConverterViewState.Content
}
is ConverterViewState.Error -> Unit
}
},
snackbarHost = { hostState ->
SnackbarHost(hostState = hostState) { data ->
Snackbar(snackbarData = data)
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment