From 98e1104df7fe0e7dec0964fc542bab5b6a681ca0 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 24 Jul 2024 12:33:28 -0700 Subject: [PATCH] Migrate AnimatedNode.java to Kotlin (#45601) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45601 AnimatedNode.java -> AnimatedNode.kt changelog: [internal] internal Differential Revision: D60076481 Reviewed By: tdn120 --- .../ReactAndroid/api/ReactAndroid.api | 17 ++++ .../facebook/react/animated/AnimatedNode.java | 78 ------------------- .../facebook/react/animated/AnimatedNode.kt | 74 ++++++++++++++++++ 3 files changed, 91 insertions(+), 78 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AnimatedNode.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AnimatedNode.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index d60bbbb9898dc4..ca064ca756b3c4 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -440,6 +440,23 @@ public abstract interface class com/facebook/react/ViewManagerOnDemandReactPacka public abstract fun getViewManagerNames (Lcom/facebook/react/bridge/ReactApplicationContext;)Ljava/util/Collection; } +public abstract class com/facebook/react/animated/AnimatedNode { + public static final field Companion Lcom/facebook/react/animated/AnimatedNode$Companion; + public static final field DEFAULT_ANIMATED_NODE_CHILD_COUNT I + public static final field INITIAL_BFS_COLOR I + public fun ()V + public final fun addChild (Lcom/facebook/react/animated/AnimatedNode;)V + public fun onAttachedToNode (Lcom/facebook/react/animated/AnimatedNode;)V + public fun onDetachedFromNode (Lcom/facebook/react/animated/AnimatedNode;)V + public abstract fun prettyPrint ()Ljava/lang/String; + public final fun prettyPrintWithChildren ()Ljava/lang/String; + public final fun removeChild (Lcom/facebook/react/animated/AnimatedNode;)V + public fun update ()V +} + +public final class com/facebook/react/animated/AnimatedNode$Companion { +} + public class com/facebook/react/animated/NativeAnimatedModule : com/facebook/fbreact/specs/NativeAnimatedModuleSpec, com/facebook/react/bridge/LifecycleEventListener, com/facebook/react/bridge/UIManagerListener { public static final field ANIMATED_MODULE_DEBUG Z public fun (Lcom/facebook/react/bridge/ReactApplicationContext;)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AnimatedNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AnimatedNode.java deleted file mode 100644 index 089a4a25b350d3..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AnimatedNode.java +++ /dev/null @@ -1,78 +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.infer.annotation.Assertions; -import com.facebook.infer.annotation.Nullsafe; -import java.util.ArrayList; -import java.util.List; - -/** Base class for all Animated.js library node types that can be created on the "native" side. */ -/*package*/ @Nullsafe(Nullsafe.Mode.LOCAL) -abstract class AnimatedNode { - - public static final int INITIAL_BFS_COLOR = 0; - - private static final int DEFAULT_ANIMATED_NODE_CHILD_COUNT = 1; - - /*package*/ @Nullable List mChildren; /* lazy-initialized when a child is added */ - /*package*/ int mActiveIncomingNodes = 0; - /*package*/ int mBFSColor = INITIAL_BFS_COLOR; - /*package*/ int mTag = -1; - - public final void addChild(AnimatedNode child) { - if (mChildren == null) { - mChildren = new ArrayList<>(DEFAULT_ANIMATED_NODE_CHILD_COUNT); - } - Assertions.assertNotNull(mChildren).add(child); - child.onAttachedToNode(this); - } - - public final void removeChild(AnimatedNode child) { - if (mChildren == null) { - return; - } - child.onDetachedFromNode(this); - mChildren.remove(child); - } - - /** - * Subclasses may want to override this method in order to store a reference to the parent of a - * given node that can then be used to calculate current node's value in {@link #update}. In that - * case it is important to also override {@link #onDetachedFromNode} to clear that reference once - * current node gets detached. - */ - public void onAttachedToNode(AnimatedNode parent) {} - - /** See {@link #onAttachedToNode} */ - public void onDetachedFromNode(AnimatedNode parent) {} - - /** - * This method will be run on each node at most once every repetition of the animation loop. It - * will be executed on a node only when all the node's parent has already been updated. Therefore - * it can be used to calculate node's value. - */ - public void update() {} - - /** - * Pretty-printer for the AnimatedNode. Only called in production pre-crash for debug diagnostics. - */ - public abstract String prettyPrint(); - - public String prettyPrintWithChildren() { - String children = ""; - if (mChildren != null && mChildren.size() > 0) { - for (AnimatedNode child : mChildren) { - children += " " + child.mTag; - } - } - - return prettyPrint() + (children.length() > 0 ? " children: " + children : ""); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AnimatedNode.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AnimatedNode.kt new file mode 100644 index 00000000000000..a57729dc8a1474 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/AnimatedNode.kt @@ -0,0 +1,74 @@ +/* + * 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 java.util.ArrayList + +/** Base class for all Animated.js library node types that can be created on the "native" side. */ +public abstract class AnimatedNode { + + public companion object { + public const val INITIAL_BFS_COLOR: Int = 0 + public const val DEFAULT_ANIMATED_NODE_CHILD_COUNT: Int = 1 + } + + // TODO: T196787278 Reduce the visibility of these fields to package once we have + // converted the whole module to Kotlin + + @JvmField + internal var mChildren: MutableList? = + null /* lazy-initialized when a child is added */ + @JvmField internal var mActiveIncomingNodes: Int = 0 + @JvmField internal var mBFSColor: Int = INITIAL_BFS_COLOR + @JvmField internal var mTag: Int = -1 + + public fun addChild(child: AnimatedNode): Unit { + val children = + mChildren + ?: ArrayList(DEFAULT_ANIMATED_NODE_CHILD_COUNT).also { mChildren = it } + + children.add(child) + child.onAttachedToNode(this) + } + + public fun removeChild(child: AnimatedNode): Unit { + val children = mChildren ?: return + child.onDetachedFromNode(this) + children.remove(child) + } + + /** + * Subclasses may want to override this method in order to store a reference to the parent of a + * given node that can then be used to calculate current node's value in {@link #update}. In that + * case it is important to also override {@link #onDetachedFromNode} to clear that reference once + * current node gets detached. + */ + public open fun onAttachedToNode(parent: AnimatedNode): Unit = Unit + + /** See {@link #onAttachedToNode} */ + public open fun onDetachedFromNode(parent: AnimatedNode): Unit = Unit + + /** + * This method will be run on each node at most once every repetition of the animation loop. It + * will be executed on a node only when all the node's parent has already been updated. Therefore + * it can be used to calculate node's value. + */ + public open fun update(): Unit = Unit + + /** + * Pretty-printer for the AnimatedNode. Only called in production pre-crash for debug diagnostics. + */ + public abstract fun prettyPrint(): String + + public fun prettyPrintWithChildren(): String { + val children = StringBuilder() + mChildren?.forEach { child -> children.append(" ${child.mTag}") } + + return prettyPrint() + if (children.isNotEmpty()) " children: $children" else "" + } +}