-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathRootAnimator.kt
More file actions
59 lines (49 loc) · 2.16 KB
/
RootAnimator.kt
File metadata and controls
59 lines (49 loc) · 2.16 KB
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
57
58
59
package com.reactnativenavigation.hierarchy.root
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.view.View
import com.reactnativenavigation.options.TransitionAnimationOptions
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController
open class RootAnimator {
open fun setRoot(appearing: ViewController<*>, disappearing: ViewController<*>?, setRoot: TransitionAnimationOptions, onAnimationEnd: ()->Unit) {
if (appearing.isDestroyed) {
onAnimationEnd()
return
}
appearing.view.visibility = View.VISIBLE
if (!setRoot.hasValue() || (!setRoot.enter.hasAnimation() && !setRoot.exit.hasAnimation())) {
onAnimationEnd()
return
}
val animationSet = createAnimator(onAnimationEnd)
val appearingAnimation = if (setRoot.enter.hasAnimation()) {
setRoot.enter.getAnimation(appearing.view)
} else null
val disappearingAnimation = if (disappearing != null && !disappearing.isDestroyed && setRoot.exit.hasAnimation()) {
setRoot.exit.getAnimation(disappearing.view)
} else null
when {
appearingAnimation != null && disappearingAnimation != null -> animationSet.playTogether(appearingAnimation, disappearingAnimation)
appearingAnimation != null -> animationSet.play(appearingAnimation)
disappearingAnimation != null -> animationSet.play(disappearingAnimation)
}
animationSet.start()
}
private fun createAnimator(onAnimationEnd: () -> Unit): AnimatorSet {
val set = AnimatorSet()
set.addListener(object : AnimatorListenerAdapter() {
private var isCancelled = false
override fun onAnimationStart(animation: Animator) {
}
override fun onAnimationCancel(animation: Animator) {
isCancelled = true
onAnimationEnd()
}
override fun onAnimationEnd(animation: Animator) {
if (!isCancelled) onAnimationEnd()
}
})
return set
}
}