2016-06-09 17:34:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#import "RCTNativeAnimatedModule.h"
|
|
|
|
|
2016-11-28 19:09:46 +00:00
|
|
|
#import "RCTNativeAnimatedNodesManager.h"
|
|
|
|
|
|
|
|
typedef void (^AnimatedOperation)(RCTNativeAnimatedNodesManager *nodesManager);
|
2016-06-09 17:34:41 +00:00
|
|
|
|
|
|
|
@implementation RCTNativeAnimatedModule
|
|
|
|
{
|
2016-11-28 19:09:46 +00:00
|
|
|
RCTNativeAnimatedNodesManager *_nodesManager;
|
2017-03-28 12:30:00 +00:00
|
|
|
|
|
|
|
// Oparations called after views have been updated.
|
2016-11-28 19:09:46 +00:00
|
|
|
NSMutableArray<AnimatedOperation> *_operations;
|
2017-03-28 12:30:00 +00:00
|
|
|
// Operations called before views have been updated.
|
|
|
|
NSMutableArray<AnimatedOperation> *_preOperations;
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 19:09:46 +00:00
|
|
|
RCT_EXPORT_MODULE();
|
2016-11-01 10:56:50 +00:00
|
|
|
|
2017-01-06 05:48:10 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
[_nodesManager stopAnimationLoop];
|
2016-11-01 10:56:50 +00:00
|
|
|
[self.bridge.eventDispatcher removeDispatchObserver:self];
|
2017-03-28 12:30:00 +00:00
|
|
|
[self.bridge.uiManager removeUIManagerObserver:self];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 19:44:28 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
// This module needs to be on the same queue as the UIManager to avoid
|
|
|
|
// having to lock `_operations` and `_preOperations` since `uiManagerWillFlushUIBlocks`
|
|
|
|
// will be called from that queue.
|
2016-11-28 19:09:46 +00:00
|
|
|
return RCTGetUIManagerQueue();
|
2016-08-07 19:44:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 19:09:46 +00:00
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
2016-08-12 01:10:16 +00:00
|
|
|
{
|
2016-11-28 19:09:46 +00:00
|
|
|
[super setBridge:bridge];
|
|
|
|
|
|
|
|
_nodesManager = [[RCTNativeAnimatedNodesManager alloc] initWithUIManager:self.bridge.uiManager];
|
|
|
|
_operations = [NSMutableArray new];
|
2017-03-28 12:30:00 +00:00
|
|
|
_preOperations = [NSMutableArray new];
|
2016-11-28 19:09:46 +00:00
|
|
|
|
|
|
|
[bridge.eventDispatcher addDispatchObserver:self];
|
2017-03-28 12:30:00 +00:00
|
|
|
[bridge.uiManager addUIManagerObserver:self];
|
2016-08-12 01:10:16 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 19:09:46 +00:00
|
|
|
#pragma mark -- API
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
RCT_EXPORT_METHOD(createAnimatedNode:(nonnull NSNumber *)tag
|
|
|
|
config:(NSDictionary<NSString *, id> *)config)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager createAnimatedNode:tag config:config];
|
|
|
|
}];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(connectAnimatedNodes:(nonnull NSNumber *)parentTag
|
|
|
|
childTag:(nonnull NSNumber *)childTag)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager connectAnimatedNodes:parentTag childTag:childTag];
|
|
|
|
}];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(disconnectAnimatedNodes:(nonnull NSNumber *)parentTag
|
|
|
|
childTag:(nonnull NSNumber *)childTag)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager disconnectAnimatedNodes:parentTag childTag:childTag];
|
|
|
|
}];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(startAnimatingNode:(nonnull NSNumber *)animationId
|
|
|
|
nodeTag:(nonnull NSNumber *)nodeTag
|
|
|
|
config:(NSDictionary<NSString *, id> *)config
|
|
|
|
endCallback:(RCTResponseSenderBlock)callBack)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager startAnimatingNode:animationId nodeTag:nodeTag config:config endCallback:callBack];
|
|
|
|
}];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(stopAnimation:(nonnull NSNumber *)animationId)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager stopAnimation:animationId];
|
|
|
|
}];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(setAnimatedNodeValue:(nonnull NSNumber *)nodeTag
|
|
|
|
value:(nonnull NSNumber *)value)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager setAnimatedNodeValue:nodeTag value:value];
|
|
|
|
}];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 17:17:29 +00:00
|
|
|
RCT_EXPORT_METHOD(setAnimatedNodeOffset:(nonnull NSNumber *)nodeTag
|
|
|
|
offset:(nonnull NSNumber *)offset)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager setAnimatedNodeOffset:nodeTag offset:offset];
|
|
|
|
}];
|
2016-09-26 17:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(flattenAnimatedNodeOffset:(nonnull NSNumber *)nodeTag)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager flattenAnimatedNodeOffset:nodeTag];
|
|
|
|
}];
|
2016-09-26 17:17:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 04:36:52 +00:00
|
|
|
RCT_EXPORT_METHOD(extractAnimatedNodeOffset:(nonnull NSNumber *)nodeTag)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager extractAnimatedNodeOffset:nodeTag];
|
|
|
|
}];
|
2016-11-08 04:36:52 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 17:34:41 +00:00
|
|
|
RCT_EXPORT_METHOD(connectAnimatedNodeToView:(nonnull NSNumber *)nodeTag
|
|
|
|
viewTag:(nonnull NSNumber *)viewTag)
|
|
|
|
{
|
2017-01-27 02:14:40 +00:00
|
|
|
NSString *viewName = [self.bridge.uiManager viewNameForReactTag:viewTag];
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2017-01-27 02:14:40 +00:00
|
|
|
[nodesManager connectAnimatedNodeToView:nodeTag viewTag:viewTag viewName:viewName];
|
2016-11-28 19:09:46 +00:00
|
|
|
}];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(disconnectAnimatedNodeFromView:(nonnull NSNumber *)nodeTag
|
|
|
|
viewTag:(nonnull NSNumber *)viewTag)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
// Disconnecting a view also restores its default values so we have to make
|
|
|
|
// sure this happens before views get updated with their new props. This is
|
|
|
|
// why we enqueue this on the pre-operations queue.
|
|
|
|
[self addPreOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager disconnectAnimatedNodeFromView:nodeTag viewTag:viewTag];
|
|
|
|
}];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(dropAnimatedNode:(nonnull NSNumber *)tag)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager dropAnimatedNode:tag];
|
|
|
|
}];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 01:10:16 +00:00
|
|
|
RCT_EXPORT_METHOD(startListeningToAnimatedNodeValue:(nonnull NSNumber *)tag)
|
|
|
|
{
|
2016-11-28 19:09:46 +00:00
|
|
|
__weak id<RCTValueAnimatedNodeObserver> valueObserver = self;
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager startListeningToAnimatedNodeValue:tag valueObserver:valueObserver];
|
|
|
|
}];
|
2016-08-12 01:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(stopListeningToAnimatedNodeValue:(nonnull NSNumber *)tag)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2017-03-28 16:08:51 +00:00
|
|
|
[nodesManager stopListeningToAnimatedNodeValue:tag];
|
2016-11-28 19:09:46 +00:00
|
|
|
}];
|
2016-08-12 01:10:16 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 10:56:50 +00:00
|
|
|
RCT_EXPORT_METHOD(addAnimatedEventToView:(nonnull NSNumber *)viewTag
|
|
|
|
eventName:(nonnull NSString *)eventName
|
|
|
|
eventMapping:(NSDictionary<NSString *, id> *)eventMapping)
|
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2016-11-28 19:09:46 +00:00
|
|
|
[nodesManager addAnimatedEventToView:viewTag eventName:eventName eventMapping:eventMapping];
|
|
|
|
}];
|
2016-11-01 10:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(removeAnimatedEventFromView:(nonnull NSNumber *)viewTag
|
2017-03-09 23:16:01 +00:00
|
|
|
eventName:(nonnull NSString *)eventName
|
|
|
|
animatedNodeTag:(nonnull NSNumber *)animatedNodeTag)
|
2016-11-01 10:56:50 +00:00
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
|
2017-03-09 23:16:01 +00:00
|
|
|
[nodesManager removeAnimatedEventFromView:viewTag eventName:eventName animatedNodeTag:animatedNodeTag];
|
2016-11-28 19:09:46 +00:00
|
|
|
}];
|
2016-11-01 10:56:50 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 19:09:46 +00:00
|
|
|
#pragma mark -- Batch handling
|
2016-08-12 01:10:16 +00:00
|
|
|
|
2017-03-28 12:30:00 +00:00
|
|
|
- (void)addOperationBlock:(AnimatedOperation)operation
|
2016-11-01 10:56:50 +00:00
|
|
|
{
|
2017-03-28 12:30:00 +00:00
|
|
|
[_operations addObject:operation];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addPreOperationBlock:(AnimatedOperation)operation
|
|
|
|
{
|
|
|
|
[_preOperations addObject:operation];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)uiManagerWillFlushUIBlocks:(RCTUIManager *)uiManager
|
|
|
|
{
|
|
|
|
if (_preOperations.count == 0 && _operations.count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSArray<AnimatedOperation> *preOperations = _preOperations;
|
|
|
|
NSArray<AnimatedOperation> *operations = _operations;
|
|
|
|
_preOperations = [NSMutableArray new];
|
2016-11-28 19:09:46 +00:00
|
|
|
_operations = [NSMutableArray new];
|
2016-11-01 10:56:50 +00:00
|
|
|
|
2017-03-28 12:30:00 +00:00
|
|
|
[uiManager prependUIBlock:^(__unused RCTUIManager *manager, __unused NSDictionary<NSNumber *, UIView *> *viewRegistry) {
|
|
|
|
for (AnimatedOperation operation in preOperations) {
|
2016-11-28 19:09:46 +00:00
|
|
|
operation(self->_nodesManager);
|
2017-03-28 12:30:00 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
[uiManager addUIBlock:^(__unused RCTUIManager *manager, __unused NSDictionary<NSNumber *, UIView *> *viewRegistry) {
|
|
|
|
for (AnimatedOperation operation in operations) {
|
|
|
|
operation(self->_nodesManager);
|
|
|
|
}
|
2017-03-28 17:50:00 +00:00
|
|
|
|
|
|
|
[self->_nodesManager updateAnimations];
|
2017-03-28 12:30:00 +00:00
|
|
|
}];
|
2016-10-31 21:21:58 +00:00
|
|
|
}
|
2016-08-12 01:10:16 +00:00
|
|
|
|
2016-11-28 19:09:46 +00:00
|
|
|
#pragma mark -- Events
|
2016-06-09 17:34:41 +00:00
|
|
|
|
2016-11-28 19:09:46 +00:00
|
|
|
- (NSArray<NSString *> *)supportedEvents
|
2016-06-09 17:34:41 +00:00
|
|
|
{
|
2016-11-28 19:09:46 +00:00
|
|
|
return @[@"onAnimatedValueUpdate"];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 19:09:46 +00:00
|
|
|
- (void)animatedNode:(RCTValueAnimatedNode *)node didUpdateValue:(CGFloat)value
|
2016-11-01 10:56:50 +00:00
|
|
|
{
|
2016-11-28 19:09:46 +00:00
|
|
|
[self sendEventWithName:@"onAnimatedValueUpdate"
|
|
|
|
body:@{@"tag": node.nodeTag, @"value": @(value)}];
|
2016-11-01 10:56:50 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 19:09:46 +00:00
|
|
|
- (void)eventDispatcherWillDispatchEvent:(id<RCTEvent>)event
|
2016-06-09 17:34:41 +00:00
|
|
|
{
|
2016-11-28 19:09:46 +00:00
|
|
|
// Native animated events only work for events dispatched from the main queue.
|
|
|
|
if (!RCTIsMainQueue()) {
|
|
|
|
return;
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
2016-11-28 19:09:46 +00:00
|
|
|
return [_nodesManager handleAnimatedEvent:event];
|
2016-06-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|