11package com.flamyoad.filemanager.ui
22
3+ import androidx.compose.animation.AnimatedContentTransitionScope
34import androidx.compose.animation.EnterTransition
45import androidx.compose.animation.ExitTransition
6+ import androidx.compose.animation.core.tween
57import androidx.compose.runtime.Composable
68import androidx.compose.ui.Modifier
9+ import androidx.lifecycle.Lifecycle
10+ import androidx.lifecycle.compose.dropUnlessResumed
11+ import androidx.navigation.NavController
712import androidx.navigation.NavHostController
813import androidx.navigation.compose.NavHost
14+ import com.flamyoad.common.Route
915import com.flamyoad.explorer_impl.FileListRoute
1016import com.flamyoad.explorer_impl.HomePageRoute
1117import com.flamyoad.explorer_impl.fileListRoute
@@ -20,20 +26,49 @@ fun FileManagerNavHost(
2026 NavHost (
2127 navController = navController,
2228 startDestination = HomePageRoute ,
23- enterTransition = { EnterTransition .None },
24- exitTransition = { ExitTransition .None },
29+ enterTransition = {
30+ slideIntoContainer(
31+ AnimatedContentTransitionScope .SlideDirection .Start ,
32+ tween(500 )
33+ )
34+ },
35+ exitTransition = {
36+ slideOutOfContainer(
37+ AnimatedContentTransitionScope .SlideDirection .Start ,
38+ tween(500 )
39+ )
40+ },
2541 modifier = modifier
2642 ) {
2743 homePageRoute(
2844 onNavigateToFileList = { directory ->
29- navController.navigate (FileListRoute (directory))
45+ navController.navigateSafely (FileListRoute (directory))
3046 }
3147 )
3248 fileListRoute(
3349 onNavigateToFileList = { directory ->
34- navController.navigate (FileListRoute (directory))
50+ navController.navigateSafely (FileListRoute (directory))
3551 }
3652 )
3753 imageViewerRoute()
3854 }
55+ }
56+
57+ // dropUnlessResumed is used to avoid navigating multiple times to the same destination or
58+ // popping the backstack when the destination is already on top.
59+ // https://github.com/seve-andre/jetpack-compose-template/pull/37
60+ // @Composable
61+ // fun NavController.navigateTo(
62+ // route: Route,
63+ // ): () -> Unit = dropUnlessResumed { this.navigate(route) }
64+
65+
66+ // https://slack-chats.kotlinlang.org/t/18829110/hey-guys-i-m-trying-to-wrap-my-head-around-the-new-dropunles
67+ // https://github.com/HedvigInsurance/android/blob/develop/app%2Fapp%2Fsrc%2Fmain%2Fkotlin%2Fcom%2Fhedvig%2Fandroid%2Fapp%2Fnavigation%2FRememberNavigator.kt#L20-L28
68+ // Pros: Doesn't require @Composable context
69+ fun NavController.navigateSafely (route : Any ) {
70+ val currentBackStackEntry = this .currentBackStackEntry ? : return
71+ if (currentBackStackEntry.lifecycle.currentState == Lifecycle .State .RESUMED ) {
72+ this .navigate(route)
73+ }
3974}
0 commit comments