Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 64 additions & 44 deletions app/src/main/java/to/bitkit/ui/utils/Transitions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<NavBackStackEntry>.() -> EnterTransition?) = {
slideInHorizontally(
initialOffsetX = { fullWidth -> fullWidth },
animationSpec = tween(300, easing = FastOutSlowInEasing)
)
}

val defaultExitTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?) = {
slideOutHorizontally(
targetOffsetX = { fullWidth -> -fullWidth / 3 },
animationSpec = tween(300, easing = FastOutSlowInEasing)
) + fadeOut(
animationSpec = tween(300, easing = FastOutSlowInEasing),
targetAlpha = 0.8f
)
}

val defaultPopEnterTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?) = {
slideInHorizontally(
initialOffsetX = { fullWidth -> -fullWidth / 3 },
animationSpec = tween(300, easing = FastOutSlowInEasing)
) + fadeIn(
animationSpec = tween(300, easing = FastOutSlowInEasing),
initialAlpha = 0.8f
)
}

val defaultPopExitTrans: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> 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 <reified T : Any> NavGraphBuilder.composableWithDefaultTransitions(
typeMap: Map<KType, NavType<*>> = emptyMap(),
@Suppress("LongParameterList", "MaxLineLength")
inline fun <reified T : Any> NavGraphBuilder.navigationWithDefaultTransitions(
startDestination: Any,
typeMap: Map<KType, @JvmSuppressWildcards NavType<*>> = emptyMap(),
deepLinks: List<NavDeepLink> = emptyList(),
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
Transitions.slideInHorizontally
},
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
Transitions.scaleOut
},
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
Transitions.scaleIn
},
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
Transitions.slideOutHorizontally
},
noinline content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit,
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = defaultEnterTrans,
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = defaultExitTrans,
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = defaultPopEnterTrans,
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = defaultPopExitTrans,
noinline builder: NavGraphBuilder.() -> Unit,
) {
composable<T>(
navigation<T>(
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 <reified T : Any> NavGraphBuilder.navigationWithDefaultTransitions(
startDestination: Any,
typeMap: Map<KType, @JvmSuppressWildcards NavType<*>> = emptyMap(),
@Suppress("LongParameterList", "MaxLineLength")
inline fun <reified T : Any> NavGraphBuilder.composableWithDefaultTransitions(
typeMap: Map<KType, NavType<*>> = emptyMap(),
deepLinks: List<NavDeepLink> = emptyList(),
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
Transitions.slideInHorizontally
},
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
Transitions.scaleOut
},
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = {
Transitions.scaleIn
},
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = {
Transitions.slideOutHorizontally
},
noinline builder: NavGraphBuilder.() -> Unit,
noinline enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = defaultEnterTrans,
noinline exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = defaultExitTrans,
noinline popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? = defaultPopEnterTrans,
noinline popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? = defaultPopExitTrans,
noinline content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit,
) {
navigation<T>(
startDestination = startDestination,
composable<T>(
typeMap = typeMap,
deepLinks = deepLinks,
enterTransition = enterTransition,
exitTransition = exitTransition,
popEnterTransition = popEnterTransition,
popExitTransition = popExitTransition,
builder = builder,
content = content,
)
}