Skip to content

Commit 436bf68

Browse files
committed
refactor: reuse animations
1 parent 2844092 commit 436bf68

File tree

1 file changed

+53
-70
lines changed

1 file changed

+53
-70
lines changed

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

Lines changed: 53 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import androidx.compose.animation.core.FastOutSlowInEasing
88
import androidx.compose.animation.core.tween
99
import androidx.compose.animation.fadeIn
1010
import androidx.compose.animation.fadeOut
11-
import androidx.compose.animation.scaleIn
12-
import androidx.compose.animation.scaleOut
1311
import androidx.compose.animation.slideInHorizontally
1412
import androidx.compose.animation.slideInVertically
1513
import androidx.compose.animation.slideOutHorizontally
@@ -22,113 +20,70 @@ import androidx.navigation.NavGraphBuilder
2220
import androidx.navigation.NavType
2321
import androidx.navigation.compose.composable
2422
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
2527
import kotlin.reflect.KType
2628

29+
@Suppress("MagicNumber")
2730
object Transitions {
2831
val slideInHorizontally = slideInHorizontally(animationSpec = tween(), initialOffsetX = { it })
2932
val slideOutHorizontally = slideOutHorizontally(animationSpec = tween(), targetOffsetX = { it })
30-
val scaleIn = scaleIn(animationSpec = tween(), initialScale = 0.95f) + fadeIn()
31-
val scaleOut = scaleOut(animationSpec = tween(), targetScale = 0.95f) + fadeOut()
3233
val slideInVertically = slideInVertically(animationSpec = tween(), initialOffsetY = { it })
3334
val slideOutVertically = slideOutVertically(animationSpec = tween(), targetOffsetY = { it })
34-
}
3535

36-
/**
37-
* Adds the [Composable] to the [NavGraphBuilder] with the default screen transitions.
38-
*/
39-
@Suppress("LongParameterList", "MagicNumber")
40-
inline fun <reified T : Any> NavGraphBuilder.composableWithDefaultTransitions(
41-
typeMap: Map<KType, NavType<*>> = emptyMap(),
42-
deepLinks: List<NavDeepLink> = emptyList(),
43-
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
44-
// New screen slides in from the right
36+
val defaultEnterTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?) = {
4537
slideInHorizontally(
4638
initialOffsetX = { fullWidth -> fullWidth },
4739
animationSpec = tween(300, easing = FastOutSlowInEasing)
4840
)
49-
},
50-
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
51-
// Current screen slides out to the left (partially visible behind new screen)
41+
}
42+
43+
val defaultExitTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?) = {
5244
slideOutHorizontally(
5345
targetOffsetX = { fullWidth -> -fullWidth / 3 },
5446
animationSpec = tween(300, easing = FastOutSlowInEasing)
5547
) + fadeOut(
5648
animationSpec = tween(300, easing = FastOutSlowInEasing),
5749
targetAlpha = 0.8f
5850
)
59-
},
60-
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
61-
// Previous screen slides in from the left (was partially visible)
51+
}
52+
53+
val defaultPopEnterTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?) = {
6254
slideInHorizontally(
6355
initialOffsetX = { fullWidth -> -fullWidth / 3 },
6456
animationSpec = tween(300, easing = FastOutSlowInEasing)
6557
) + fadeIn(
6658
animationSpec = tween(300, easing = FastOutSlowInEasing),
6759
initialAlpha = 0.8f
6860
)
69-
},
70-
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
71-
// Current screen slides out to the right
61+
}
62+
63+
val defaultPopExitTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?) = {
7264
slideOutHorizontally(
7365
targetOffsetX = { fullWidth -> fullWidth },
7466
animationSpec = tween(300, easing = FastOutSlowInEasing)
7567
)
76-
},
77-
noinline content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit,
78-
) {
79-
composable<T>(
80-
typeMap = typeMap,
81-
deepLinks = deepLinks,
82-
enterTransition = enterTransition,
83-
exitTransition = exitTransition,
84-
popEnterTransition = popEnterTransition,
85-
popExitTransition = popExitTransition,
86-
content = content,
87-
)
68+
}
8869
}
8970

9071
/**
9172
* Construct a nested [NavGraph] with the default screen transitions.
9273
*/
93-
@Suppress("LongParameterList", "MagicNumber")
74+
@Suppress("LongParameterList")
9475
inline fun <reified T : Any> NavGraphBuilder.navigationWithDefaultTransitions(
9576
startDestination: Any,
9677
typeMap: Map<KType, @JvmSuppressWildcards NavType<*>> = emptyMap(),
9778
deepLinks: List<NavDeepLink> = emptyList(),
98-
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
99-
// New screen slides in from the right
100-
slideInHorizontally(
101-
initialOffsetX = { fullWidth -> fullWidth },
102-
animationSpec = tween(300, easing = FastOutSlowInEasing)
103-
)
104-
},
105-
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
106-
// Current screen slides out to the left (partially visible behind new screen)
107-
slideOutHorizontally(
108-
targetOffsetX = { fullWidth -> -fullWidth / 3 },
109-
animationSpec = tween(300, easing = FastOutSlowInEasing)
110-
) + fadeOut(
111-
animationSpec = tween(300, easing = FastOutSlowInEasing),
112-
targetAlpha = 0.8f
113-
)
114-
},
115-
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
116-
// Previous screen slides in from the left (was partially visible)
117-
slideInHorizontally(
118-
initialOffsetX = { fullWidth -> -fullWidth / 3 },
119-
animationSpec = tween(300, easing = FastOutSlowInEasing)
120-
) + fadeIn(
121-
animationSpec = tween(300, easing = FastOutSlowInEasing),
122-
initialAlpha = 0.8f
123-
)
124-
},
125-
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
126-
// Current screen slides out to the right
127-
slideOutHorizontally(
128-
targetOffsetX = { fullWidth -> fullWidth },
129-
animationSpec = tween(300, easing = FastOutSlowInEasing)
130-
)
131-
},
79+
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)?
80+
= defaultEnterTrans,
81+
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)?
82+
= defaultExitTrans,
83+
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)?
84+
= defaultPopEnterTrans,
85+
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)?
86+
= defaultPopExitTrans,
13287
noinline builder: NavGraphBuilder.() -> Unit,
13388
) {
13489
navigation<T>(
@@ -142,3 +97,31 @@ inline fun <reified T : Any> NavGraphBuilder.navigationWithDefaultTransitions(
14297
builder = builder,
14398
)
14499
}
100+
101+
/**
102+
* Adds the [Composable] to the [NavGraphBuilder] with the default screen transitions.
103+
*/
104+
@Suppress("LongParameterList")
105+
inline fun <reified T : Any> NavGraphBuilder.composableWithDefaultTransitions(
106+
typeMap: Map<KType, NavType<*>> = emptyMap(),
107+
deepLinks: List<NavDeepLink> = emptyList(),
108+
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)?
109+
= defaultEnterTrans,
110+
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)?
111+
= defaultExitTrans,
112+
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)?
113+
= defaultPopEnterTrans,
114+
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)?
115+
= defaultPopExitTrans,
116+
noinline content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit,
117+
) {
118+
composable<T>(
119+
typeMap = typeMap,
120+
deepLinks = deepLinks,
121+
enterTransition = enterTransition,
122+
exitTransition = exitTransition,
123+
popEnterTransition = popEnterTransition,
124+
popExitTransition = popExitTransition,
125+
content = content,
126+
)
127+
}

0 commit comments

Comments
 (0)