Skip to content

Commit 0ccd550

Browse files
authored
Merge pull request #350 from synonymdev/feat/smooth-default-transition
Smooth default transition
2 parents 273107a + 90d124c commit 0ccd550

File tree

1 file changed

+64
-44
lines changed

1 file changed

+64
-44
lines changed

app/src/main/java/to/bitkit/ui/utils/Transitions.kt

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import androidx.compose.animation.AnimatedContentScope
44
import androidx.compose.animation.AnimatedContentTransitionScope
55
import androidx.compose.animation.EnterTransition
66
import androidx.compose.animation.ExitTransition
7+
import androidx.compose.animation.core.FastOutSlowInEasing
78
import androidx.compose.animation.core.tween
89
import androidx.compose.animation.fadeIn
910
import androidx.compose.animation.fadeOut
10-
import androidx.compose.animation.scaleIn
11-
import androidx.compose.animation.scaleOut
1211
import androidx.compose.animation.slideInHorizontally
1312
import androidx.compose.animation.slideInVertically
1413
import androidx.compose.animation.slideOutHorizontally
@@ -21,79 +20,100 @@ import androidx.navigation.NavGraphBuilder
2120
import androidx.navigation.NavType
2221
import androidx.navigation.compose.composable
2322
import androidx.navigation.compose.navigation
23+
import to.bitkit.ui.utils.Transitions.defaultEnterTrans
24+
import to.bitkit.ui.utils.Transitions.defaultExitTrans
25+
import to.bitkit.ui.utils.Transitions.defaultPopEnterTrans
26+
import to.bitkit.ui.utils.Transitions.defaultPopExitTrans
2427
import kotlin.reflect.KType
2528

29+
@Suppress("MagicNumber")
2630
object Transitions {
2731
val slideInHorizontally = slideInHorizontally(animationSpec = tween(), initialOffsetX = { it })
2832
val slideOutHorizontally = slideOutHorizontally(animationSpec = tween(), targetOffsetX = { it })
29-
val scaleIn = scaleIn(animationSpec = tween(), initialScale = 0.95f) + fadeIn()
30-
val scaleOut = scaleOut(animationSpec = tween(), targetScale = 0.95f) + fadeOut()
3133
val slideInVertically = slideInVertically(animationSpec = tween(), initialOffsetY = { it })
3234
val slideOutVertically = slideOutVertically(animationSpec = tween(), targetOffsetY = { it })
35+
36+
val defaultEnterTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?) = {
37+
slideInHorizontally(
38+
initialOffsetX = { fullWidth -> fullWidth },
39+
animationSpec = tween(300, easing = FastOutSlowInEasing)
40+
)
41+
}
42+
43+
val defaultExitTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?) = {
44+
slideOutHorizontally(
45+
targetOffsetX = { fullWidth -> -fullWidth / 3 },
46+
animationSpec = tween(300, easing = FastOutSlowInEasing)
47+
) + fadeOut(
48+
animationSpec = tween(300, easing = FastOutSlowInEasing),
49+
targetAlpha = 0.8f
50+
)
51+
}
52+
53+
val defaultPopEnterTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?) = {
54+
slideInHorizontally(
55+
initialOffsetX = { fullWidth -> -fullWidth / 3 },
56+
animationSpec = tween(300, easing = FastOutSlowInEasing)
57+
) + fadeIn(
58+
animationSpec = tween(300, easing = FastOutSlowInEasing),
59+
initialAlpha = 0.8f
60+
)
61+
}
62+
63+
val defaultPopExitTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?) = {
64+
slideOutHorizontally(
65+
targetOffsetX = { fullWidth -> fullWidth },
66+
animationSpec = tween(300, easing = FastOutSlowInEasing)
67+
)
68+
}
3369
}
3470

3571
/**
36-
* Adds the [Composable] to the [NavGraphBuilder] with the default screen transitions.
72+
* Construct a nested [NavGraph] with the default screen transitions.
3773
*/
38-
@Suppress("LongParameterList")
39-
inline fun <reified T : Any> NavGraphBuilder.composableWithDefaultTransitions(
40-
typeMap: Map<KType, NavType<*>> = emptyMap(),
74+
@Suppress("LongParameterList", "MaxLineLength")
75+
inline fun <reified T : Any> NavGraphBuilder.navigationWithDefaultTransitions(
76+
startDestination: Any,
77+
typeMap: Map<KType, @JvmSuppressWildcards NavType<*>> = emptyMap(),
4178
deepLinks: List<NavDeepLink> = emptyList(),
42-
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
43-
Transitions.slideInHorizontally
44-
},
45-
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
46-
Transitions.scaleOut
47-
},
48-
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
49-
Transitions.scaleIn
50-
},
51-
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
52-
Transitions.slideOutHorizontally
53-
},
54-
noinline content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit,
79+
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = defaultEnterTrans,
80+
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = defaultExitTrans,
81+
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = defaultPopEnterTrans,
82+
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = defaultPopExitTrans,
83+
noinline builder: NavGraphBuilder.() -> Unit,
5584
) {
56-
composable<T>(
85+
navigation<T>(
86+
startDestination = startDestination,
5787
typeMap = typeMap,
5888
deepLinks = deepLinks,
5989
enterTransition = enterTransition,
6090
exitTransition = exitTransition,
6191
popEnterTransition = popEnterTransition,
6292
popExitTransition = popExitTransition,
63-
content = content,
93+
builder = builder,
6494
)
6595
}
6696

6797
/**
68-
* Construct a nested [NavGraph] with the default screen transitions.
98+
* Adds the [Composable] to the [NavGraphBuilder] with the default screen transitions.
6999
*/
70-
@Suppress("LongParameterList")
71-
inline fun <reified T : Any> NavGraphBuilder.navigationWithDefaultTransitions(
72-
startDestination: Any,
73-
typeMap: Map<KType, @JvmSuppressWildcards NavType<*>> = emptyMap(),
100+
@Suppress("LongParameterList", "MaxLineLength")
101+
inline fun <reified T : Any> NavGraphBuilder.composableWithDefaultTransitions(
102+
typeMap: Map<KType, NavType<*>> = emptyMap(),
74103
deepLinks: List<NavDeepLink> = emptyList(),
75-
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
76-
Transitions.slideInHorizontally
77-
},
78-
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
79-
Transitions.scaleOut
80-
},
81-
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
82-
Transitions.scaleIn
83-
},
84-
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
85-
Transitions.slideOutHorizontally
86-
},
87-
noinline builder: NavGraphBuilder.() -> Unit,
104+
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = defaultEnterTrans,
105+
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = defaultExitTrans,
106+
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = defaultPopEnterTrans,
107+
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = defaultPopExitTrans,
108+
noinline content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit,
88109
) {
89-
navigation<T>(
90-
startDestination = startDestination,
110+
composable<T>(
91111
typeMap = typeMap,
92112
deepLinks = deepLinks,
93113
enterTransition = enterTransition,
94114
exitTransition = exitTransition,
95115
popEnterTransition = popEnterTransition,
96116
popExitTransition = popExitTransition,
97-
builder = builder,
117+
content = content,
98118
)
99119
}

0 commit comments

Comments
 (0)