Skip to content
This repository was archived by the owner on Aug 21, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions app/src/main/java/com/gravatar/app/navigation/RootNavigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.gravatar.app.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -18,19 +21,14 @@ import org.koin.compose.koinInject
@Composable
fun RootNavigation() {
val navController = rememberNavController()
val backStackEntry = navController.currentBackStackEntryAsState()
val backStackEntry by navController.currentBackStackEntryAsState()
val isUserLoggedIn: IsUserLoggedIn = koinInject<IsUserLoggedIn>()

LaunchedEffect(Unit) {
isUserLoggedIn()
.collect { userSession ->
val lastRoute = backStackEntry.value?.destination?.route?.let {
RootDestination.fromRoute(it)
} ?: RootDestination.Splash

val currentRoute = navController.currentDestination?.route?.let {
RootDestination.fromRoute(it)
}
val lastRoute =
backStackEntry?.destination?.rootDestination ?: RootDestination.Splash

when (userSession) {
LOGGED_IN -> navController.navigateToRootDestination(
Expand Down Expand Up @@ -65,11 +63,7 @@ private fun NavHostController.navigateToRootDestination(
popTo: RootDestination,
shouldSaveState: Boolean = true
) {
val currentRoute = currentDestination?.route?.let {
RootDestination.fromRoute(it)
}

if (currentRoute != destination) {
if (popTo != destination) {
navigate(destination) {
popUpTo(popTo) {
inclusive = true
Expand All @@ -81,6 +75,14 @@ private fun NavHostController.navigateToRootDestination(
}
}

private val NavDestination.rootDestination: RootDestination?
get() = when {
hasRoute(RootDestination.Splash::class) -> RootDestination.Splash
hasRoute(RootDestination.Login::class) -> RootDestination.Login
hasRoute(RootDestination.Home::class) -> RootDestination.Home
else -> null
}

internal sealed class RootDestination {
@Serializable
data object Splash : RootDestination()
Expand All @@ -90,15 +92,4 @@ internal sealed class RootDestination {

@Serializable
data object Home : RootDestination()

companion object {
fun fromRoute(route: String?): RootDestination? {
return when (route) {
Splash::class.qualifiedName -> Splash
Login::class.qualifiedName -> Login
Home::class.qualifiedName -> Home
else -> null
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ internal sealed class HomeDestination(
val labelRes: Int,
val position: Int,
) {
val route: String =
this::class.qualifiedName ?: error("Route name is not defined for $this")

@Serializable
data object Gravatar : HomeDestination(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
Expand Down Expand Up @@ -64,7 +65,7 @@ internal fun HomeScreen(
) {
val navController = rememberNavController()
val snackbarHostState = remember { SnackbarHostState() }
val backStackEntry = navController.currentBackStackEntryAsState()
val backStackEntry by navController.currentBackStackEntryAsState()

Scaffold(
contentWindowInsets = WindowInsets(0, 0, 0, 0),
Expand All @@ -83,9 +84,9 @@ internal fun HomeScreen(
)
},
label = { Text(stringResource(destination.labelRes)) },
selected = destination.route == backStackEntry.value?.destination?.route,
selected = backStackEntry?.destination?.hasRoute(destination::class) == true,
onClick = {
if (backStackEntry.value?.destination?.route != destination.route) {
if (backStackEntry?.destination?.hasRoute(destination::class) == false) {
navController.navigate(destination) {
popUpTo(navController.graph.startDestinationId) {
saveState = true
Expand Down