Created
August 11, 2021 18:19
-
-
Save KwabenBerko/752d334557ec702bc617c4ae99f7bb20 to your computer and use it in GitHub Desktop.
Snackbars In Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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