Skip to content

Commit e89ae52

Browse files
committed
AuthGraph.kt: Separate auth-gateway destinations to dedicated nested NavGraph
* This fixes the issue where navigating to the nested AuthDestination.Login route causes a runtime crash as navigating to nested NavGraph routes is not possible (this is apparently "Intended Behaviour" - see https://issuetracker.google.com/issues/357896945) * Add separate `AuthGatewayGraphRoot` destination * Add `navigateToAuthGatewayGraph` DSL * Remove `isLoggedIn` checks from `authGraph` NavGraph
1 parent cd33615 commit e89ae52

5 files changed

Lines changed: 38 additions & 22 deletions

File tree

app/src/main/java/com/edricchan/studybuddy/navigation/compat/CompatNavigation.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ fun NavGraphBuilder.aboutGraph(
4747
*/
4848
// TODO: Migrate destinations to Jetpack Compose
4949
fun NavGraphBuilder.compatGraphs(
50-
context: Context,
51-
isLoggedIn: Boolean
50+
context: Context
5251
) {
5352
activity<CompatDestination.Debug> {
5453
activityClass = DebugActivity::class
@@ -68,7 +67,7 @@ fun NavGraphBuilder.compatGraphs(
6867

6968
aboutGraph(context = context)
7069
context(context) {
71-
authGraph(isLoggedIn = isLoggedIn)
70+
authGraph()
7271
taskGraph()
7372
}
7473

app/src/main/java/com/edricchan/studybuddy/ui/modules/main/MainActivity.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,10 @@ class MainActivity : BaseActivity(), NavController.OnDestinationChangedListener
315315
val isLoggedIn = auth.currentUser != null
316316
graph = createGraph(
317317
if (isLoggedIn) TaskDestination.TaskGraphRoot
318-
else AuthDestination.Login
318+
else AuthDestination.AuthGatewayGraphRoot
319319
) {
320320
compatGraphs(
321-
context = this@MainActivity,
322-
isLoggedIn = isLoggedIn
321+
context = this@MainActivity
323322
)
324323
}
325324

features/auth/navigation/src/main/kotlin/com/edricchan/studybuddy/features/auth/navigation/AuthDestination.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ sealed interface AuthDestination {
1313
@Serializable
1414
data object AuthGraphRoot : AuthDestination
1515

16+
/** Root destination for all gateway-related destinations in the authentication graph. */
17+
@Serializable
18+
data object AuthGatewayGraphRoot : AuthDestination
19+
1620
/** Destination to view the currently signed-in user's information. */
1721
@Serializable
1822
data class AccountInfo(val action: AccountAction? = null) : AuthDestination {

features/auth/navigation/src/main/kotlin/com/edricchan/studybuddy/features/auth/navigation/AuthNavigation.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ fun NavController.navigateToAuthGraph(
1717
builder: NavOptionsBuilder.() -> Unit = DefaultNavOptionsBuilder
1818
) = navigate(AuthDestination.AuthGraphRoot, builder)
1919

20+
/**
21+
* Navigates to the [AuthDestination.AuthGatewayGraphRoot] route.
22+
*
23+
* By default, [NavOptionsBuilder.launchSingleTop] is set to `true`. If this
24+
* behaviour is undesired, [builder] should be specified.
25+
*/
26+
fun NavController.navigateToAuthGatewayGraph(
27+
builder: NavOptionsBuilder.() -> Unit = DefaultNavOptionsBuilder
28+
) = navigate(AuthDestination.AuthGatewayGraphRoot, builder)
29+
2030
/**
2131
* Navigates to the [AuthDestination.AccountInfo] route.
2232
*

features/auth/src/main/kotlin/com/edricchan/studybuddy/features/auth/navigation/graph/AuthGraph.kt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,30 @@ import com.edricchan.studybuddy.features.auth.navigation.R as NavR
1818
* @see AuthDestination
1919
*/
2020
context(context: Context)
21-
fun NavGraphBuilder.authGraph(
22-
isLoggedIn: Boolean
23-
) = navigation<AuthDestination.AuthGraphRoot>(
24-
startDestination = if (isLoggedIn) AuthDestination.AccountInfo() else AuthDestination.Login
25-
) {
26-
fragment<AccountFragment, AuthDestination.AccountInfo>(
27-
typeMap = mapOf(typeOf<AuthDestination.AccountInfo.AccountAction>() to AuthDestination.AccountInfo.AccountAction.NavType)
21+
fun NavGraphBuilder.authGraph() {
22+
navigation<AuthDestination.AuthGraphRoot>(
23+
startDestination = AuthDestination.AccountInfo()
2824
) {
29-
label = context.getString(NavR.string.nav_auth_dest_account_info_label)
25+
fragment<AccountFragment, AuthDestination.AccountInfo>(
26+
typeMap = mapOf(typeOf<AuthDestination.AccountInfo.AccountAction>() to AuthDestination.AccountInfo.AccountAction.NavType)
27+
) {
28+
label = context.getString(NavR.string.nav_auth_dest_account_info_label)
29+
}
3030
}
3131

32-
fragment<LoginFragment, AuthDestination.Login> {
33-
label = context.getString(NavR.string.nav_auth_dest_login_label)
34-
}
32+
navigation<AuthDestination.AuthGatewayGraphRoot>(
33+
startDestination = AuthDestination.Login
34+
) {
35+
fragment<LoginFragment, AuthDestination.Login> {
36+
label = context.getString(NavR.string.nav_auth_dest_login_label)
37+
}
3538

36-
fragment<RegisterFragment, AuthDestination.Register> {
37-
label = context.getString(NavR.string.nav_auth_dest_register_label)
38-
}
39+
fragment<RegisterFragment, AuthDestination.Register> {
40+
label = context.getString(NavR.string.nav_auth_dest_register_label)
41+
}
3942

40-
fragment<RecoveryFragment, AuthDestination.Recovery> {
41-
label = context.getString(NavR.string.nav_auth_dest_recover_account_label)
43+
fragment<RecoveryFragment, AuthDestination.Recovery> {
44+
label = context.getString(NavR.string.nav_auth_dest_recover_account_label)
45+
}
4246
}
4347
}

0 commit comments

Comments
 (0)