Files
react-native/ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.java
Janic Duplessis 6565929358 Add support for animated events
Summary:
This adds support for `Animated.event` driven natively. This is WIP and would like feedback on how this is implemented.

At the moment, it works by providing a mapping between a view tag, an event name, an event path and an animated value when a view has a prop with a `AnimatedEvent` object. Then we can hook into `EventDispatcher`, check for events that target our view + event name and update the animated value using the event path.

For now it works with the onScroll event but it should be generic enough to work with anything.
Closes https://github.com/facebook/react-native/pull/9253

Differential Revision: D3759844

Pulled By: foghina

fbshipit-source-id: 86989c705847955bd65e6cf5a7d572ec7ccd3eb4
2016-09-19 04:14:01 -07:00

53 lines
1.7 KiB
Java

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.animated;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import java.util.List;
import javax.annotation.Nullable;
/**
* Handles updating a {@link ValueAnimatedNode} when an event gets dispatched.
*/
/* package */ class EventAnimationDriver implements RCTEventEmitter {
private List<String> mEventPath;
/* package */ ValueAnimatedNode mValueNode;
public EventAnimationDriver(List<String> eventPath, ValueAnimatedNode valueNode) {
mEventPath = eventPath;
mValueNode = valueNode;
}
@Override
public void receiveEvent(int targetTag, String eventName, @Nullable WritableMap event) {
if (event == null) {
throw new IllegalArgumentException("Native animated events must have event data.");
}
// Get the new value for the node by looking into the event map using the provided event path.
ReadableMap curMap = event;
for (int i = 0; i < mEventPath.size() - 1; i++) {
curMap = curMap.getMap(mEventPath.get(i));
}
mValueNode.mValue = curMap.getDouble(mEventPath.get(mEventPath.size() - 1));
}
@Override
public void receiveTouches(String eventName, WritableArray touches, WritableArray changedIndices) {
throw new RuntimeException("receiveTouches is not support by native animated events");
}
}