@Composable
fun RootNav() {
val navController = rememberNavController()
val authViewModel: AuthViewModel = hiltViewModel()
val authState by authViewModel.authState.collectAsState()
val startDestination = when (authState) {
is AuthState.Unauthenticated -> ScreenRoutes.AuthNav.route
is AuthState.AuthenticatedWithoutCouple -> ScreenRoutes.RegisterNav.route
is AuthState.AuthenticatedWithCouple -> ScreenRoutes.HomeNav.route
else -> ScreenRoutes.AuthNav.route
}
NavHost(
navController = navController,
startDestination = startDestination
) {
AuthNav(authViewModel, navController)
RegisterNav(navController)
composable(route = ScreenRoutes.HomeNav.route) {
HomeNavGraph(navController){
authViewModel.signOut()
}
}
}
}
I have a structure like the one below, it works correctly but the user always sees the Login screen, how can I fix this?
You collected authState
from AuthViewModel
so I guest that your authState
is initially set to AuthState.Unauthenticated
in your AuthViewModel
, something like:
val authState: MutableStateFlow<AuthState>(AuthState.Unauthenticated)
so your startDestination
is always AuthState.Unauthenticated
first before it's updated to any other value