On my homepage, I am using the following code. Here
viewmodel.user
is a MutableState.
val loginState = savedStateHandle.getLiveData<Boolean>("LOGIN_SUCCESS").observeAsState()
if (viewModel.user.value == null) {
LaunchedEffect(viewModel.user.value) {
navController.navigate(Screens.Login.id)
}
}
when (loginState.value) {
false -> {
onFinish()
}
true -> {
MainContent()
}
}
And In Login Screen
AuthUiState.LoggedIn -> {
savedStateHandle?.set("LOGIN_SUCCESS", true)
navController.popBackStack()
}
But the problem is when I use popBackStack() and go back to my homepage the following block runs again and go back to the login page again because of
viewModel.user.value
is still null. Since I am using two different viewModels
LoginViewModel
and
HomeViewModel
so I can't update
viewModel.user
value directly from the
LoginViewModel
right after the successful login instead, I update it from the
MainContent()
in
HomeViewModel
.
if (viewModel.user.value == null) {
LaunchedEffect(viewModel.user.value) {
navController.navigate(Screens.Login.id)
}
}
What is the appropriate way of updating user value when using two different ViewModel? So that the navigation block doesn't execute again when I pop back to my homepage.