From a140b9a784e46d7458e726c5e8b9c6e5c810b27b Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Sun, 28 Jul 2024 04:17:09 -0700 Subject: [PATCH] TransformAnimatedNode Java->Kotlin (#45765) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45765 # Changelog: [Internal] - As in the title. Differential Revision: D60347833 --- .../react/animated/TransformAnimatedNode.java | 93 ------------------- .../react/animated/TransformAnimatedNode.kt | 85 +++++++++++++++++ 2 files changed, 85 insertions(+), 93 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TransformAnimatedNode.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TransformAnimatedNode.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TransformAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TransformAnimatedNode.java deleted file mode 100644 index 10ea209124718b..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TransformAnimatedNode.java +++ /dev/null @@ -1,93 +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.animated; - -import com.facebook.react.bridge.JavaOnlyArray; -import com.facebook.react.bridge.JavaOnlyMap; -import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.bridge.ReadableMap; -import java.util.ArrayList; -import java.util.List; - -/** - * Native counterpart of transform animated node (see AnimatedTransform class in - * AnimatedImplementation.js) - */ -/* package */ class TransformAnimatedNode extends AnimatedNode { - - private class TransformConfig { - public String mProperty; - } - - private class AnimatedTransformConfig extends TransformConfig { - public int mNodeTag; - } - - private class StaticTransformConfig extends TransformConfig { - public double mValue; - } - - private final NativeAnimatedNodesManager mNativeAnimatedNodesManager; - private final List mTransformConfigs; - - TransformAnimatedNode(ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager) { - ReadableArray transforms = config.getArray("transforms"); - mTransformConfigs = new ArrayList<>(transforms.size()); - for (int i = 0; i < transforms.size(); i++) { - ReadableMap transformConfigMap = transforms.getMap(i); - String property = transformConfigMap.getString("property"); - String type = transformConfigMap.getString("type"); - if (type.equals("animated")) { - AnimatedTransformConfig transformConfig = new AnimatedTransformConfig(); - transformConfig.mProperty = property; - transformConfig.mNodeTag = transformConfigMap.getInt("nodeTag"); - mTransformConfigs.add(transformConfig); - } else { - StaticTransformConfig transformConfig = new StaticTransformConfig(); - transformConfig.mProperty = property; - transformConfig.mValue = transformConfigMap.getDouble("value"); - mTransformConfigs.add(transformConfig); - } - } - mNativeAnimatedNodesManager = nativeAnimatedNodesManager; - } - - public void collectViewUpdates(JavaOnlyMap propsMap) { - List transforms = new ArrayList<>(mTransformConfigs.size()); - - for (TransformConfig transformConfig : mTransformConfigs) { - double value; - if (transformConfig instanceof AnimatedTransformConfig) { - int nodeTag = ((AnimatedTransformConfig) transformConfig).mNodeTag; - AnimatedNode node = mNativeAnimatedNodesManager.getNodeById(nodeTag); - if (node == null) { - throw new IllegalArgumentException("Mapped style node does not exist"); - } else if (node instanceof ValueAnimatedNode) { - value = ((ValueAnimatedNode) node).getValue(); - } else { - throw new IllegalArgumentException( - "Unsupported type of node used as a transform child " + "node " + node.getClass()); - } - } else { - value = ((StaticTransformConfig) transformConfig).mValue; - } - - transforms.add(JavaOnlyMap.of(transformConfig.mProperty, value)); - } - - propsMap.putArray("transform", JavaOnlyArray.from(transforms)); - } - - @Override - public String prettyPrint() { - return "TransformAnimatedNode[" - + tag - + "]: mTransformConfigs: " - + (mTransformConfigs != null ? mTransformConfigs.toString() : "null"); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TransformAnimatedNode.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TransformAnimatedNode.kt new file mode 100644 index 00000000000000..2ca8ff43f2216b --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TransformAnimatedNode.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.animated + +import com.facebook.react.bridge.JavaOnlyArray +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.bridge.ReadableMap + +/** + * Native counterpart of transform animated node (see AnimatedTransform class in + * AnimatedImplementation.js) + */ +internal class TransformAnimatedNode( + config: ReadableMap, + private val nativeAnimatedNodesManager: NativeAnimatedNodesManager +) : AnimatedNode() { + private open inner class TransformConfig { + var property: String? = null + } + + private inner class AnimatedTransformConfig : TransformConfig() { + var nodeTag = 0 + } + + private inner class StaticTransformConfig : TransformConfig() { + var value = 0.0 + } + + private val transformConfigs: MutableList + + init { + val transforms = config.getArray("transforms") + val size = transforms?.size() ?: 0 + transformConfigs = ArrayList(size) + if (transforms != null) { + for (i in 0 until size) { + val transformConfigMap = transforms.getMap(i) + val property = transformConfigMap.getString("property") + val type = transformConfigMap.getString("type") + if (type == "animated") { + val transformConfig = AnimatedTransformConfig() + transformConfig.property = property + transformConfig.nodeTag = transformConfigMap.getInt("nodeTag") + transformConfigs.add(transformConfig) + } else { + val transformConfig = StaticTransformConfig() + transformConfig.property = property + transformConfig.value = transformConfigMap.getDouble("value") + transformConfigs.add(transformConfig) + } + } + } + } + + public fun collectViewUpdates(propsMap: JavaOnlyMap) { + val transforms: MutableList = ArrayList(transformConfigs.size) + for (transformConfig in transformConfigs) { + val value = + if (transformConfig is AnimatedTransformConfig) { + val nodeTag = transformConfig.nodeTag + val node = nativeAnimatedNodesManager.getNodeById(nodeTag) + if (node == null) { + throw IllegalArgumentException("Mapped style node does not exist") + } else if (node is ValueAnimatedNode) { + node.getValue() + } else { + throw IllegalArgumentException( + "Unsupported type of node used as a transform child " + "node " + node.javaClass) + } + } else { + (transformConfig as StaticTransformConfig).value + } + transforms.add(JavaOnlyMap.of(transformConfig.property, value)) + } + propsMap.putArray("transform", JavaOnlyArray.from(transforms)) + } + + override fun prettyPrint(): String = + "TransformAnimatedNode[$tag]: transformConfigs: $transformConfigs" +}