Skip to content

Commit

Permalink
refactor: simplify when to show back button by checking the previous …
Browse files Browse the repository at this point in the history
…back stack entry
  • Loading branch information
jarnedemeulemeester committed Dec 8, 2024
1 parent bf97265 commit c792266
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions app/phone/src/main/java/dev/jdtech/jellyfin/NavigationRoot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.navigation.NavOptionsBuilder
import androidx.navigation.Navigator
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import dev.jdtech.jellyfin.presentation.setup.addserver.AddServerScreen
import dev.jdtech.jellyfin.presentation.setup.login.LoginScreen
import dev.jdtech.jellyfin.presentation.setup.servers.ServersScreen
Expand All @@ -23,17 +22,13 @@ import kotlinx.serialization.Serializable
data object WelcomeRoute

@Serializable
data class ServersRoute(
val showBack: Boolean = true,
)
data object ServersRoute

@Serializable
data object AddServerRoute

@Serializable
data class UsersRoute(
val showBack: Boolean = true,
)
data object UsersRoute

@Serializable
data object LoginRoute
Expand All @@ -46,9 +41,9 @@ fun NavigationRoot(
hasCurrentUser: Boolean,
) {
val startDestination = when {
hasServers && hasCurrentServer && hasCurrentUser -> UsersRoute(showBack = false) // TODO: change to MainRoute
hasServers && hasCurrentServer -> UsersRoute(showBack = false)
hasServers -> ServersRoute(showBack = false)
hasServers && hasCurrentServer && hasCurrentUser -> UsersRoute // TODO: change to MainRoute
hasServers && hasCurrentServer -> UsersRoute
hasServers -> ServersRoute
else -> WelcomeRoute
}
NavHost(
Expand All @@ -69,21 +64,20 @@ fun NavigationRoot(
)
}
composable<ServersRoute> { backStackEntry ->
val route: ServersRoute = backStackEntry.toRoute()
ServersScreen(
navigateToLogin = {
navController.safeNavigate(LoginRoute)
},
navigateToUsers = {
navController.safeNavigate(UsersRoute())
navController.safeNavigate(UsersRoute)
},
onAddClick = {
navController.safeNavigate(AddServerRoute)
},
onBackClick = {
navController.safePopBackStack()
},
showBack = route.showBack,
showBack = navController.previousBackStackEntry != null && navController.previousBackStackEntry != backStackEntry,
)
}
composable<AddServerRoute> {
Expand All @@ -97,12 +91,11 @@ fun NavigationRoot(
)
}
composable<UsersRoute> { backStackEntry ->
val route: UsersRoute = backStackEntry.toRoute()
UsersScreen(
navigateToHome = {},
onChangeServerClick = {
navController.safeNavigate(ServersRoute()) {
popUpTo(ServersRoute()) {
navController.safeNavigate(ServersRoute) {
popUpTo(ServersRoute) {
inclusive = false
}
launchSingleTop = true
Expand All @@ -114,15 +107,15 @@ fun NavigationRoot(
onBackClick = {
navController.safePopBackStack()
},
showBack = route.showBack,
showBack = navController.previousBackStackEntry != null && navController.previousBackStackEntry != backStackEntry,
)
}
composable<LoginRoute> {
LoginScreen(
onSuccess = {},
onChangeServerClick = {
navController.safeNavigate(ServersRoute()) {
popUpTo(ServersRoute()) {
navController.safeNavigate(ServersRoute) {
popUpTo(ServersRoute) {
inclusive = false
}
launchSingleTop = true
Expand Down

0 comments on commit c792266

Please sign in to comment.