diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AdditionAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AdditionAnimatedNode.java index 4285248bbc1576..a0eba369227172 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AdditionAnimatedNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AdditionAnimatedNode.java @@ -32,11 +32,11 @@ public AdditionAnimatedNode( @Override public void update() { - mValue = 0; + nodeValue = 0; for (int i = 0; i < mInputNodes.length; i++) { AnimatedNode animatedNode = mNativeAnimatedNodesManager.getNodeById(mInputNodes[i]); if (animatedNode != null && animatedNode instanceof ValueAnimatedNode) { - mValue += ((ValueAnimatedNode) animatedNode).getValue(); + nodeValue += ((ValueAnimatedNode) animatedNode).getValue(); } else { throw new JSApplicationCausedNativeException( "Illegal node ID set as an input for Animated.Add node"); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ColorAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ColorAnimatedNode.java index 441bc194509651..ede7f5a6a8d476 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ColorAnimatedNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ColorAnimatedNode.java @@ -94,10 +94,10 @@ private void tryApplyNativeColor() { ValueAnimatedNode bNode = (ValueAnimatedNode) mNativeAnimatedNodesManager.getNodeById(mBNodeId); ValueAnimatedNode aNode = (ValueAnimatedNode) mNativeAnimatedNodesManager.getNodeById(mANodeId); - rNode.mValue = Color.red(color); - gNode.mValue = Color.green(color); - bNode.mValue = Color.blue(color); - aNode.mValue = Color.alpha(color) / 255.0; + rNode.nodeValue = Color.red(color); + gNode.nodeValue = Color.green(color); + bNode.nodeValue = Color.blue(color); + aNode.nodeValue = Color.alpha(color) / 255.0; mNativeColorApplied = true; } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DecayAnimation.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DecayAnimation.java index 97463913dae684..f4d07d8e4c4d5d 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DecayAnimation.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/DecayAnimation.java @@ -49,11 +49,11 @@ public void runAnimationStep(long frameTimeNanos) { // since this is the first animation step, consider the start to be on the previous frame mStartFrameTimeMillis = frameTimeMillis - 16; if (mFromValue == mLastValue) { // first iteration, assign mFromValue based on mAnimatedValue - mFromValue = mAnimatedValue.mValue; + mFromValue = mAnimatedValue.nodeValue; } else { // not the first iteration, reset mAnimatedValue based on mFromValue - mAnimatedValue.mValue = mFromValue; + mAnimatedValue.nodeValue = mFromValue; } - mLastValue = mAnimatedValue.mValue; + mLastValue = mAnimatedValue.nodeValue; } final double value = @@ -74,6 +74,6 @@ public void runAnimationStep(long frameTimeNanos) { } mLastValue = value; - mAnimatedValue.mValue = value; + mAnimatedValue.nodeValue = value; } } 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 index c2eefefab5375b..fbe312e4859370 100644 --- 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 @@ -25,14 +25,14 @@ internal class DiffClampAnimatedNode( inputNodeTag = config.getInt("input") minValue = config.getDouble("min") maxValue = config.getDouble("max") - mValue = lastValue + nodeValue = lastValue } override fun update() { val value = inputNodeValue val diff = value - lastValue lastValue = value - mValue = min(max(mValue + diff, minValue), maxValue) + nodeValue = min(max(nodeValue + diff, minValue), maxValue) } private val inputNodeValue: Double @@ -42,7 +42,7 @@ internal class DiffClampAnimatedNode( throw JSApplicationCausedNativeException( "Illegal node ID set as an input for Animated.DiffClamp node") } - return animatedNode.value + return animatedNode.getValue() } override fun prettyPrint(): String = 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 index 0dbb3feb04269f..1aa5d903c4eeeb 100644 --- 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 @@ -37,14 +37,14 @@ public void update() { if (animatedNode != null && animatedNode instanceof ValueAnimatedNode) { double value = ((ValueAnimatedNode) animatedNode).getValue(); if (i == 0) { - mValue = value; + nodeValue = value; continue; } if (value == 0) { throw new JSApplicationCausedNativeException( "Detected a division by zero in Animated.divide node with Animated ID " + tag); } - mValue /= value; + nodeValue /= value; } else { throw new JSApplicationCausedNativeException( "Illegal node ID set as an input for Animated.divide node with Animated ID " + tag); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.java index 91dd128c243fc7..c1a4cfe48bb256 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.java @@ -107,10 +107,10 @@ public void receiveEvent( String lastKey = mEventPath.get(mEventPath.size() - 1); if (currMap != null) { - mValueNode.mValue = currMap.getDouble(lastKey); + mValueNode.nodeValue = currMap.getDouble(lastKey); } else { int lastIndex = Integer.parseInt(lastKey); - mValueNode.mValue = currArray.getDouble(lastIndex); + mValueNode.nodeValue = currArray.getDouble(lastIndex); } } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/FrameBasedAnimationDriver.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/FrameBasedAnimationDriver.java index 4900e85f89aecf..b13a9741c4b0c5 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/FrameBasedAnimationDriver.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/FrameBasedAnimationDriver.java @@ -68,7 +68,7 @@ public void runAnimationStep(long frameTimeNanos) { mStartFrameTimeNanos = frameTimeNanos; if (mCurrentLoop == 1) { // initiate start value when animation runs for the first time - mFromValue = mAnimatedValue.mValue; + mFromValue = mAnimatedValue.nodeValue; } } @@ -109,6 +109,6 @@ public void runAnimationStep(long frameTimeNanos) { } else { nextValue = mFromValue + mFrames[frameIndex] * (mToValue - mFromValue); } - mAnimatedValue.mValue = nextValue; + mAnimatedValue.nodeValue = nextValue; } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/InterpolationAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/InterpolationAnimatedNode.java index 161163bc2bcf56..4307de260166ef 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/InterpolationAnimatedNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/InterpolationAnimatedNode.java @@ -279,7 +279,7 @@ public void update() { double value = mParent.getValue(); switch (mOutputType) { case Number: - mValue = + nodeValue = interpolate( value, mInputRange, (double[]) mOutputRange, mExtrapolateLeft, mExtrapolateRight); break; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ModulusAnimatedNode.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ModulusAnimatedNode.kt index d7e7ec5e4bc1b4..b09d0f5276f191 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ModulusAnimatedNode.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ModulusAnimatedNode.kt @@ -21,8 +21,8 @@ internal class ModulusAnimatedNode( override public fun update() { val animatedNode = nativeAnimatedNodesManager.getNodeById(inputNode) if (animatedNode is ValueAnimatedNode) { - val animatedNodeValue = animatedNode.value - mValue = (animatedNodeValue % modulus + modulus) % modulus + val animatedNodeValue = animatedNode.getValue() + nodeValue = (animatedNodeValue % modulus + modulus) % modulus } else { throw JSApplicationCausedNativeException( "Illegal node ID set as an input for Animated.modulus node") diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/MultiplicationAnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/MultiplicationAnimatedNode.java index aac5a31267413e..06296ea7eb5f49 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/MultiplicationAnimatedNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/MultiplicationAnimatedNode.java @@ -32,11 +32,11 @@ public MultiplicationAnimatedNode( @Override public void update() { - mValue = 1; + nodeValue = 1; for (int i = 0; i < mInputNodes.length; i++) { AnimatedNode animatedNode = mNativeAnimatedNodesManager.getNodeById(mInputNodes[i]); if (animatedNode != null && animatedNode instanceof ValueAnimatedNode) { - mValue *= ((ValueAnimatedNode) animatedNode).getValue(); + nodeValue *= ((ValueAnimatedNode) animatedNode).getValue(); } else { throw new JSApplicationCausedNativeException( "Illegal node ID set as an input for Animated.multiply node"); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java index dac37629b2f39d..ed7438838b9220 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java @@ -209,7 +209,7 @@ public void setAnimatedNodeValue(int tag, double value) { + "] does not exist, or is not a 'value' node"); } stopAnimationsForNode(node); - ((ValueAnimatedNode) node).mValue = value; + ((ValueAnimatedNode) node).nodeValue = value; mUpdatedNodes.put(tag, node); } @@ -222,7 +222,7 @@ public void setAnimatedNodeOffset(int tag, double offset) { + tag + "] does not exist, or is not a 'value' node"); } - ((ValueAnimatedNode) node).mOffset = offset; + ((ValueAnimatedNode) node).offset = offset; mUpdatedNodes.put(tag, node); } @@ -306,7 +306,7 @@ private void stopAnimationsForNode(AnimatedNode animatedNode) { // Invoke animation end callback with {finished: false} WritableMap endCallbackResponse = Arguments.createMap(); endCallbackResponse.putBoolean("finished", false); - endCallbackResponse.putDouble("value", animation.mAnimatedValue.mValue); + endCallbackResponse.putDouble("value", animation.mAnimatedValue.nodeValue); animation.mEndCallback.invoke(endCallbackResponse); } else if (mReactApplicationContext != null) { // If no callback is passed in, this /may/ be an animation set up by the single-op @@ -315,7 +315,7 @@ private void stopAnimationsForNode(AnimatedNode animatedNode) { WritableMap params = Arguments.createMap(); params.putInt("animationId", animation.mId); params.putBoolean("finished", false); - params.putDouble("value", animation.mAnimatedValue.mValue); + params.putDouble("value", animation.mAnimatedValue.nodeValue); if (events == null) { events = Arguments.createArray(); } @@ -344,7 +344,7 @@ public void stopAnimation(int animationId) { // Invoke animation end callback with {finished: false} WritableMap endCallbackResponse = Arguments.createMap(); endCallbackResponse.putBoolean("finished", false); - endCallbackResponse.putDouble("value", animation.mAnimatedValue.mValue); + endCallbackResponse.putDouble("value", animation.mAnimatedValue.nodeValue); animation.mEndCallback.invoke(endCallbackResponse); } else if (mReactApplicationContext != null) { // If no callback is passed in, this /may/ be an animation set up by the single-op @@ -353,7 +353,7 @@ public void stopAnimation(int animationId) { WritableMap params = Arguments.createMap(); params.putInt("animationId", animation.mId); params.putBoolean("finished", false); - params.putDouble("value", animation.mAnimatedValue.mValue); + params.putDouble("value", animation.mAnimatedValue.nodeValue); if (events == null) { events = Arguments.createArray(); } @@ -672,7 +672,7 @@ public void runUpdates(long frameTimeNanos) { if (animation.mEndCallback != null) { WritableMap endCallbackResponse = Arguments.createMap(); endCallbackResponse.putBoolean("finished", true); - endCallbackResponse.putDouble("value", animation.mAnimatedValue.mValue); + endCallbackResponse.putDouble("value", animation.mAnimatedValue.nodeValue); animation.mEndCallback.invoke(endCallbackResponse); } else if (mReactApplicationContext != null) { // If no callback is passed in, this /may/ be an animation set up by the single-op @@ -681,7 +681,7 @@ public void runUpdates(long frameTimeNanos) { WritableMap params = Arguments.createMap(); params.putInt("animationId", animation.mId); params.putBoolean("finished", true); - params.putDouble("value", animation.mAnimatedValue.mValue); + params.putDouble("value", animation.mAnimatedValue.nodeValue); if (events == null) { events = Arguments.createArray(); } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SpringAnimation.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SpringAnimation.java index 29c69e141c0769..32e50e84114b11 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SpringAnimation.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/SpringAnimation.java @@ -79,21 +79,21 @@ public void runAnimationStep(long frameTimeNanos) { long frameTimeMillis = frameTimeNanos / 1000000; if (!mSpringStarted) { if (mCurrentLoop == 0) { - mOriginalValue = mAnimatedValue.mValue; + mOriginalValue = mAnimatedValue.nodeValue; mCurrentLoop = 1; } - mStartValue = mCurrentState.position = mAnimatedValue.mValue; + mStartValue = mCurrentState.position = mAnimatedValue.nodeValue; mLastTime = frameTimeMillis; mTimeAccumulator = 0.0; mSpringStarted = true; } advance((frameTimeMillis - mLastTime) / 1000.0); mLastTime = frameTimeMillis; - mAnimatedValue.mValue = mCurrentState.position; + mAnimatedValue.nodeValue = mCurrentState.position; if (isAtRest()) { if (mIterations == -1 || mCurrentLoop < mIterations) { // looping animation, return to start mSpringStarted = false; - mAnimatedValue.mValue = mOriginalValue; + mAnimatedValue.nodeValue = mOriginalValue; mCurrentLoop++; } else { // animation has completed mHasFinished = true; 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 index 4aac41ffcb4b90..f3b6ac8cab8269 100644 --- 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 @@ -37,9 +37,9 @@ public void update() { if (animatedNode != null && animatedNode instanceof ValueAnimatedNode) { double value = ((ValueAnimatedNode) animatedNode).getValue(); if (i == 0) { - mValue = value; + nodeValue = value; } else { - mValue -= value; + nodeValue -= value; } } else { throw new JSApplicationCausedNativeException( 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 7f4bf392033fb4..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[" + tag + "]: 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..e9799bbdb12f62 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.kt @@ -0,0 +1,50 @@ +/* + * 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(config: ReadableMap? = null) : AnimatedNode() { + @JvmField internal var nodeValue: Double = config?.getDouble("value") ?: Double.NaN + @JvmField internal var offset: Double = config?.getDouble("offset") ?: 0.0 + private var valueListener: AnimatedNodeValueListener? = null + + public fun getValue(): Double { + if ((offset + nodeValue).isNaN()) { + this.update() + } + return offset + nodeValue + } + + open fun getAnimatedObject(): Any? = null + + public fun flattenOffset(): Unit { + nodeValue += offset + offset = 0.0 + } + + public fun extractOffset(): Unit { + offset += nodeValue + nodeValue = 0.0 + } + + public fun onValueUpdate(): Unit { + valueListener?.onValueUpdate(getValue()) + } + + public fun setValueListener(listener: AnimatedNodeValueListener?): Unit { + valueListener = listener + } + + override public fun prettyPrint(): String = + "ValueAnimatedNode[$tag]: value: $nodeValue offset: $offset" +}