From d9caab6999bea925835c22df55db9395b50670b2 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Tue, 30 Jul 2024 02:55:25 -0700 Subject: [PATCH] Migrate DivisionAnimatedNode, Java->Kotlin (#45762) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45762 # Changelog: [Internal] - As in the title. Reviewed By: cortinico Differential Revision: D60341635 --- .../react/animated/DivisionAnimatedNode.java | 64 ------------------- .../react/animated/DivisionAnimatedNode.kt | 55 ++++++++++++++++ 2 files changed, 55 insertions(+), 64 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DivisionAnimatedNode.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DivisionAnimatedNode.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DivisionAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DivisionAnimatedNode.java deleted file mode 100644 index 1aa5d903c4eeeb..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DivisionAnimatedNode.java +++ /dev/null @@ -1,64 +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.JSApplicationCausedNativeException; -import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.bridge.ReadableMap; - -/** - * Animated node which takes two or more value node as an input and outputs an in-order division of - * their values. - */ -/*package*/ class DivisionAnimatedNode extends ValueAnimatedNode { - - private final NativeAnimatedNodesManager mNativeAnimatedNodesManager; - private final int[] mInputNodes; - - public DivisionAnimatedNode( - ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager) { - mNativeAnimatedNodesManager = nativeAnimatedNodesManager; - ReadableArray inputNodes = config.getArray("input"); - mInputNodes = new int[inputNodes.size()]; - for (int i = 0; i < mInputNodes.length; i++) { - mInputNodes[i] = inputNodes.getInt(i); - } - } - - @Override - public void update() { - for (int i = 0; i < mInputNodes.length; i++) { - AnimatedNode animatedNode = mNativeAnimatedNodesManager.getNodeById(mInputNodes[i]); - if (animatedNode != null && animatedNode instanceof ValueAnimatedNode) { - double value = ((ValueAnimatedNode) animatedNode).getValue(); - if (i == 0) { - nodeValue = value; - continue; - } - if (value == 0) { - throw new JSApplicationCausedNativeException( - "Detected a division by zero in Animated.divide node with Animated ID " + tag); - } - nodeValue /= value; - } else { - throw new JSApplicationCausedNativeException( - "Illegal node ID set as an input for Animated.divide node with Animated ID " + tag); - } - } - } - - @Override - public String prettyPrint() { - return "DivisionAnimatedNode[" - + tag - + "]: input nodes: " - + (mInputNodes != null ? mInputNodes.toString() : "null") - + " - super: " - + super.prettyPrint(); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DivisionAnimatedNode.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DivisionAnimatedNode.kt new file mode 100644 index 00000000000000..b2625bf0f00e21 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DivisionAnimatedNode.kt @@ -0,0 +1,55 @@ +/* + * 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.JSApplicationCausedNativeException +import com.facebook.react.bridge.ReadableMap + +/** + * Animated node which takes two or more value node as an input and outputs an in-order division of + * their values. + */ +internal class DivisionAnimatedNode( + config: ReadableMap, + private val nativeAnimatedNodesManager: NativeAnimatedNodesManager +) : ValueAnimatedNode() { + private val inputNodes: IntArray + + init { + val input = config.getArray("input") + inputNodes = + if (input == null) { + IntArray(0) + } else { + IntArray(input.size()) { i -> input.getInt(i) } + } + } + + override fun update() { + inputNodes.forEachIndexed { i, inputNode -> + val animatedNode = nativeAnimatedNodesManager.getNodeById(inputNode) + if (animatedNode != null && animatedNode is ValueAnimatedNode) { + val v = animatedNode.nodeValue + if (i == 0) { + nodeValue = v + } else if (v == 0.0) { + throw JSApplicationCausedNativeException( + "Detected a division by zero in Animated.divide node with Animated ID $tag") + } else { + nodeValue /= v + } + } else { + throw JSApplicationCausedNativeException( + "Illegal node ID set as an input for Animated.divide node with Animated ID $tag") + } + } + } + + override fun prettyPrint(): String = + "DivisionAnimatedNode[$tag]: input nodes: $inputNodes - super: ${super.prettyPrint()}" +}