From 6c5a9e8257370a3221ca245245f77e937faec656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateo=20Guzm=C3=A1n?= Date: Wed, 12 Mar 2025 07:56:06 -0700 Subject: [PATCH] Migrate `BaseLayoutAnimation` to Kotlin (#49962) Summary: Migrate com.facebook.react.uimanager.layoutanimation.BaseLayoutAnimation to Kotlin. ## Changelog: [INTERNAL] - Migrate com.facebook.react.uimanager.layoutanimation.BaseLayoutAnimation to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/49962 Test Plan: ```bash yarn test-android yarn android ``` Reviewed By: cortinico Differential Revision: D71031224 Pulled By: javache fbshipit-source-id: c74d72b85dcc37a48a2ebb795a29ff317511d5ed --- .../layoutanimation/BaseLayoutAnimation.java | 94 ------------------- .../layoutanimation/BaseLayoutAnimation.kt | 85 +++++++++++++++++ 2 files changed, 85 insertions(+), 94 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/BaseLayoutAnimation.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/BaseLayoutAnimation.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/BaseLayoutAnimation.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/BaseLayoutAnimation.java deleted file mode 100644 index 0667f1bb753..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/BaseLayoutAnimation.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.uimanager.layoutanimation; - -import android.view.View; -import android.view.animation.Animation; -import android.view.animation.ScaleAnimation; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.react.common.annotations.internal.LegacyArchitecture; -import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel; -import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger; -import com.facebook.react.uimanager.IllegalViewOperationException; - -/** Class responsible for default layout animation, i.e animation of view creation and deletion. */ -@LegacyArchitecture -/* package */ @Nullsafe(Nullsafe.Mode.LOCAL) -abstract class BaseLayoutAnimation extends AbstractLayoutAnimation { - static { - LegacyArchitectureLogger.assertWhenLegacyArchitectureMinifyingEnabled( - "BaseLayoutAnimation", LegacyArchitectureLogLevel.WARNING); - } - - abstract boolean isReverse(); - - @Override - boolean isValid() { - return mDurationMs > 0 && mAnimatedProperty != null; - } - - @Override - Animation createAnimationImpl(View view, int x, int y, int width, int height) { - if (mAnimatedProperty != null) { - switch (mAnimatedProperty) { - case OPACITY: - { - float fromValue = isReverse() ? view.getAlpha() : 0.0f; - float toValue = isReverse() ? 0.0f : view.getAlpha(); - return new OpacityAnimation(view, fromValue, toValue); - } - case SCALE_XY: - { - float fromValue = isReverse() ? 1.0f : 0.0f; - float toValue = isReverse() ? 0.0f : 1.0f; - return new ScaleAnimation( - fromValue, - toValue, - fromValue, - toValue, - Animation.RELATIVE_TO_SELF, - .5f, - Animation.RELATIVE_TO_SELF, - .5f); - } - case SCALE_X: - { - float fromValue = isReverse() ? 1.0f : 0.0f; - float toValue = isReverse() ? 0.0f : 1.0f; - return new ScaleAnimation( - fromValue, - toValue, - 1f, - 1f, - Animation.RELATIVE_TO_SELF, - .5f, - Animation.RELATIVE_TO_SELF, - 0f); - } - case SCALE_Y: - { - float fromValue = isReverse() ? 1.0f : 0.0f; - float toValue = isReverse() ? 0.0f : 1.0f; - return new ScaleAnimation( - 1f, - 1f, - fromValue, - toValue, - Animation.RELATIVE_TO_SELF, - 0f, - Animation.RELATIVE_TO_SELF, - .5f); - } - default: - throw new IllegalViewOperationException( - "Missing animation for property : " + mAnimatedProperty); - } - } - throw new IllegalViewOperationException("Missing animated property from animation config"); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/BaseLayoutAnimation.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/BaseLayoutAnimation.kt new file mode 100644 index 00000000000..ff4a8107f38 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/layoutanimation/BaseLayoutAnimation.kt @@ -0,0 +1,85 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.uimanager.layoutanimation + +import android.view.View +import android.view.animation.Animation +import android.view.animation.ScaleAnimation +import com.facebook.react.common.annotations.internal.LegacyArchitecture +import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel +import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger +import com.facebook.react.uimanager.IllegalViewOperationException + +/** Class responsible for default layout animation, i.e animation of view creation and deletion. */ +@LegacyArchitecture +internal abstract class BaseLayoutAnimation : AbstractLayoutAnimation() { + abstract fun isReverse(): Boolean + + override fun isValid(): Boolean = mDurationMs > 0 && mAnimatedProperty != null + + override fun createAnimationImpl(view: View, x: Int, y: Int, width: Int, height: Int): Animation { + mAnimatedProperty?.let { + return when (it) { + AnimatedPropertyType.OPACITY -> { + val fromValue = if (isReverse()) view.alpha else 0.0f + val toValue = if (isReverse()) 0.0f else view.alpha + OpacityAnimation(view, fromValue, toValue) + } + + AnimatedPropertyType.SCALE_XY -> { + val fromValue = if (isReverse()) 1.0f else 0.0f + val toValue = if (isReverse()) 0.0f else 1.0f + ScaleAnimation( + fromValue, + toValue, + fromValue, + toValue, + Animation.RELATIVE_TO_SELF, + .5f, + Animation.RELATIVE_TO_SELF, + .5f) + } + + AnimatedPropertyType.SCALE_X -> { + val fromValue = if (isReverse()) 1.0f else 0.0f + val toValue = if (isReverse()) 0.0f else 1.0f + ScaleAnimation( + fromValue, + toValue, + 1f, + 1f, + Animation.RELATIVE_TO_SELF, + .5f, + Animation.RELATIVE_TO_SELF, + 0f) + } + + AnimatedPropertyType.SCALE_Y -> { + val fromValue = if (isReverse()) 1.0f else 0.0f + val toValue = if (isReverse()) 0.0f else 1.0f + ScaleAnimation( + 1f, + 1f, + fromValue, + toValue, + Animation.RELATIVE_TO_SELF, + 0f, + Animation.RELATIVE_TO_SELF, + .5f) + } + } + } ?: throw IllegalViewOperationException("Missing animated property from animation config") + } + + private companion object { + init { + LegacyArchitectureLogger.assertWhenLegacyArchitectureMinifyingEnabled( + "BaseLayoutAnimation", LegacyArchitectureLogLevel.WARNING) + } + } +}