Daniel Burnhan
07/19/2021, 8:36 PMclass MyAppModel: ViewModel{
val loggedInState: Flow<Boolean>
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
"SETUP APP MODEL HERE"
val myAppModel= MyAppModel()
setContent {
MyUI(myAppModel)
}
}
}
@Composable
fun MyUI(vm: MyAppModel){
val loggedIn = vm.loggedInState.asLiveData().observeAsState()
when (loggedIn.value){
is true -> DasboardUI()
is false -> AuthUI()
}
}
Now that I have a navgraph. It seems like Ill have to have to observe this state in every path in the navgraph to be able to react to it? Otherwise I could create two separate navigation hosts and have them separate for Authentication workflow and the rest of the app?Ian Lake
07/19/2021, 8:44 PMIan Lake
07/19/2021, 8:45 PMColton Idle
07/20/2021, 12:55 AMyou need to check that state in each destination that requires login (as it could be the very first destination to actually be ran).Couldn't you basically just have this
isUserLoggedIn
state on the application or activity level, and then if that state updates, then the activity just uses navigator
to open up the login screen?Daniel Burnhan
07/20/2021, 12:56 AMDaniel Burnhan
07/20/2021, 12:57 AMColton Idle
07/20/2021, 1:03 AMIan Lake
07/20/2021, 1:33 PMIan Lake
07/20/2021, 1:34 PMColton Idle
07/20/2021, 1:37 PMColton Idle
07/20/2021, 1:37 PMColton Idle
07/23/2021, 2:40 PM@Composable
fun HomeScreen(
navController: NavController,
screenTitle: String,
viewModel: HomeScreenViewModel = viewModel()
) {
if (viewModel.isLoggedIn()) {
HomePageContent(navController = navController, screenTitle = screenTitle)
} else {
// go to sign up screen
}
}