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
This commit is contained in:
Ruslan Shestopalyuk
2024-07-27 12:34:34 -07:00
committed by Facebook GitHub Bot
parent c07ca78ac6
commit 58a4e2ef14
2 changed files with 51 additions and 66 deletions
@@ -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();
}
}
@@ -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()}"
}