From 58a4e2ef149b00a5c7e70ac6264bbaec671cc9d9 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Sat, 27 Jul 2024 12:34:34 -0700 Subject: [PATCH] Convert DiffClampAnimatedNode to Kotlin (#45716) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45716 # Changelog: [Internal] - As in the title. Reviewed By: steelrooter Differential Revision: D60283709 fbshipit-source-id: 110e9ee9deecd0c39575b94b0604ad3fa9a9b96e --- .../react/animated/DiffClampAnimatedNode.java | 66 ------------------- .../react/animated/DiffClampAnimatedNode.kt | 51 ++++++++++++++ 2 files changed, 51 insertions(+), 66 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DiffClampAnimatedNode.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DiffClampAnimatedNode.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DiffClampAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DiffClampAnimatedNode.java deleted file mode 100644 index 4ed74b8ccfa..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DiffClampAnimatedNode.java +++ /dev/null @@ -1,66 +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.infer.annotation.Nullsafe; -import com.facebook.react.bridge.JSApplicationCausedNativeException; -import com.facebook.react.bridge.ReadableMap; - -/*package*/ @Nullsafe(Nullsafe.Mode.LOCAL) -class DiffClampAnimatedNode extends ValueAnimatedNode { - private final NativeAnimatedNodesManager mNativeAnimatedNodesManager; - private final int mInputNodeTag; - private final double mMin; - private final double mMax; - - private double mLastValue; - - public DiffClampAnimatedNode( - ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager) { - mNativeAnimatedNodesManager = nativeAnimatedNodesManager; - mInputNodeTag = config.getInt("input"); - mMin = config.getDouble("min"); - mMax = config.getDouble("max"); - - mValue = mLastValue = 0; - } - - @Override - public void update() { - double value = getInputNodeValue(); - - double diff = value - mLastValue; - mLastValue = value; - mValue = Math.min(Math.max(mValue + diff, mMin), mMax); - } - - private double getInputNodeValue() { - AnimatedNode animatedNode = mNativeAnimatedNodesManager.getNodeById(mInputNodeTag); - if (animatedNode == null || !(animatedNode instanceof ValueAnimatedNode)) { - throw new JSApplicationCausedNativeException( - "Illegal node ID set as an input for Animated.DiffClamp node"); - } - - return ((ValueAnimatedNode) animatedNode).getValue(); - } - - public String prettyPrint() { - return "DiffClampAnimatedNode[" - + mTag - + "]: InputNodeTag: " - + mInputNodeTag - + " min: " - + mMin - + " max: " - + mMax - + " lastValue: " - + mLastValue - + " super: " - + super.prettyPrint(); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DiffClampAnimatedNode.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DiffClampAnimatedNode.kt new file mode 100644 index 00000000000..8a9501e47f0 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DiffClampAnimatedNode.kt @@ -0,0 +1,51 @@ +/* + * 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 +import kotlin.math.max +import kotlin.math.min + +internal class DiffClampAnimatedNode( + config: ReadableMap, + private val nativeAnimatedNodesManager: NativeAnimatedNodesManager +) : ValueAnimatedNode() { + private val inputNodeTag: Int + private val minValue: Double + private val maxValue: Double + private var lastValue: Double = 0.0 + + init { + inputNodeTag = config.getInt("input") + minValue = config.getDouble("min") + maxValue = config.getDouble("max") + mValue = lastValue + } + + override fun update() { + val value = inputNodeValue + val diff = value - lastValue + lastValue = value + mValue = min(max(mValue + diff, minValue), maxValue) + } + + private val inputNodeValue: Double + get() { + val animatedNode = nativeAnimatedNodesManager.getNodeById(inputNodeTag) + if (animatedNode == null || animatedNode !is ValueAnimatedNode) { + throw JSApplicationCausedNativeException( + "Illegal node ID set as an input for Animated.DiffClamp node") + } + return animatedNode.value + } + + override fun prettyPrint(): String = + "DiffClampAnimatedNode[$mTag]: InputNodeTag: $inputNodeTag min: $minValue " + + "max: $maxValue lastValue: $lastValue super: ${super.prettyPrint()}" +}