Rafiul Islam
08/25/2021, 12:53 PMRafiul Islam
08/25/2021, 12:53 PMif (viewModel.isLoggedIn) {
navController.navigate(Screens.Home.id){
popUpTo(Screens.Authenticate.id){ inclusive = true }
}
}
here isLoggedIn is a mutable state.
var isLoggedIn by mutableStateOf(false)
private set
Tin Tran
08/25/2021, 1:02 PMTin Tran
08/25/2021, 1:02 PMTin Tran
08/25/2021, 1:03 PMLaunchedEffect(viewModel.isLoggedIn) {
if (viewModel.isLoggedIn) {
navController.navigate(Screens.Home.id){
popUpTo(Screens.Authenticate.id)
}
}
}
Rafiul Islam
08/25/2021, 1:15 PMif (viewModel.isNetworkError) {
LaunchedEffect(key1 = viewModel.isNetworkError) {
state.snackbarHostState.showSnackbar("Something went wrong!")
}
}
or this
if (viewModel.isNetworkError) {
LaunchedEffect(key1 = state.snackbarHostState) {
state.snackbarHostState.showSnackbar("Something went wrong!")
}
}
Tin Tran
08/25/2021, 1:17 PMLaunchedEffect(key1 = viewModel.isNetworkError) {
if (viewModel.isNetworkError) {
state.snackbarHostState.showSnackbar("Something went wrong!")
}
}
Tin Tran
08/25/2021, 1:20 PMRafiul Islam
08/25/2021, 6:30 PMif (posts.hasError) {
val errorMessage = stringResource(id = R.string.load_error)
val retryMessage = stringResource(id = R.string.retry)
// If onRefreshPosts or onErrorDismiss change while the LaunchedEffect is running,
// don't restart the effect and use the latest lambda values.
val onRefreshPostsState by rememberUpdatedState(onRefreshPosts)
val onErrorDismissState by rememberUpdatedState(onErrorDismiss)
// Show snackbar using a coroutine, when the coroutine is cancelled the snackbar will
// automatically dismiss. This coroutine will cancel whenever posts.hasError is false
// (thanks to the surrounding if statement) or if scaffoldState.snackbarHostState changes.
LaunchedEffect(scaffoldState.snackbarHostState) {
val snackbarResult = scaffoldState.snackbarHostState.showSnackbar(
message = errorMessage,
actionLabel = retryMessage
)
when (snackbarResult) {
SnackbarResult.ActionPerformed -> onRefreshPostsState()
SnackbarResult.Dismissed -> onErrorDismissState()
}
}
}
Tin Tran
08/26/2021, 7:44 AM