Skip to content

Commit

Permalink
PropsAnimatedNode.java->.kt (facebook#45757)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#45757

# Changelog:
[Internal] -

As in the title.

Differential Revision: D60341400
  • Loading branch information
rshest authored and facebook-github-bot committed Jul 28, 2024
1 parent 605405d commit 3b60d55
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 145 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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.IllegalViewOperationException
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 = HashMap()
while (iter != null && iter.hasNextKey()) {
val propKey = iter.nextKey()
val nodeIndex = props.getInt(propKey)
propNodeMapping[propKey] = nodeIndex
}
propMap = JavaOnlyMap()
}

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?
get() =
try {
connectedViewUIManager?.resolveView(connectedViewTag)
} catch (ex: IllegalViewOperationException) {
// resolveView throws an {@link IllegalViewOperationException} when the view doesn't exist
// (this can happen if the surface is being deallocated).
null
}

override fun prettyPrint(): String =
"PropsAnimatedNode[$tag] connectedViewTag: $connectedViewTag " +
"propNodeMapping: $propNodeMapping propMap: $propMap"
}

0 comments on commit 3b60d55

Please sign in to comment.