ve[RN][Android] Convert StyleAnimatedNode.java->.kt (#45761)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45761

# Changelog:
[Internal] -

As in the title.

Reviewed By: cortinico

Differential Revision: D60342089

fbshipit-source-id: b780506c925e62d0b861a0c5d50794f617ab51f9
This commit is contained in:
Ruslan Shestopalyuk
2024-07-30 05:22:16 -07:00
committed by Facebook GitHub Bot
parent e51658abef
commit ef9149b4fc
2 changed files with 61 additions and 70 deletions
@@ -1,70 +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.JavaOnlyMap;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import java.util.HashMap;
import java.util.Map;
/**
* Native counterpart of style animated node (see AnimatedStyle class in AnimatedImplementation.js)
*/
/*package*/ class StyleAnimatedNode extends AnimatedNode {
private final NativeAnimatedNodesManager mNativeAnimatedNodesManager;
private final Map<String, Integer> mPropMapping;
StyleAnimatedNode(ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager) {
ReadableMap style = config.getMap("style");
ReadableMapKeySetIterator iter = style.keySetIterator();
mPropMapping = new HashMap<>();
while (iter.hasNextKey()) {
String propKey = iter.nextKey();
int nodeIndex = style.getInt(propKey);
mPropMapping.put(propKey, nodeIndex);
}
mNativeAnimatedNodesManager = nativeAnimatedNodesManager;
}
public void collectViewUpdates(JavaOnlyMap propsMap) {
for (Map.Entry<String, Integer> entry : mPropMapping.entrySet()) {
@Nullable AnimatedNode node = mNativeAnimatedNodesManager.getNodeById(entry.getValue());
if (node == null) {
throw new IllegalArgumentException("Mapped style node does not exist");
} else if (node instanceof TransformAnimatedNode) {
((TransformAnimatedNode) node).collectViewUpdates(propsMap);
} else if (node instanceof ValueAnimatedNode) {
Object animatedObject = ((ValueAnimatedNode) node).getAnimatedObject();
if (animatedObject instanceof Integer) {
propsMap.putInt(entry.getKey(), (Integer) animatedObject);
} else if (animatedObject instanceof String) {
propsMap.putString(entry.getKey(), (String) animatedObject);
} else {
propsMap.putDouble(entry.getKey(), ((ValueAnimatedNode) node).getValue());
}
} else if (node instanceof ColorAnimatedNode) {
propsMap.putInt(entry.getKey(), ((ColorAnimatedNode) node).getColor());
} else if (node instanceof ObjectAnimatedNode) {
((ObjectAnimatedNode) node).collectViewUpdates(entry.getKey(), propsMap);
} else {
throw new IllegalArgumentException(
"Unsupported type of node used in property node " + node.getClass());
}
}
}
public String prettyPrint() {
return "StyleAnimatedNode["
+ tag
+ "] mPropMapping: "
+ (mPropMapping != null ? mPropMapping.toString() : "null");
}
}
@@ -0,0 +1,61 @@
/*
* 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.JavaOnlyMap
import com.facebook.react.bridge.ReadableMap
/**
* Native counterpart of style animated node (see AnimatedStyle class in AnimatedImplementation.js)
*/
internal class StyleAnimatedNode(
config: ReadableMap,
private val nativeAnimatedNodesManager: NativeAnimatedNodesManager
) : AnimatedNode() {
private val propMapping: Map<String, Int>
init {
val style = config.getMap("style")
val iter = style?.keySetIterator()
propMapping =
buildMap() {
while (iter != null && iter.hasNextKey()) {
val propKey = iter.nextKey()
put(propKey, style.getInt(propKey))
}
}
}
public fun collectViewUpdates(propsMap: JavaOnlyMap) {
for ((key, value) in propMapping) {
val node = nativeAnimatedNodesManager.getNodeById(value)
requireNotNull(node) { "Mapped style node does not exist" }
if (node is TransformAnimatedNode) {
node.collectViewUpdates(propsMap)
} else if (node is ValueAnimatedNode) {
val animatedObject = node.getAnimatedObject()
if (animatedObject is Int) {
propsMap.putInt(key, animatedObject)
} else if (animatedObject is String) {
propsMap.putString(key, animatedObject)
} else {
propsMap.putDouble(key, node.getValue())
}
} else if (node is ColorAnimatedNode) {
propsMap.putInt(key, node.color)
} else if (node is ObjectAnimatedNode) {
node.collectViewUpdates(key, propsMap)
} else {
throw IllegalArgumentException(
"Unsupported type of node used in property node ${node.javaClass}")
}
}
}
override fun prettyPrint(): String = "StyleAnimatedNode[$tag] mPropMapping: $propMapping"
}