Vladimir
08/04/2024, 9:20 PMfun NavGraphBuilder.homeScreenGraph(navController: NavController) {
composable<Home> {
HomeScreen(navController)
}
}
@Serializable
object Home
Stylianos Gakis
08/04/2024, 9:24 PMVladimir
08/04/2024, 9:26 PMVladimir
08/04/2024, 9:26 PMStylianos Gakis
08/04/2024, 10:33 PMVladimir
08/04/2024, 10:40 PMStylianos Gakis
08/04/2024, 11:07 PMonNavigateToHome: () -> Unit
so they don't need to know about Home whatsoever.
Then when you call the composable, you pass onNavigateToHome = { navController.navigate(Home) }
Stylianos Gakis
08/04/2024, 11:08 PMVladimir
08/04/2024, 11:36 PMStylianos Gakis
08/04/2024, 11:55 PMVladimir
08/05/2024, 7:39 AMVladimir
08/05/2024, 9:39 AMMyApp
composable using hiltViewModel
So my MainActivity
looks like this:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApp()
}
}
And then my MyApp
composable looks like this:
@Composable
fun MyApp(sessionViewModel: SessionViewModel = hiltViewModel()) {
//todo theme wrapper is likely to go here
val navController = rememberNavController()
MyNavHost(navController, sessionViewModel)
if(sessionViewModel.sessionStatus.value) {
navController.navigate(route = Home)
}
else {
navController.navigate(route = Login)
}
LaunchedEffect(sessionViewModel.sessionStatus.value) {
if (sessionViewModel.sessionStatus.value) {
navController.navigate(Home) {
popUpTo(Login) { inclusive = true }
}
}
}
}
Having carefully read what you wrote I'd like to kindly ask you the following:
1. Do you agree with this SessionViewModel injection?
2. Is it correct to assume that the lifecycle of the MyApp
composable will persist throughout the lifespan of the app, because it is the main wrapping composable? So even if within it I navigate to other screens it will still be sensitive to state changes, such as if I ever choose to invalidate the session?Stylianos Gakis
08/05/2024, 9:58 AMLocalViewModelStoreOwner.current
and scoping it to the ViewModelStoreOwner. You can see this in the source code of it right here https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:hilt/hil[…]t/navigation/compose/HiltViewModel.kt;l=46-47?q=hiltViewModel
So the ViewModelStoreOwner is set for each destination as a composition local when you write composable<Home> {...}
.
But when you are outside of any navigation destination, your LocalViewModelStoreOwner.current will point to your Activity. Which is itself also a ViewModelStoreOwner
as seen here https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:activity[…]roidx/activity/ComponentActivity.kt;l=115?q=ComponentActivity
So that VM will be scoped to the entire Activity, and if you are in a single activity app which you most likely should be in, then yes this will persist for the entire lifecycle of your app.
So what you are doing looks good to me!Stylianos Gakis
08/05/2024, 9:59 AMif(sessionViewModel.sessionStatus.value) {
navController.navigate(route = Home)
}
else {
navController.navigate(route = Login)
}
where you are doing a side effect in composition instead of inside a side-effect.
Right below it you do it right inside a side effect. You will want to avoid doing the side-effect just in composition like that, since this may be called even tens of times in one second if your composable just happens to be recomposing.Vladimir
08/05/2024, 10:12 AM