How using jetpack compose navigation to conditionally navigate with the deeplink?

165 views Asked by At

I have an application that uses only jetpack compose navigation. One Single Activity that hosts jetpack compose NavHost. I need that if user is not authorized the deeplink navigation stops on the authorization otherwise continue till the details in the bottom menu graph.

The problem is that despite of authorized or not authorized, and whatever i tried - the deeplink navigation ignores the authentication and always navigates through till the details screen of the bottom menu graph.

    @Composable
    fun ExampleNavHost(
        startedFromDeeplink: Boolean = rememberStartedFromDeeplink(),
        navController: NavHostController = rememberNavController(),
        viewModel: MainViewModel = viewModel()
    ) {
        val authenticated by viewModel.authenticated.collectAsState()

        val startDestination = if (authenticated) BOTTOM_MENU_GRAPH else AUTH_GRAPH

        NavHost(
            navController = navController,
            startDestination = if (startedFromDeeplink) startDestination else StartDestination.route,
        ) {

            start(
                onStartNavigate = {
                    navController.popBackStack()
                    navController.navigate(startDestination)
                }
            )

            authentication(
                onLoginClicked = navController::navigateToBottomMenu,
                onNavigateToResetPasswordClicked = navController::navigateToResetPassword,
                onNavigateToRegisterClicked = navController::navigateToRegister,
                onRegisterClicked = navController::popBackStack,
                onResetPasswordClicked = navController::popBackStack,
                onNavigateBack = navController::popBackStack
            )

            bottomMenu(
                topLevelDestinations = immutableListOf(
                    HomeTopLevelDestination, SettingsTopLevelDestination, ProfileTopLevelDestination
                ),
                onNavigateToDetails = navController::navigateToDetails,
                onNavigateUp = navController::popBackStack,
                onLogout = navController::logout
            )
        }
    }
    @SuppressLint("ComposableNaming")
    @Composable
    private fun rememberStartedFromDeeplink(): Boolean {
        val context = LocalContext.current
        return remember { checkIfStartedFromDeeplink(context) }
    }

    private fun checkIfStartedFromDeeplink(context: Context): Boolean {
        // Get the intent that started this activity
        val intent = (context as Activity).intent
        return intent != null && intent.action == Intent.ACTION_VIEW && intent.data != null
    }

0

There are 0 answers