From 6ea843ea973072b0f5e149073871ef1c3615fa7d Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Tue, 30 Jul 2024 02:37:50 -0700 Subject: [PATCH] Kotlinify SubtractionAnimatedNode Differential Revision: D60347765 --- .../animated/SubtractionAnimatedNode.java | 60 ------------------- .../react/animated/SubtractionAnimatedNode.kt | 52 ++++++++++++++++ 2 files changed, 52 insertions(+), 60 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SubtractionAnimatedNode.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SubtractionAnimatedNode.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SubtractionAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SubtractionAnimatedNode.java deleted file mode 100644 index f3b6ac8cab8269..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SubtractionAnimatedNode.java +++ /dev/null @@ -1,60 +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 that plays a role of value aggregator. It takes two or more value nodes as an input - * and outputs a difference of values outputted by those nodes. - */ -/*package*/ class SubtractionAnimatedNode extends ValueAnimatedNode { - - private final NativeAnimatedNodesManager mNativeAnimatedNodesManager; - private final int[] mInputNodes; - - public SubtractionAnimatedNode( - 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; - } else { - nodeValue -= value; - } - } else { - throw new JSApplicationCausedNativeException( - "Illegal node ID set as an input for Animated.subtract node"); - } - } - } - - @Override - public String prettyPrint() { - return "SubtractionAnimatedNode[" - + 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/SubtractionAnimatedNode.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SubtractionAnimatedNode.kt new file mode 100644 index 00000000000000..a87eaacc193eb0 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SubtractionAnimatedNode.kt @@ -0,0 +1,52 @@ +/* + * 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 that plays a role of value aggregator. It takes two or more value nodes as an input + * and outputs a difference of values outputted by those nodes. + */ +internal class SubtractionAnimatedNode( + 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() { + for (i in inputNodes.indices) { + val animatedNode = nativeAnimatedNodesManager.getNodeById(inputNodes[i]) + if (animatedNode != null && animatedNode is ValueAnimatedNode) { + val value = animatedNode.getValue() + if (i == 0) { + nodeValue = value + } else { + nodeValue -= value + } + } else { + throw JSApplicationCausedNativeException( + "Illegal node ID set as an input for Animated.subtract node") + } + } + } + + override fun prettyPrint(): String = + "SubtractionAnimatedNode[$tag]: input nodes: $inputNodes - super: ${super.prettyPrint()}" +}