Skip to content

Commit

Permalink
Support Animated.event extracting values from array
Browse files Browse the repository at this point in the history
Summary:
`onPointerMove` events get dispatched with an `offset: [x, y]` attribute (API is not yet available in OSS, so subject to change), but `EventAnimationDriver` in RN Android is not able to extract values with such keys (even though the equivalent JS implementation does allow it).

(TODO: verify iOS behaviour)

Changelog:
[General][Added] - Animated.event can be used to extract values with numeric keys from native events

Reviewed By: mdvacca

Differential Revision: D32531117

fbshipit-source-id: 918a5443c5d8f5f8200d86bb67f84e8bc175c1d3
  • Loading branch information
javache authored and facebook-github-bot committed Jan 18, 2022
1 parent c338fff commit b210571
Showing 1 changed file with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
package com.facebook.react.animated;

import androidx.annotation.Nullable;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.UnexpectedNativeTypeException;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
Expand All @@ -31,12 +34,45 @@ public void receiveEvent(int targetTag, String eventName, @Nullable WritableMap
}

// Get the new value for the node by looking into the event map using the provided event path.
ReadableMap curMap = event;
ReadableMap currMap = event;
ReadableArray currArray = null;
for (int i = 0; i < mEventPath.size() - 1; i++) {
curMap = curMap.getMap(mEventPath.get(i));
if (currMap != null) {
String key = mEventPath.get(i);
ReadableType keyType = currMap.getType(key);
if (keyType == ReadableType.Map) {
currMap = currMap.getMap(key);
currArray = null;
} else if (keyType == ReadableType.Array) {
currArray = currMap.getArray(key);
currMap = null;
} else {
throw new UnexpectedNativeTypeException(
"Unexpected type " + keyType + " for key '" + key + "'");
}
} else {
int index = Integer.parseInt(mEventPath.get(i));
ReadableType keyType = currArray.getType(index);
if (keyType == ReadableType.Map) {
currMap = currArray.getMap(index);
currArray = null;
} else if (keyType == ReadableType.Array) {
currArray = currArray.getArray(index);
currMap = null;
} else {
throw new UnexpectedNativeTypeException(
"Unexpected type " + keyType + " for index '" + index + "'");
}
}
}

mValueNode.mValue = curMap.getDouble(mEventPath.get(mEventPath.size() - 1));
String lastKey = mEventPath.get(mEventPath.size() - 1);
if (currMap != null) {
mValueNode.mValue = currMap.getDouble(lastKey);
} else {
int lastIndex = Integer.parseInt(lastKey);
mValueNode.mValue = currArray.getDouble(lastIndex);
}
}

@Override
Expand Down

0 comments on commit b210571

Please sign in to comment.