forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
120 additions
and
145 deletions.
There are no files selected for viewing
145 changes: 0 additions & 145 deletions
145
...eact-native/ReactAndroid/src/main/java/com/facebook/react/animated/PropsAnimatedNode.java
This file was deleted.
Oops, something went wrong.
120 changes: 120 additions & 0 deletions
120
.../react-native/ReactAndroid/src/main/java/com/facebook/react/animated/PropsAnimatedNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* 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 android.view.View | ||
import com.facebook.react.bridge.JSApplicationIllegalArgumentException | ||
import com.facebook.react.bridge.JavaOnlyMap | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.facebook.react.bridge.UIManager | ||
import com.facebook.react.uimanager.common.UIManagerType | ||
import com.facebook.react.uimanager.common.ViewUtil.getUIManagerType | ||
|
||
/** | ||
* Animated node that represents view properties. There is a special handling logic implemented for | ||
* the nodes of this type in [NativeAnimatedNodesManager] that is responsible for extracting a map | ||
* of updated properties, which can be then passed down to the view. | ||
*/ | ||
internal class PropsAnimatedNode( | ||
config: ReadableMap, | ||
private val nativeAnimatedNodesManager: NativeAnimatedNodesManager | ||
) : AnimatedNode() { | ||
private var connectedViewTag = -1 | ||
private val propNodeMapping: MutableMap<String, Int> | ||
private val propMap = JavaOnlyMap() | ||
private var connectedViewUIManager: UIManager? = null | ||
|
||
init { | ||
val props = config.getMap("props") | ||
val iter = props?.keySetIterator() | ||
propNodeMapping = mutableMapOf() | ||
while (iter != null && iter.hasNextKey()) { | ||
val propKey = iter.nextKey() | ||
val nodeIndex = props.getInt(propKey) | ||
propNodeMapping[propKey] = nodeIndex | ||
} | ||
} | ||
|
||
public fun connectToView(viewTag: Int, uiManager: UIManager?) { | ||
if (connectedViewTag != -1) { | ||
throw JSApplicationIllegalArgumentException( | ||
"Animated node $tag is already attached to a view: $connectedViewTag") | ||
} | ||
connectedViewTag = viewTag | ||
connectedViewUIManager = uiManager | ||
} | ||
|
||
public fun disconnectFromView(viewTag: Int) { | ||
if (connectedViewTag != viewTag && connectedViewTag != -1) { | ||
throw JSApplicationIllegalArgumentException( | ||
"Attempting to disconnect view that has " + | ||
"not been connected with the given animated node: $viewTag " + | ||
"but is connected to view $connectedViewTag") | ||
} | ||
connectedViewTag = -1 | ||
} | ||
|
||
public fun restoreDefaultValues() { | ||
// Cannot restore default values if this view has already been disconnected. | ||
if (connectedViewTag == -1) { | ||
return | ||
} | ||
// Don't restore default values in Fabric. | ||
// In Non-Fabric this had the effect of "restore the value to whatever the value was on the | ||
// ShadowNode instead of in the View hierarchy". However, "synchronouslyUpdateViewOnUIThread" | ||
// will not have that impact on Fabric, because the FabricUIManager doesn't have access to the | ||
// ShadowNode layer. | ||
if (getUIManagerType(connectedViewTag) == UIManagerType.FABRIC) { | ||
return | ||
} | ||
val it = propMap.keySetIterator() | ||
while (it.hasNextKey()) { | ||
propMap.putNull(it.nextKey()) | ||
} | ||
connectedViewUIManager?.synchronouslyUpdateViewOnUIThread(connectedViewTag, propMap) | ||
} | ||
|
||
public fun updateView() { | ||
if (connectedViewTag == -1) { | ||
return | ||
} | ||
for ((key, value) in propNodeMapping) { | ||
val node = nativeAnimatedNodesManager.getNodeById(value) | ||
requireNotNull(node) { "Mapped property node does not exist" } | ||
if (node is StyleAnimatedNode) { | ||
node.collectViewUpdates(propMap) | ||
} else if (node is ValueAnimatedNode) { | ||
val animatedObject = node.getAnimatedObject() | ||
if (animatedObject is Int) { | ||
propMap.putInt(key, animatedObject) | ||
} else if (animatedObject is String) { | ||
propMap.putString(key, animatedObject) | ||
} else { | ||
propMap.putDouble(key, node.getValue()) | ||
} | ||
} else if (node is ColorAnimatedNode) { | ||
propMap.putInt(key, node.color) | ||
} else if (node is ObjectAnimatedNode) { | ||
node.collectViewUpdates(key, propMap) | ||
} else { | ||
throw IllegalArgumentException( | ||
"Unsupported type of node used in property node ${node.javaClass}") | ||
} | ||
} | ||
connectedViewUIManager?.synchronouslyUpdateViewOnUIThread(connectedViewTag, propMap) | ||
} | ||
|
||
public val connectedView: View? | ||
// resolveView throws an {@link IllegalViewOperationException} when the view doesn't exist | ||
// (this can happen if the surface is being deallocated). | ||
get() = runCatching { connectedViewUIManager?.resolveView(connectedViewTag) }.getOrNull() | ||
|
||
override fun prettyPrint(): String = | ||
"PropsAnimatedNode[$tag] connectedViewTag: $connectedViewTag " + | ||
"propNodeMapping: $propNodeMapping propMap: $propMap" | ||
} |