Skip to content

Commit

Permalink
Migrate ValueAnimatedNode to Kotlin (facebook#45651)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#45651

ValueAnimatedNode.java -> ValueAnimatedNode.kt

changelog: [internal] internal

Reviewed By: rshest

Differential Revision: D60188318
  • Loading branch information
arushikesarwani94 authored and facebook-github-bot committed Jul 28, 2024
1 parent caa4ac7 commit 748557c
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -74,6 +74,6 @@ public void runAnimationStep(long frameTimeNanos) {
}

mLastValue = value;
mAnimatedValue.mValue = value;
mAnimatedValue.nodeValue = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -109,6 +109,6 @@ public void runAnimationStep(long frameTimeNanos) {
} else {
nextValue = mFromValue + mFrames[frameIndex] * (mToValue - mFromValue);
}
mAnimatedValue.mValue = nextValue;
mAnimatedValue.nodeValue = nextValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 748557c

Please sign in to comment.