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
This commit is contained in:
Mateo Guzmán authored and Facebook GitHub Bot committed 2025-03-12 07:56:06 -07:00
1 parent 2d7356193f
commit 6c5a9e8257
2 files changed
+85 -94

No files matched your search

@@ -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");
}
}
@@ -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)
}
}
}