0
\$\begingroup\$

The project is simple: Login screen and a forgot password screen. You can navigate from Login to ForgotPassword.

I feel like I overcomplicated things, but I don't know if (and how) it can be simplified.

The code: LoginRoute.kt

@Composable
fun LoginRoute(
    viewModel: LoginViewModel = hiltViewModel(),
    navController: NavHostController,
) {
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()

    LoginScreen(
        uiState = uiState,
        onLoginClicked = {
            viewModel.acceptIntent(LoginIntent.LogIn)
        },
        onForgotPasswordClicked = {
            navController.navigate(NavigationDestination.ForgotPassword.route)
        }
    )
}

LoginNavigationFactory:

class LoginNavigationFactory @Inject constructor() : NavigationFactory {

    override fun create(builder: NavGraphBuilder, navController: NavHostController) {
        builder.composable(NavigationDestination.Login.route) {
            LoginRoute(navController = navController)
        }
        builder.composable(NavigationDestination.ForgotPassword.route) {
            ForgotPasswordRoute()
        }
    }
}

Do I really need to pass navController as an argument to the Routes? If I don't do it and simply do rememberNavController in the composable, it gives me NullPointers.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I pass in a delegate and handle navigation at the top. I find this better practice

\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.