-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathBaseViewAppearanceAnimator.kt
More file actions
175 lines (146 loc) · 6.26 KB
/
BaseViewAppearanceAnimator.kt
File metadata and controls
175 lines (146 loc) · 6.26 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.reactnativenavigation.views.animations
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.view.View
import androidx.annotation.CallSuper
import androidx.annotation.VisibleForTesting
import androidx.core.animation.doOnEnd
import com.reactnativenavigation.options.AnimationOptions
import com.reactnativenavigation.options.animations.ViewAnimationOptions
import com.reactnativenavigation.options.params.Bool
import com.reactnativenavigation.utils.resetViewProperties
open class BaseViewAppearanceAnimator<T : View>(
private val hideDirection: HideDirection,
view: T? = null,
private val defaultAnimatorCreator: ViewAnimatorCreator = DefaultViewAnimatorCreator()
) {
enum class HideDirection { Up, Down }
private enum class AnimationState { Idle, AnimatingEnter, AnimatingExit }
protected lateinit var view: T
@VisibleForTesting
var showAnimator: Animator = AnimatorSet()
private set(value) {
field = value
field.addListener(showAnimatorListener)
field.doOnEnd { onShowAnimationEnd() }
}
@VisibleForTesting
var hideAnimator: Animator = AnimatorSet()
set(value) {
field = value
field.addListener(hideAnimatorListener)
field.doOnEnd { onHideAnimationEnd() }
}
private val showAnimatorListener = AnimatorListener(AnimationState.AnimatingEnter, View.VISIBLE)
private val hideAnimatorListener = AnimatorListener(AnimationState.AnimatingExit, View.GONE)
private inner class AnimatorListener(private val startState: AnimationState, private val endVisibility: Int) : AnimatorListenerAdapter() {
var isCancelled = false
override fun onAnimationStart(animation: Animator) {
view.resetViewProperties()
view.visibility = View.VISIBLE
animationState = startState
}
override fun onAnimationCancel(animation: Animator) {
isCancelled = true
}
override fun onAnimationEnd(animation: Animator) {
if (!isCancelled) {
animationState = AnimationState.Idle
view.visibility = endVisibility
}
}
}
private var animationState = AnimationState.Idle
private val isOrWillBeVisible: Boolean
get() = isFullyVisible || animationState == AnimationState.AnimatingEnter
private val isFullyVisible: Boolean
get() = view.visibility == View.VISIBLE && animationState == AnimationState.Idle
val isOrWillBeHidden: Boolean
get() = isFullyHidden || animationState == AnimationState.AnimatingExit
private val isFullyHidden: Boolean
get() = view.visibility == View.GONE && animationState == AnimationState.Idle
init {
view?.let { this.view = it }
}
@CallSuper
open fun bindView(view: T) {
this.view = view
}
open fun onShowAnimationEnd() = Unit
open fun onHideAnimationEnd() = Unit
fun isAnimatingHide() = hideAnimator.isRunning
fun isAnimatingShow() = showAnimator.isRunning
fun getPushAnimation(animationOpts: ViewAnimationOptions, visible: Bool, additionalDy: Float = 0f): Animator? {
if (isOrWillBeVisible && visible.isFalse) {
showAnimator.cancel()
hideAnimator = animationOpts.exit.getAnimation(view, defaultAnimatorCreator.getHideAnimator(view, hideDirection, additionalDy))
return hideAnimator
}
if (isOrWillBeHidden && visible.isTrueOrUndefined) {
hideAnimator.cancel()
showAnimator = animationOpts.enter.getAnimation(view, defaultAnimatorCreator.getShowAnimator(view, hideDirection, additionalDy))
return showAnimator
}
return null
}
fun getPopAnimation(animation: ViewAnimationOptions, visible: Bool, additionalDy: Float = 0f): Animator? {
if (isOrWillBeVisible && visible.isFalse) {
showAnimator.cancel()
hideAnimator = animation.exit.getAnimation(view, defaultAnimatorCreator.getHideAnimator(view, hideDirection, additionalDy))
return hideAnimator
}
if (isOrWillBeHidden && visible.isTrueOrUndefined) {
hideAnimator.cancel()
showAnimator = animation.enter.getAnimation(view, defaultAnimatorCreator.getShowAnimator(view, hideDirection, additionalDy))
return showAnimator
}
return null
}
fun getSetStackRootAnimation(animation: ViewAnimationOptions, visible: Bool, additionalDy: Float = 0f): Animator? {
if (isOrWillBeVisible && visible.isFalse) {
showAnimator.cancel()
hideAnimator = animation.exit.getAnimation(view, defaultAnimatorCreator.getHideAnimator(view, hideDirection, additionalDy))
return hideAnimator
}
if (isOrWillBeHidden && visible.isTrueOrUndefined) {
hideAnimator.cancel()
showAnimator = animation.enter.getAnimation(view, defaultAnimatorCreator.getShowAnimator(view, hideDirection, additionalDy))
return showAnimator
}
return null
}
fun show(options: AnimationOptions = AnimationOptions(), translationStartDy: Float = 0f) {
if (isOrWillBeVisible) return
showAnimator = if (options.hasValue()) {
options.setValueDy(
View.TRANSLATION_Y,
-translationStartDy,
0f
)
options.getAnimation(view)
} else {
defaultAnimatorCreator.getShowAnimator(view, hideDirection, translationStartDy)
}
hideAnimator.cancel()
showAnimator.start()
}
open fun hide(
options: AnimationOptions = AnimationOptions(),
additionalDy: Float = 0f,
onAnimationEnd: Runnable? = null
) {
if (isOrWillBeHidden) return
hideAnimator = if (options.hasValue()) {
options.setValueDy(View.TRANSLATION_Y, 0f, -additionalDy)
options.getAnimation(view)
} else {
defaultAnimatorCreator.getHideAnimator(view, hideDirection, additionalDy)
}
showAnimator.cancel()
hideAnimator.apply {
doOnEnd { onAnimationEnd?.run() }
start()
}
}
}