Improve screen transitions

This commit is contained in:
vfsfitvnm
2022-09-29 18:01:32 +02:00
parent 6f222ac564
commit 982a7eaca7
4 changed files with 69 additions and 18 deletions

View File

@@ -21,7 +21,13 @@ fun RouteHandler(
modifier: Modifier = Modifier,
listenToGlobalEmitter: Boolean = false,
handleBackPress: Boolean = true,
transitionSpec: AnimatedContentScope<RouteHandlerScope>.() -> ContentTransform = { fastFade },
transitionSpec: AnimatedContentScope<RouteHandlerScope>.() -> ContentTransform = {
when {
isStacking -> defaultStacking
isStill -> defaultStill
else -> defaultUnstacking
}
},
content: @Composable RouteHandlerScope.() -> Unit
) {
var route by rememberSaveable(stateSaver = Route.Saver) {
@@ -47,7 +53,13 @@ fun RouteHandler(
modifier: Modifier = Modifier,
listenToGlobalEmitter: Boolean = false,
handleBackPress: Boolean = true,
transitionSpec: AnimatedContentScope<RouteHandlerScope>.() -> ContentTransform = { fastFade },
transitionSpec: AnimatedContentScope<RouteHandlerScope>.() -> ContentTransform = {
when {
isStacking -> defaultStacking
isStill -> defaultStill
else -> defaultUnstacking
}
},
content: @Composable RouteHandlerScope.() -> Unit
) {
val backDispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher

View File

@@ -3,27 +3,44 @@ package it.vfsfitvnm.route
import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.ContentTransform
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.with
import androidx.compose.animation.scaleOut
@ExperimentalAnimationApi
val AnimatedContentScope<RouteHandlerScope>.leftSlide: ContentTransform
get() = slideIntoContainer(AnimatedContentScope.SlideDirection.Left) with
slideOutOfContainer(AnimatedContentScope.SlideDirection.Left)
val defaultStacking = ContentTransform(
initialContentExit = scaleOut(targetScale = 0.9f) + fadeOut(),
targetContentEnter = fadeIn(),
targetContentZIndex = 1f
)
@ExperimentalAnimationApi
val AnimatedContentScope<RouteHandlerScope>.rightSlide: ContentTransform
get() = slideIntoContainer(AnimatedContentScope.SlideDirection.Right) with
slideOutOfContainer(AnimatedContentScope.SlideDirection.Right)
val defaultUnstacking = ContentTransform(
initialContentExit = fadeOut(),
targetContentEnter = EnterTransition.None,
targetContentZIndex = 0f
)
@ExperimentalAnimationApi
val AnimatedContentScope<RouteHandlerScope>.fastFade: ContentTransform
get() = fadeIn(tween(200)) with fadeOut(tween(200))
val defaultStill = ContentTransform(
initialContentExit = scaleOut(targetScale = 0.9f) + fadeOut(),
targetContentEnter = fadeIn(),
targetContentZIndex = 1f
)
@ExperimentalAnimationApi
val AnimatedContentScope<RouteHandlerScope>.empty: ContentTransform
get() = EnterTransition.None with ExitTransition.None
inline val AnimatedContentScope<RouteHandlerScope>.isStacking: Boolean
get() = initialState.route == null && targetState.route != null
@ExperimentalAnimationApi
inline val AnimatedContentScope<RouteHandlerScope>.isUnstacking: Boolean
get() = initialState.route != null && targetState.route == null
@ExperimentalAnimationApi
inline val AnimatedContentScope<RouteHandlerScope>.isStill: Boolean
get() = initialState.route == null && targetState.route == null
@ExperimentalAnimationApi
inline val AnimatedContentScope<RouteHandlerScope>.isUnknown: Boolean
get() = initialState.route != null && targetState.route != null