Interop: move event interceptor lifecycle into separate class

Summary:
In next diff Coordinator will have get more responsibilities therefore it's better to separate the concerns to different class

Changelog: [Internal]

Reviewed By: shergin

Differential Revision: D18304834

fbshipit-source-id: 168a68969ed9da5772895f2da87e5273dccbaf30
This commit is contained in:
Samuel Susla
2019-11-05 12:33:06 -08:00
committed by Facebook Github Bot
parent 6fb5f81193
commit c6b16ecc41
3 changed files with 97 additions and 29 deletions
@@ -10,15 +10,15 @@
#import <React/UIView+React.h>
#import <react/components/legacyviewmanagerinterop/LegacyViewManagerInteropComponentDescriptor.h>
#import <react/components/legacyviewmanagerinterop/LegacyViewManagerInteropViewProps.h>
#import <react/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.h>
#import <react/utils/ManagedObjectWrapper.h>
#import "RCTLegacyViewManagerInteropCoordinatorAdapter.h"
using namespace facebook::react;
@implementation RCTLegacyViewManagerInteropComponentView {
UIView *_paperView;
NSMutableDictionary<NSNumber *, UIView *> *_viewsToBeMounted;
NSMutableArray<UIView *> *_viewsToBeUnmounted;
RCTLegacyViewManagerInteropCoordinatorAdapter *_adapter;
LegacyViewManagerInteropShadowNode::ConcreteState::Shared _state;
}
@@ -34,12 +34,6 @@ using namespace facebook::react;
return self;
}
- (void)setTag:(NSInteger)tag
{
[self.coordinator removeObserveForTag:self.tag];
[super setTag:tag];
}
+ (NSMutableSet<NSString *> *)supportedViewManagers
{
static NSMutableSet<NSString *> *supported =
@@ -78,10 +72,9 @@ using namespace facebook::react;
- (void)prepareForRecycle
{
_adapter = nil;
[_viewsToBeMounted removeAllObjects];
[_viewsToBeUnmounted removeAllObjects];
[_paperView removeFromSuperview];
_paperView = nil;
_state.reset();
[super prepareForRecycle];
}
@@ -110,41 +103,38 @@ using namespace facebook::react;
{
[super finalizeUpdates:updateMask];
if (!_paperView) {
if (!_adapter) {
_adapter = [[RCTLegacyViewManagerInteropCoordinatorAdapter alloc] initWithCoordinator:self.coordinator
reactTag:self.tag];
__weak __typeof(self) weakSelf = self;
_paperView = self.coordinator.paperView;
[self.coordinator addObserveForTag:self.tag
usingBlock:^(std::string eventName, folly::dynamic event) {
if (weakSelf) {
__typeof(self) strongSelf = weakSelf;
auto eventEmitter =
std::static_pointer_cast<LegacyViewManagerInteropViewEventEmitter const>(
strongSelf->_eventEmitter);
eventEmitter->dispatchEvent(eventName, event);
}
}];
_paperView.reactTag = [NSNumber numberWithInteger:self.tag];
self.contentView = _paperView;
_adapter.eventInterceptor = ^(std::string eventName, folly::dynamic event) {
if (weakSelf) {
__typeof(self) strongSelf = weakSelf;
auto eventEmitter =
std::static_pointer_cast<LegacyViewManagerInteropViewEventEmitter const>(strongSelf->_eventEmitter);
eventEmitter->dispatchEvent(eventName, event);
}
};
self.contentView = _adapter.paperView;
}
for (NSNumber *key in _viewsToBeMounted) {
[_paperView insertReactSubview:_viewsToBeMounted[key] atIndex:key.integerValue];
[_adapter.paperView insertReactSubview:_viewsToBeMounted[key] atIndex:key.integerValue];
}
[_viewsToBeMounted removeAllObjects];
for (UIView *view in _viewsToBeUnmounted) {
[_paperView removeReactSubview:view];
[_adapter.paperView removeReactSubview:view];
}
[_viewsToBeUnmounted removeAllObjects];
[_paperView didUpdateReactSubviews];
[_adapter.paperView didUpdateReactSubviews];
if (updateMask & RNComponentViewUpdateMaskProps) {
const auto &newProps = *std::static_pointer_cast<const LegacyViewManagerInteropViewProps>(_props);
[self.coordinator setProps:newProps.otherProps forView:_paperView];
[_adapter setProps:newProps.otherProps];
}
}
@@ -0,0 +1,25 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
#import <react/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.h>
NS_ASSUME_NONNULL_BEGIN
@interface RCTLegacyViewManagerInteropCoordinatorAdapter : NSObject
- (instancetype)initWithCoordinator:(RCTLegacyViewManagerInteropCoordinator *)coordinator reactTag:(NSInteger)tag;
@property (strong, nonatomic) UIView *paperView;
@property (nonatomic, copy, nullable) void (^eventInterceptor)(std::string eventName, folly::dynamic event);
- (void)setProps:(folly::dynamic const &)props;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,53 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTLegacyViewManagerInteropCoordinatorAdapter.h"
#import <React/UIView+React.h>
@implementation RCTLegacyViewManagerInteropCoordinatorAdapter {
RCTLegacyViewManagerInteropCoordinator *_coordinator;
NSInteger _tag;
}
- (instancetype)initWithCoordinator:(RCTLegacyViewManagerInteropCoordinator *)coordinator reactTag:(NSInteger)tag
{
if (self = [super init]) {
_coordinator = coordinator;
_tag = tag;
}
return self;
}
- (void)dealloc
{
[_paperView removeFromSuperview];
[_coordinator removeObserveForTag:_tag];
}
- (UIView *)paperView
{
if (!_paperView) {
_paperView = _coordinator.paperView;
_paperView.reactTag = [NSNumber numberWithInteger:_tag];
__weak __typeof(self) weakSelf = self;
[_coordinator addObserveForTag:_tag
usingBlock:^(std::string eventName, folly::dynamic event) {
if (weakSelf.eventInterceptor) {
weakSelf.eventInterceptor(eventName, event);
}
}];
}
return _paperView;
}
- (void)setProps:(const folly::dynamic &)props
{
[_coordinator setProps:props forView:self.paperView];
}
@end