-
Notifications
You must be signed in to change notification settings - Fork 17
/
StackTransitionType.kt
52 lines (45 loc) · 1.53 KB
/
StackTransitionType.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
package com.github.terrakok.modo.animation
import com.github.terrakok.modo.ComposeRendererScope
import com.github.terrakok.modo.stack.StackState
/**
* Describes the transition types for a stack.
*/
enum class StackTransitionType {
/**
* Represents opening a new screen in a stack.
*/
Push,
/**
* Represents replacement of current screen in a stack to a new one.
*/
Replace,
/**
* Represents closing the current screen in a stack.
*/
Pop,
/**
* Represents no transition.
*/
Idle
}
/**
* Culculates the transition type for the given [oldState] and [newState].
* It can be used with a combination with [ScreenTransitionContent] to culculate transitionSpec based on [StackTransitionType].
*/
fun calculateStackTransitionType(oldState: StackState?, newState: StackState?): StackTransitionType =
if (oldState != null && newState != null) {
val oldStack = oldState.stack
val newStack = newState.stack
when {
oldStack.lastOrNull() == newStack.lastOrNull() || oldStack.isEmpty() -> StackTransitionType.Idle
newStack.lastOrNull() in oldStack -> StackTransitionType.Pop
oldStack.lastOrNull() in newStack -> StackTransitionType.Push
else -> StackTransitionType.Replace
}
} else {
StackTransitionType.Idle
}
/**
* @see calculateStackTransitionType
*/
fun ComposeRendererScope<StackState>.calculateStackTransitionType(): StackTransitionType = calculateStackTransitionType(oldState, newState)