diff --git a/app/src/main/java/to/bitkit/ui/utils/Transitions.kt b/app/src/main/java/to/bitkit/ui/utils/Transitions.kt index 6c1fefb2e..d40cef1c1 100644 --- a/app/src/main/java/to/bitkit/ui/utils/Transitions.kt +++ b/app/src/main/java/to/bitkit/ui/utils/Transitions.kt @@ -4,11 +4,10 @@ import androidx.compose.animation.AnimatedContentScope import androidx.compose.animation.AnimatedContentTransitionScope import androidx.compose.animation.EnterTransition import androidx.compose.animation.ExitTransition +import androidx.compose.animation.core.FastOutSlowInEasing import androidx.compose.animation.core.tween import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut -import androidx.compose.animation.scaleIn -import androidx.compose.animation.scaleOut import androidx.compose.animation.slideInHorizontally import androidx.compose.animation.slideInVertically import androidx.compose.animation.slideOutHorizontally @@ -21,79 +20,100 @@ import androidx.navigation.NavGraphBuilder import androidx.navigation.NavType import androidx.navigation.compose.composable import androidx.navigation.compose.navigation +import to.bitkit.ui.utils.Transitions.defaultEnterTrans +import to.bitkit.ui.utils.Transitions.defaultExitTrans +import to.bitkit.ui.utils.Transitions.defaultPopEnterTrans +import to.bitkit.ui.utils.Transitions.defaultPopExitTrans import kotlin.reflect.KType +@Suppress("MagicNumber") object Transitions { val slideInHorizontally = slideInHorizontally(animationSpec = tween(), initialOffsetX = { it }) val slideOutHorizontally = slideOutHorizontally(animationSpec = tween(), targetOffsetX = { it }) - val scaleIn = scaleIn(animationSpec = tween(), initialScale = 0.95f) + fadeIn() - val scaleOut = scaleOut(animationSpec = tween(), targetScale = 0.95f) + fadeOut() val slideInVertically = slideInVertically(animationSpec = tween(), initialOffsetY = { it }) val slideOutVertically = slideOutVertically(animationSpec = tween(), targetOffsetY = { it }) + + val defaultEnterTrans: (AnimatedContentTransitionScope.() -> EnterTransition?) = { + slideInHorizontally( + initialOffsetX = { fullWidth -> fullWidth }, + animationSpec = tween(300, easing = FastOutSlowInEasing) + ) + } + + val defaultExitTrans: (AnimatedContentTransitionScope.() -> ExitTransition?) = { + slideOutHorizontally( + targetOffsetX = { fullWidth -> -fullWidth / 3 }, + animationSpec = tween(300, easing = FastOutSlowInEasing) + ) + fadeOut( + animationSpec = tween(300, easing = FastOutSlowInEasing), + targetAlpha = 0.8f + ) + } + + val defaultPopEnterTrans: (AnimatedContentTransitionScope.() -> EnterTransition?) = { + slideInHorizontally( + initialOffsetX = { fullWidth -> -fullWidth / 3 }, + animationSpec = tween(300, easing = FastOutSlowInEasing) + ) + fadeIn( + animationSpec = tween(300, easing = FastOutSlowInEasing), + initialAlpha = 0.8f + ) + } + + val defaultPopExitTrans: (AnimatedContentTransitionScope.() -> ExitTransition?) = { + slideOutHorizontally( + targetOffsetX = { fullWidth -> fullWidth }, + animationSpec = tween(300, easing = FastOutSlowInEasing) + ) + } } /** - * Adds the [Composable] to the [NavGraphBuilder] with the default screen transitions. + * Construct a nested [NavGraph] with the default screen transitions. */ -@Suppress("LongParameterList") -inline fun NavGraphBuilder.composableWithDefaultTransitions( - typeMap: Map> = emptyMap(), +@Suppress("LongParameterList", "MaxLineLength") +inline fun NavGraphBuilder.navigationWithDefaultTransitions( + startDestination: Any, + typeMap: Map> = emptyMap(), deepLinks: List = emptyList(), - noinline enterTransition: (AnimatedContentTransitionScope.() -> EnterTransition?)? = { - Transitions.slideInHorizontally - }, - noinline exitTransition: (AnimatedContentTransitionScope.() -> ExitTransition?)? = { - Transitions.scaleOut - }, - noinline popEnterTransition: (AnimatedContentTransitionScope.() -> EnterTransition?)? = { - Transitions.scaleIn - }, - noinline popExitTransition: (AnimatedContentTransitionScope.() -> ExitTransition?)? = { - Transitions.slideOutHorizontally - }, - noinline content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit, + noinline enterTransition: (AnimatedContentTransitionScope.() -> EnterTransition?)? = defaultEnterTrans, + noinline exitTransition: (AnimatedContentTransitionScope.() -> ExitTransition?)? = defaultExitTrans, + noinline popEnterTransition: (AnimatedContentTransitionScope.() -> EnterTransition?)? = defaultPopEnterTrans, + noinline popExitTransition: (AnimatedContentTransitionScope.() -> ExitTransition?)? = defaultPopExitTrans, + noinline builder: NavGraphBuilder.() -> Unit, ) { - composable( + navigation( + startDestination = startDestination, typeMap = typeMap, deepLinks = deepLinks, enterTransition = enterTransition, exitTransition = exitTransition, popEnterTransition = popEnterTransition, popExitTransition = popExitTransition, - content = content, + builder = builder, ) } /** - * Construct a nested [NavGraph] with the default screen transitions. + * Adds the [Composable] to the [NavGraphBuilder] with the default screen transitions. */ -@Suppress("LongParameterList") -inline fun NavGraphBuilder.navigationWithDefaultTransitions( - startDestination: Any, - typeMap: Map> = emptyMap(), +@Suppress("LongParameterList", "MaxLineLength") +inline fun NavGraphBuilder.composableWithDefaultTransitions( + typeMap: Map> = emptyMap(), deepLinks: List = emptyList(), - noinline enterTransition: (AnimatedContentTransitionScope.() -> EnterTransition?)? = { - Transitions.slideInHorizontally - }, - noinline exitTransition: (AnimatedContentTransitionScope.() -> ExitTransition?)? = { - Transitions.scaleOut - }, - noinline popEnterTransition: (AnimatedContentTransitionScope.() -> EnterTransition?)? = { - Transitions.scaleIn - }, - noinline popExitTransition: (AnimatedContentTransitionScope.() -> ExitTransition?)? = { - Transitions.slideOutHorizontally - }, - noinline builder: NavGraphBuilder.() -> Unit, + noinline enterTransition: (AnimatedContentTransitionScope.() -> EnterTransition?)? = defaultEnterTrans, + noinline exitTransition: (AnimatedContentTransitionScope.() -> ExitTransition?)? = defaultExitTrans, + noinline popEnterTransition: (AnimatedContentTransitionScope.() -> EnterTransition?)? = defaultPopEnterTrans, + noinline popExitTransition: (AnimatedContentTransitionScope.() -> ExitTransition?)? = defaultPopExitTrans, + noinline content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit, ) { - navigation( - startDestination = startDestination, + composable( typeMap = typeMap, deepLinks = deepLinks, enterTransition = enterTransition, exitTransition = exitTransition, popEnterTransition = popEnterTransition, popExitTransition = popExitTransition, - builder = builder, + content = content, ) }