mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-09 22:43:10 +08:00
Native Animated - Restore default values when removing props on Android
Summary: Same as #11819 but for Android. I didn't notice the bug initially in my app because I was using different animations on Android which did not trigger this issue. **Test plan** Created a simple repro example and tested that this fixes it. https://gist.github.com/janicduplessis/0f3eb362dae63fedf99a0d3ee041796a Closes https://github.com/facebook/react-native/pull/12842 Differential Revision: D5556439 Pulled By: hramos fbshipit-source-id: d13f4ad258d03cca46c793751ebc49d942b99152
This commit is contained in:
committed by
Facebook Github Bot
parent
b4f91be647
commit
ac43548063
@@ -9,6 +9,7 @@
|
||||
|
||||
package com.facebook.react.animated;
|
||||
|
||||
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
||||
import com.facebook.react.bridge.JavaOnlyMap;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
||||
@@ -27,47 +28,78 @@ import javax.annotation.Nullable;
|
||||
*/
|
||||
/*package*/ class PropsAnimatedNode extends AnimatedNode {
|
||||
|
||||
/*package*/ int mConnectedViewTag = -1;
|
||||
|
||||
private int mConnectedViewTag = -1;
|
||||
private final NativeAnimatedNodesManager mNativeAnimatedNodesManager;
|
||||
private final Map<String, Integer> mPropMapping;
|
||||
private final UIImplementation mUIImplementation;
|
||||
private final Map<String, Integer> mPropNodeMapping;
|
||||
// This is the backing map for `mDiffMap` we can mutate this to update it instead of having to
|
||||
// create a new one for each update.
|
||||
private final JavaOnlyMap mPropMap;
|
||||
private final ReactStylesDiffMap mDiffMap;
|
||||
|
||||
PropsAnimatedNode(ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager) {
|
||||
PropsAnimatedNode(ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager, UIImplementation uiImplementation) {
|
||||
ReadableMap props = config.getMap("props");
|
||||
ReadableMapKeySetIterator iter = props.keySetIterator();
|
||||
mPropMapping = new HashMap<>();
|
||||
mPropNodeMapping = new HashMap<>();
|
||||
while (iter.hasNextKey()) {
|
||||
String propKey = iter.nextKey();
|
||||
int nodeIndex = props.getInt(propKey);
|
||||
mPropMapping.put(propKey, nodeIndex);
|
||||
mPropNodeMapping.put(propKey, nodeIndex);
|
||||
}
|
||||
mPropMap = new JavaOnlyMap();
|
||||
mDiffMap = new ReactStylesDiffMap(mPropMap);
|
||||
mNativeAnimatedNodesManager = nativeAnimatedNodesManager;
|
||||
mUIImplementation = uiImplementation;
|
||||
}
|
||||
|
||||
public final void updateView(UIImplementation uiImplementation) {
|
||||
if (mConnectedViewTag == -1) {
|
||||
throw new IllegalStateException("Node has not been attached to a view");
|
||||
public void connectToView(int viewTag) {
|
||||
if (mConnectedViewTag != -1) {
|
||||
throw new JSApplicationIllegalArgumentException("Animated node " + mTag + " is " +
|
||||
"already attached to a view");
|
||||
}
|
||||
JavaOnlyMap propsMap = new JavaOnlyMap();
|
||||
for (Map.Entry<String, Integer> entry : mPropMapping.entrySet()) {
|
||||
mConnectedViewTag = viewTag;
|
||||
}
|
||||
|
||||
public void disconnectFromView(int viewTag) {
|
||||
if (mConnectedViewTag != viewTag) {
|
||||
throw new JSApplicationIllegalArgumentException("Attempting to disconnect view that has " +
|
||||
"not been connected with the given animated node");
|
||||
}
|
||||
|
||||
mConnectedViewTag = -1;
|
||||
}
|
||||
|
||||
public void restoreDefaultValues() {
|
||||
ReadableMapKeySetIterator it = mPropMap.keySetIterator();
|
||||
while(it.hasNextKey()) {
|
||||
mPropMap.putNull(it.nextKey());
|
||||
}
|
||||
|
||||
mUIImplementation.synchronouslyUpdateViewOnUIThread(
|
||||
mConnectedViewTag,
|
||||
mDiffMap);
|
||||
}
|
||||
|
||||
public final void updateView() {
|
||||
if (mConnectedViewTag == -1) {
|
||||
return;
|
||||
}
|
||||
for (Map.Entry<String, Integer> entry : mPropNodeMapping.entrySet()) {
|
||||
@Nullable AnimatedNode node = mNativeAnimatedNodesManager.getNodeById(entry.getValue());
|
||||
if (node == null) {
|
||||
throw new IllegalArgumentException("Mapped property node does not exists");
|
||||
} else if (node instanceof StyleAnimatedNode) {
|
||||
((StyleAnimatedNode) node).collectViewUpdates(propsMap);
|
||||
((StyleAnimatedNode) node).collectViewUpdates(mPropMap);
|
||||
} else if (node instanceof ValueAnimatedNode) {
|
||||
propsMap.putDouble(entry.getKey(), ((ValueAnimatedNode) node).getValue());
|
||||
mPropMap.putDouble(entry.getKey(), ((ValueAnimatedNode) node).getValue());
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported type of node used in property node " +
|
||||
node.getClass());
|
||||
}
|
||||
}
|
||||
// TODO: Reuse propsMap and stylesDiffMap objects - note that in subsequent animation steps
|
||||
// for a given node most of the time we will be creating the same set of props (just with
|
||||
// different values). We can take advantage on that and optimize the way we allocate property
|
||||
// maps (we also know that updating view props doesn't retain a reference to the styles object).
|
||||
uiImplementation.synchronouslyUpdateViewOnUIThread(
|
||||
|
||||
mUIImplementation.synchronouslyUpdateViewOnUIThread(
|
||||
mConnectedViewTag,
|
||||
new ReactStylesDiffMap(propsMap));
|
||||
mDiffMap);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user