|
| 1 | +package co.nimblehq.sample.compose.extensions |
| 2 | + |
| 3 | +import androidx.compose.animation.AnimatedContentScope |
| 4 | +import androidx.compose.animation.AnimatedContentTransitionScope |
| 5 | +import androidx.compose.animation.EnterTransition |
| 6 | +import androidx.compose.animation.ExitTransition |
| 7 | +import androidx.compose.animation.core.tween |
| 8 | +import androidx.compose.runtime.Composable |
| 9 | +import androidx.navigation.NavBackStackEntry |
| 10 | +import androidx.navigation.NavDeepLink |
| 11 | +import androidx.navigation.NavGraphBuilder |
| 12 | +import androidx.navigation.NavHostController |
| 13 | +import androidx.navigation.NavOptionsBuilder |
| 14 | +import androidx.navigation.compose.composable |
| 15 | +import co.nimblehq.sample.compose.ui.base.BaseAppDestination |
| 16 | + |
| 17 | +/** |
| 18 | + * Use this extension or [navigate(BaseAppDestination.Up())] to prevent duplicated navigation events |
| 19 | + */ |
| 20 | +fun NavHostController.navigateAppDestinationUp() { |
| 21 | + navigateTo(BaseAppDestination.Up()) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * TODO Create new class extend NavHostController then move the related codes to that class |
| 26 | + */ |
| 27 | +private const val IntervalInMillis: Long = 1000L |
| 28 | +private var lastNavigationEventExecutedTimeInMillis: Long = 0L |
| 29 | + |
| 30 | +/** |
| 31 | + * Use this extension to prevent duplicated navigation events with the same destination in a short time |
| 32 | + */ |
| 33 | +private fun NavHostController.throttleNavigation( |
| 34 | + appDestination: BaseAppDestination, |
| 35 | + onNavigate: () -> Unit, |
| 36 | +) { |
| 37 | + val currentTime = System.currentTimeMillis() |
| 38 | + if (currentBackStackEntry?.destination?.route == appDestination.route |
| 39 | + && (currentTime - lastNavigationEventExecutedTimeInMillis < IntervalInMillis) |
| 40 | + ) { |
| 41 | + return |
| 42 | + } |
| 43 | + lastNavigationEventExecutedTimeInMillis = currentTime |
| 44 | + |
| 45 | + onNavigate() |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Navigate to provided [BaseAppDestination] |
| 50 | + * Caution to use this method. This method use savedStateHandle to store the Parcelable data. |
| 51 | + * When previousBackstackEntry is popped out from navigation stack, savedStateHandle will return null and cannot retrieve data. |
| 52 | + * eg.Login -> Home, the Login screen will be popped from the back-stack on logging in successfully. |
| 53 | + */ |
| 54 | +fun <T : BaseAppDestination> NavHostController.navigateTo( |
| 55 | + appDestination: T, |
| 56 | + builder: (NavOptionsBuilder.() -> Unit)? = null, |
| 57 | +) = throttleNavigation(appDestination) { |
| 58 | + when (appDestination) { |
| 59 | + is BaseAppDestination.Up -> { |
| 60 | + appDestination.results.forEach { (key, value) -> |
| 61 | + previousBackStackEntry?.savedStateHandle?.set(key, value) |
| 62 | + } |
| 63 | + navigateUp() |
| 64 | + } |
| 65 | + else -> { |
| 66 | + appDestination.parcelableArgument?.let { (key, value) -> |
| 67 | + currentBackStackEntry?.savedStateHandle?.set(key, value) |
| 68 | + } |
| 69 | + navigate(route = appDestination.destination) { |
| 70 | + if (builder != null) { |
| 71 | + builder() |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +private const val NavAnimationDurationInMillis = 300 |
| 79 | + |
| 80 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.enterSlideInLeftTransition() = |
| 81 | + slideIntoContainer( |
| 82 | + towards = AnimatedContentTransitionScope.SlideDirection.Start, |
| 83 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 84 | + ) |
| 85 | + |
| 86 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.exitSlideOutLeftTransition() = |
| 87 | + slideOutOfContainer( |
| 88 | + towards = AnimatedContentTransitionScope.SlideDirection.Start, |
| 89 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 90 | + ) |
| 91 | + |
| 92 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.enterSlideInRightTransition() = |
| 93 | + slideIntoContainer( |
| 94 | + towards = AnimatedContentTransitionScope.SlideDirection.End, |
| 95 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 96 | + ) |
| 97 | + |
| 98 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.exitSlideOutRightTransition() = |
| 99 | + slideOutOfContainer( |
| 100 | + towards = AnimatedContentTransitionScope.SlideDirection.End, |
| 101 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 102 | + ) |
| 103 | + |
| 104 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.enterSlideInUpTransition() = |
| 105 | + slideIntoContainer( |
| 106 | + towards = AnimatedContentTransitionScope.SlideDirection.Up, |
| 107 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 108 | + ) |
| 109 | + |
| 110 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.exitSlideOutDownTransition() = |
| 111 | + slideOutOfContainer( |
| 112 | + towards = AnimatedContentTransitionScope.SlideDirection.Down, |
| 113 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 114 | + ) |
| 115 | + |
| 116 | +fun NavGraphBuilder.composable( |
| 117 | + destination: BaseAppDestination, |
| 118 | + deepLinks: List<NavDeepLink> = emptyList(), |
| 119 | + enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = { |
| 120 | + enterSlideInLeftTransition() |
| 121 | + }, |
| 122 | + exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = { |
| 123 | + exitSlideOutLeftTransition() |
| 124 | + }, |
| 125 | + popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = { |
| 126 | + enterSlideInRightTransition() |
| 127 | + }, |
| 128 | + popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = { |
| 129 | + exitSlideOutRightTransition() |
| 130 | + }, |
| 131 | + content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit, |
| 132 | +) { |
| 133 | + composable( |
| 134 | + route = destination.route, |
| 135 | + arguments = destination.arguments, |
| 136 | + deepLinks = deepLinks, |
| 137 | + enterTransition = enterTransition, |
| 138 | + exitTransition = exitTransition, |
| 139 | + popEnterTransition = popEnterTransition, |
| 140 | + popExitTransition = popExitTransition, |
| 141 | + content = content |
| 142 | + ) |
| 143 | +} |
0 commit comments