Skip to content

Commit

Permalink
fix(phone): only navigate when current lifecycle state is resumed and…
Browse files Browse the repository at this point in the history
… increase navigation animation speed
  • Loading branch information
jarnedemeulemeester committed Dec 7, 2024
1 parent 1d9f665 commit ddc6df8
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions app/phone/src/main/java/dev/jdtech/jellyfin/NavigationRoot.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package dev.jdtech.jellyfin

import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.runtime.Composable
import androidx.lifecycle.Lifecycle
import androidx.navigation.NavHostController
import androidx.navigation.NavOptions
import androidx.navigation.NavOptionsBuilder
import androidx.navigation.Navigator
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
Expand Down Expand Up @@ -47,39 +54,45 @@ fun NavigationRoot(
NavHost(
navController = navController,
startDestination = startDestination,
enterTransition = {
fadeIn(tween(300))
},
exitTransition = {
fadeOut(tween(300))
},
) {
composable<WelcomeRoute> {
WelcomeScreen(
onContinueClick = {
navController.navigate(ServersRoute())
navController.safeNavigate(ServersRoute())
},
)
}
composable<ServersRoute> { backStackEntry ->
val route: ServersRoute = backStackEntry.toRoute()
ServersScreen(
navigateToLogin = {
navController.navigate(LoginRoute)
navController.safeNavigate(LoginRoute)
},
navigateToUsers = {
navController.navigate(UsersRoute())
navController.safeNavigate(UsersRoute())
},
onAddClick = {
navController.navigate(AddServerRoute)
navController.safeNavigate(AddServerRoute)
},
onBackClick = {
navController.popBackStack()
navController.safePopBackStack()
},
showBack = route.showBack,
)
}
composable<AddServerRoute> {
AddServerScreen(
onSuccess = {
navController.navigate(LoginRoute)
navController.safeNavigate(LoginRoute)
},
onBackClick = {
navController.popBackStack()
navController.safePopBackStack()
},
)
}
Expand All @@ -88,18 +101,18 @@ fun NavigationRoot(
UsersScreen(
navigateToHome = {},
onChangeServerClick = {
navController.navigate(ServersRoute()) {
navController.safeNavigate(ServersRoute()) {
popUpTo(ServersRoute()) {
inclusive = false
}
launchSingleTop = true
}
},
onAddClick = {
navController.navigate(LoginRoute)
navController.safeNavigate(LoginRoute)
},
onBackClick = {
navController.popBackStack()
navController.safePopBackStack()
},
showBack = route.showBack,
)
Expand All @@ -108,17 +121,37 @@ fun NavigationRoot(
LoginScreen(
onSuccess = {},
onChangeServerClick = {
navController.navigate(ServersRoute()) {
navController.safeNavigate(ServersRoute()) {
popUpTo(ServersRoute()) {
inclusive = false
}
launchSingleTop = true
}
},
onBackClick = {
navController.popBackStack()
navController.safePopBackStack()
},
)
}
}
}

private fun <T : Any> NavHostController.safeNavigate(route: T, navOptions: NavOptions? = null, navigatorExtras: Navigator.Extras? = null) {
if (this.currentBackStackEntry?.lifecycle?.currentState == Lifecycle.State.RESUMED) {
this.navigate(route, navOptions, navigatorExtras)
}
}

private fun <T : Any> NavHostController.safeNavigate(route: T, builder: NavOptionsBuilder.() -> Unit) {
if (this.currentBackStackEntry?.lifecycle?.currentState == Lifecycle.State.RESUMED) {
this.navigate(route, builder)
}
}

private fun NavHostController.safePopBackStack(): Boolean {
return if (this.currentBackStackEntry?.lifecycle?.currentState == Lifecycle.State.RESUMED) {
this.popBackStack()
} else {
false
}
}

0 comments on commit ddc6df8

Please sign in to comment.