2
\$\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

1
\$\begingroup\$

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

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to code review! While it seems that short answers are acceptable are you able to expand this answer, perhaps explaining reasons for the suggestion? \$\endgroup\$ Commented Mar 7, 2023 at 5:36

Your Answer

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

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