|
| 1 | +package co.nimblehq.template.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.compose.composable |
| 13 | +import co.nimblehq.template.compose.ui.base.BaseAppDestination |
| 14 | + |
| 15 | +private const val NavAnimationDurationInMillis = 300 |
| 16 | + |
| 17 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.enterSlideInLeftTransition() = |
| 18 | + slideIntoContainer( |
| 19 | + towards = AnimatedContentTransitionScope.SlideDirection.Start, |
| 20 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 21 | + ) |
| 22 | + |
| 23 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.exitSlideOutLeftTransition() = |
| 24 | + slideOutOfContainer( |
| 25 | + towards = AnimatedContentTransitionScope.SlideDirection.Start, |
| 26 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 27 | + ) |
| 28 | + |
| 29 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.enterSlideInRightTransition() = |
| 30 | + slideIntoContainer( |
| 31 | + towards = AnimatedContentTransitionScope.SlideDirection.End, |
| 32 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 33 | + ) |
| 34 | + |
| 35 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.exitSlideOutRightTransition() = |
| 36 | + slideOutOfContainer( |
| 37 | + towards = AnimatedContentTransitionScope.SlideDirection.End, |
| 38 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 39 | + ) |
| 40 | + |
| 41 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.enterSlideInUpTransition() = |
| 42 | + slideIntoContainer( |
| 43 | + towards = AnimatedContentTransitionScope.SlideDirection.Up, |
| 44 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 45 | + ) |
| 46 | + |
| 47 | +fun AnimatedContentTransitionScope<NavBackStackEntry>.exitSlideOutDownTransition() = |
| 48 | + slideOutOfContainer( |
| 49 | + towards = AnimatedContentTransitionScope.SlideDirection.Down, |
| 50 | + animationSpec = tween(NavAnimationDurationInMillis) |
| 51 | + ) |
| 52 | + |
| 53 | +fun NavGraphBuilder.composable( |
| 54 | + destination: BaseAppDestination, |
| 55 | + deepLinks: List<NavDeepLink> = emptyList(), |
| 56 | + enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = { |
| 57 | + enterSlideInLeftTransition() |
| 58 | + }, |
| 59 | + exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = { |
| 60 | + exitSlideOutLeftTransition() |
| 61 | + }, |
| 62 | + popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = { |
| 63 | + enterSlideInRightTransition() |
| 64 | + }, |
| 65 | + popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = { |
| 66 | + exitSlideOutRightTransition() |
| 67 | + }, |
| 68 | + content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit, |
| 69 | +) { |
| 70 | + composable( |
| 71 | + route = destination.route, |
| 72 | + arguments = destination.arguments, |
| 73 | + deepLinks = deepLinks, |
| 74 | + enterTransition = enterTransition, |
| 75 | + exitTransition = exitTransition, |
| 76 | + popEnterTransition = popEnterTransition, |
| 77 | + popExitTransition = popExitTransition, |
| 78 | + content = content |
| 79 | + ) |
| 80 | +} |
| 81 | + |
0 commit comments