Skip to content

Commit 49bbc28

Browse files
authored
refactor(Android, FormSheet): Align animators implementation to use ValueAnimator (#3922)
## Description After moving everything to a single class, I think that it's reasonable to have a consistent system for all animations, therefore I'm aligning content size change animations with entering/exiting animations. ## Changes - Replace `ViewPropertyAnimators` with `ValueAnimators` ## Before & after - visual documentation N/A - refactor ## Test plan Regression testing on Test2560 & Test3336 ## Checklist - [ ] Included code example that can be used to test this change. - [ ] For visual changes, included screenshots / GIFs / recordings documenting the change. - [ ] For API changes, updated relevant public types. - [ ] Ensured that CI passes
1 parent 83be835 commit 49bbc28

1 file changed

Lines changed: 47 additions & 19 deletions

File tree

android/src/main/java/com/swmansion/rnscreens/bottomsheet/SheetAnimationCoordinator.kt

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal class SheetAnimationCoordinator(
2424
"[RNScreens] Screen has been destroyed and shouldn't be the subject of any animations"
2525
}
2626
private var isSheetAnimationInProgress: Boolean = false
27+
private var currentContentAnimator: ValueAnimator? = null
2728

2829
private var lastKeyboardBottomOffset: Int = 0
2930

@@ -136,15 +137,25 @@ internal class SheetAnimationCoordinator(
136137
visibleDelta: Float,
137138
) {
138139
screen.translationY += visibleDelta
139-
screen
140-
.animate()
141-
.translationY(currentTranslationY)
142-
.withStartAction {
143-
behavior.updateMetrics(clampedNewHeight)
144-
screen.layoutBottomSheetAtHeight(clampedNewHeight)
145-
}.withEndAction {
146-
screen.finalizeBottomSheetLayoutUpdates()
147-
}.start()
140+
cancelCurrentContentAnimation()
141+
currentContentAnimator =
142+
ValueAnimator.ofFloat(screen.translationY, currentTranslationY).apply {
143+
addListener(
144+
object : AnimatorListenerAdapter() {
145+
override fun onAnimationStart(animation: Animator) {
146+
behavior.updateMetrics(clampedNewHeight)
147+
screen.layoutBottomSheetAtHeight(clampedNewHeight)
148+
}
149+
150+
override fun onAnimationEnd(animation: Animator) {
151+
currentContentAnimator = null
152+
screen.finalizeBottomSheetLayoutUpdates()
153+
}
154+
},
155+
)
156+
addUpdateListener { screen.translationY = it.animatedValue as Float }
157+
start()
158+
}
148159
}
149160

150161
/*
@@ -174,16 +185,26 @@ internal class SheetAnimationCoordinator(
174185
visibleDelta: Float,
175186
) {
176187
val targetTranslationY = currentTranslationY - visibleDelta
177-
screen
178-
.animate()
179-
.translationY(targetTranslationY)
180-
.withStartAction {
181-
behavior.updateMetrics(clampedNewHeight)
182-
}.withEndAction {
183-
screen.layoutBottomSheetAtHeight(clampedNewHeight)
184-
screen.translationY = currentTranslationY
185-
screen.finalizeBottomSheetLayoutUpdates()
186-
}.start()
188+
cancelCurrentContentAnimation()
189+
currentContentAnimator =
190+
ValueAnimator.ofFloat(currentTranslationY, targetTranslationY).apply {
191+
addListener(
192+
object : AnimatorListenerAdapter() {
193+
override fun onAnimationStart(animation: Animator) {
194+
behavior.updateMetrics(clampedNewHeight)
195+
}
196+
197+
override fun onAnimationEnd(animation: Animator) {
198+
currentContentAnimator = null
199+
screen.layoutBottomSheetAtHeight(clampedNewHeight)
200+
screen.translationY = currentTranslationY
201+
screen.finalizeBottomSheetLayoutUpdates()
202+
}
203+
},
204+
)
205+
addUpdateListener { screen.translationY = it.animatedValue as Float }
206+
start()
207+
}
187208
}
188209

189210
internal fun handleKeyboardInsetsProgress(insets: WindowInsetsCompat) {
@@ -273,6 +294,13 @@ internal class SheetAnimationCoordinator(
273294
}
274295
}
275296

297+
private fun cancelCurrentContentAnimation() {
298+
currentContentAnimator?.removeAllListeners()
299+
currentContentAnimator?.removeAllUpdateListeners()
300+
currentContentAnimator?.cancel()
301+
currentContentAnimator = null
302+
}
303+
276304
private fun Screen.layoutBottomSheetAtHeight(height: Int) = layout(left, bottom - height, right, bottom)
277305

278306
private fun Screen.finalizeBottomSheetLayoutUpdates() {

0 commit comments

Comments
 (0)