-
Notifications
You must be signed in to change notification settings - Fork 17
/
ScreenTransitions.kt
56 lines (52 loc) · 2.18 KB
/
ScreenTransitions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.github.terrakok.modo.animation
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ContentTransform
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.togetherWith
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.ui.Modifier
import com.github.terrakok.modo.ComposeRendererScope
import com.github.terrakok.modo.LocalTransitionCompleteChannel
import com.github.terrakok.modo.SaveableContent
import com.github.terrakok.modo.Screen
val displayingScreens = mutableStateMapOf<Screen, Unit>()
typealias ScreenTransitionContent = @Composable AnimatedVisibilityScope.(Screen) -> Unit
/**
* The way to animate [Screen]'s changing (transition).
*/
@Suppress("MagicNumber")
@ExperimentalAnimationApi
@Composable
fun ComposeRendererScope<*>.ScreenTransition(
modifier: Modifier = Modifier,
screenModifier: Modifier = Modifier,
transitionSpec: AnimatedContentTransitionScope<Screen>.() -> ContentTransform = {
fadeIn(animationSpec = tween(220, delayMillis = 90)) +
scaleIn(initialScale = 0.92f, animationSpec = tween(220, delayMillis = 90)) togetherWith
fadeOut(animationSpec = tween(90))
},
content: ScreenTransitionContent = { it.SaveableContent(screenModifier) }
) {
val transition = updateTransition(targetState = screen, label = "ScreenTransition")
transition.AnimatedContent(
transitionSpec = transitionSpec,
modifier = modifier,
contentKey = { it.screenKey },
content = content
)
if (transition.currentState == transition.targetState) {
val channel = LocalTransitionCompleteChannel.current
LaunchedEffect(Unit) {
channel.trySend(Unit)
}
}
}