From c77b0dcc1553fe276ce007649d0c91abc73b2dbf Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Mon, 29 Jul 2024 01:19:50 -0700 Subject: [PATCH] Convert StyleAnimatedNode.java->.kt (#45761) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45761 # Changelog: [Internal] - As in the title. Differential Revision: D60342089 --- .../react/animated/StyleAnimatedNode.java | 70 ------------------- .../react/animated/StyleAnimatedNode.kt | 60 ++++++++++++++++ 2 files changed, 60 insertions(+), 70 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/StyleAnimatedNode.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/StyleAnimatedNode.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/StyleAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/StyleAnimatedNode.java deleted file mode 100644 index ec87b2f2c9c813..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/StyleAnimatedNode.java +++ /dev/null @@ -1,70 +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 androidx.annotation.Nullable; -import com.facebook.react.bridge.JavaOnlyMap; -import com.facebook.react.bridge.ReadableMap; -import com.facebook.react.bridge.ReadableMapKeySetIterator; -import java.util.HashMap; -import java.util.Map; - -/** - * Native counterpart of style animated node (see AnimatedStyle class in AnimatedImplementation.js) - */ -/*package*/ class StyleAnimatedNode extends AnimatedNode { - - private final NativeAnimatedNodesManager mNativeAnimatedNodesManager; - private final Map mPropMapping; - - StyleAnimatedNode(ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager) { - ReadableMap style = config.getMap("style"); - ReadableMapKeySetIterator iter = style.keySetIterator(); - mPropMapping = new HashMap<>(); - while (iter.hasNextKey()) { - String propKey = iter.nextKey(); - int nodeIndex = style.getInt(propKey); - mPropMapping.put(propKey, nodeIndex); - } - mNativeAnimatedNodesManager = nativeAnimatedNodesManager; - } - - public void collectViewUpdates(JavaOnlyMap propsMap) { - for (Map.Entry entry : mPropMapping.entrySet()) { - @Nullable AnimatedNode node = mNativeAnimatedNodesManager.getNodeById(entry.getValue()); - if (node == null) { - throw new IllegalArgumentException("Mapped style node does not exist"); - } else if (node instanceof TransformAnimatedNode) { - ((TransformAnimatedNode) node).collectViewUpdates(propsMap); - } else if (node instanceof ValueAnimatedNode) { - Object animatedObject = ((ValueAnimatedNode) node).getAnimatedObject(); - if (animatedObject instanceof Integer) { - propsMap.putInt(entry.getKey(), (Integer) animatedObject); - } else if (animatedObject instanceof String) { - propsMap.putString(entry.getKey(), (String) animatedObject); - } else { - propsMap.putDouble(entry.getKey(), ((ValueAnimatedNode) node).getValue()); - } - } else if (node instanceof ColorAnimatedNode) { - propsMap.putInt(entry.getKey(), ((ColorAnimatedNode) node).getColor()); - } else if (node instanceof ObjectAnimatedNode) { - ((ObjectAnimatedNode) node).collectViewUpdates(entry.getKey(), propsMap); - } else { - throw new IllegalArgumentException( - "Unsupported type of node used in property node " + node.getClass()); - } - } - } - - public String prettyPrint() { - return "StyleAnimatedNode[" - + tag - + "] mPropMapping: " - + (mPropMapping != null ? mPropMapping.toString() : "null"); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/StyleAnimatedNode.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/StyleAnimatedNode.kt new file mode 100644 index 00000000000000..9e36be81c40ae6 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/StyleAnimatedNode.kt @@ -0,0 +1,60 @@ +/* + * 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.JavaOnlyMap +import com.facebook.react.bridge.ReadableMap + +/** + * Native counterpart of style animated node (see AnimatedStyle class in AnimatedImplementation.js) + */ +internal class StyleAnimatedNode( + config: ReadableMap, + private val nativeAnimatedNodesManager: NativeAnimatedNodesManager +) : AnimatedNode() { + private val propMapping: MutableMap + + init { + val style = config.getMap("style") + val iter = style?.keySetIterator() + propMapping = HashMap() + while (iter != null && iter.hasNextKey()) { + val propKey = iter.nextKey() + val nodeIndex = style.getInt(propKey) + propMapping[propKey] = nodeIndex + } + } + + public fun collectViewUpdates(propsMap: JavaOnlyMap) { + for ((key, value) in propMapping) { + val node = nativeAnimatedNodesManager.getNodeById(value) + requireNotNull(node) { "Mapped style node does not exist" } + if (node is TransformAnimatedNode) { + node.collectViewUpdates(propsMap) + } else if (node is ValueAnimatedNode) { + val animatedObject = node.getAnimatedObject() + if (animatedObject is Int) { + propsMap.putInt(key, animatedObject) + } else if (animatedObject is String) { + propsMap.putString(key, animatedObject) + } else { + propsMap.putDouble(key, node.getValue()) + } + } else if (node is ColorAnimatedNode) { + propsMap.putInt(key, node.color) + } else if (node is ObjectAnimatedNode) { + node.collectViewUpdates(key, propsMap) + } else { + throw IllegalArgumentException( + "Unsupported type of node used in property node ${node.javaClass}") + } + } + } + + override fun prettyPrint(): String = "StyleAnimatedNode[$tag] mPropMapping: $propMapping" +}