From 01989033010c82de51c76cfb9150d006127d76c4 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 24 Jul 2024 12:57:09 -0700 Subject: [PATCH] Migrate ValueAnimatedNode to Kotlin Summary: ValueAnimatedNode.java -> ValueAnimatedNode.kt changelog: [internal] internal Differential Revision: D60188318 --- .../react/animated/ValueAnimatedNode.java | 66 ------------------- .../react/animated/ValueAnimatedNode.kt | 65 ++++++++++++++++++ 2 files changed, 65 insertions(+), 66 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.java deleted file mode 100644 index 35b7b99d30befd..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.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 androidx.annotation.Nullable; -import com.facebook.react.bridge.ReadableMap; - -/** - * Basic type of animated node that maps directly from {@code Animated.Value(x)} of Animated.js - * library. - */ -/*package*/ class ValueAnimatedNode extends AnimatedNode { - /*package*/ double mValue = Double.NaN; - /*package*/ double mOffset = 0; - private @Nullable AnimatedNodeValueListener mValueListener; - - public ValueAnimatedNode() { - // empty constructor that can be used by subclasses - } - - public ValueAnimatedNode(ReadableMap config) { - mValue = config.getDouble("value"); - mOffset = config.getDouble("offset"); - } - - public double getValue() { - if (Double.isNaN(mOffset + mValue)) { - this.update(); - } - return mOffset + mValue; - } - - public Object getAnimatedObject() { - return null; - } - - public void flattenOffset() { - mValue += mOffset; - mOffset = 0; - } - - public void extractOffset() { - mOffset += mValue; - mValue = 0; - } - - public void onValueUpdate() { - if (mValueListener == null) { - return; - } - mValueListener.onValueUpdate(getValue()); - } - - public void setValueListener(@Nullable AnimatedNodeValueListener listener) { - mValueListener = listener; - } - - public String prettyPrint() { - return "ValueAnimatedNode[" + mTag + "]: value: " + mValue + " offset: " + mOffset; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.kt new file mode 100644 index 00000000000000..4bdf2337cb6da0 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.kt @@ -0,0 +1,65 @@ +/* + * 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.ReadableMap + +/** + * Basic type of animated node that maps directly from {@code Animated.Value(x)} of Animated.js + * library. + */ +internal open class ValueAnimatedNode : AnimatedNode { + @JvmField internal var mValue: Double = Double.NaN + @JvmField internal var mOffset: Double = 0.0 + private var mValueListener: AnimatedNodeValueListener? = null + + public constructor() : super() { + // empty constructor that can be used by subclasses + } + + public constructor(config: ReadableMap) : super() { + mValue = config.getDouble("value") + mOffset = config.getDouble("offset") + } + + public fun getValue(): Double { + if ((mOffset + mValue).isNaN()) { + this.update() + } + return mOffset + mValue + } + + open fun getAnimatedObject(): Any? { + return null + } + + public fun flattenOffset(): Unit { + mValue += mOffset + mOffset = 0.0 + } + + public fun extractOffset(): Unit { + mOffset += mValue + mValue = 0.0 + } + + public fun onValueUpdate(): Unit { + if (mValueListener == null) { + return + } + requireNotNull(mValueListener).onValueUpdate(getValue()) + } + + public fun setValueListener(listener: AnimatedNodeValueListener?): Unit { + mValueListener = listener + } + + override public fun prettyPrint(): String { + return "ValueAnimatedNode[$mTag]: value: $mValue offset: $mOffset" + } +}