In android jetpack compose, I have a navigation ho...
# compose
p
In android jetpack compose, I have a navigation host in my main App.kt file, which opens various screens. One of these screens is the login screen. It has a function on his viewmodel that does login with a server. When the login is successfull, sets a uistate loggedIn variable to true in that screeen. How can I pass to the main App.kt container that the login has been successfull, so it can enable navigation to screens that requires to be logged in? is it correct and a good practice to do it simply calling a hoisted function like this on the start of the login screen?
Copy code
@Composable fun LoginScreen(
    modifier: Modifier = Modifier,
    vm: LoginScreenViewModel = koinViewModel(),
    onLoggedIn: () -> Unit
) {
    val uiState by vm.uiState.collectAsStateWithLifecycle()
    if (uiState.loggedIn)
        onLoggedIn()
    [...]
    Button(onClick = { vm.logIn() } [...])
That
onLoggedIn
function is hoisted in the App screen that has the navigation and sets his loggedIn variable to true if that function gets called
a
Yes, that is fine. State hoisting will do if it's a simple app. Ideally you could maintain auth in the data layer of your app and read it in every screen.
☝️ 1
s
You also probably want to wrap your onLoggedIn call in a LaunchedEffect so it does not trigger more than once if there are unrelated recompositions