Skip to content

Commit

Permalink
Forward subviews to Paper component from LegacyViewManagerInterop
Browse files Browse the repository at this point in the history
Summary:
`RCTMaskedView` is simple enough to migrate manually to Fabric and I think we should eventually do it.
However this gives us opportunity to spot shortcomings of `LegacyViewManagerInterop` and address them.

Now `LegacyViewManagerInterop` forwards `mountChildComponent` and `unmountChildComponent` events to Paper component that it is wrapping.

Reviewed By: shergin

Differential Revision: D17905807

fbshipit-source-id: ad36c186d5c5a8ed164e412fa5fdf0042de46348
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Oct 16, 2019
1 parent d4b032d commit 5327c3e
Showing 1 changed file with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
using namespace facebook::react;

@implementation RCTLegacyViewManagerInteropComponentView {
UIView *_view;
UIView *_paperView;

/**
* A temporar storage of views that are being mounted to this component white paper component isn't yet ready.
*/
NSMutableArray<UIView *> *_insertedViews;
LegacyViewManagerInteropShadowNode::ConcreteState::Shared _state;
}

Expand All @@ -25,6 +30,7 @@ - (instancetype)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const LegacyViewManagerInteropViewProps>();
_props = defaultProps;
_insertedViews = [NSMutableArray new];
}

return self;
Expand All @@ -43,8 +49,35 @@ - (RCTLegacyViewManagerInteropCoordinator *)coordinator
const auto &state = _state->getData();
return unwrapManagedObject(state.coordinator);
} else {
return NULL;
return nil;
}
}

- (UIView *)paperView
{
if (!_paperView) {
__weak __typeof(self) weakSelf = self;
UIView *view = [self.coordinator viewWithInterceptor:^(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);
}
}];
if (view) {
for (NSUInteger i = 0; i < _insertedViews.count; i++) {
[view insertReactSubview:_insertedViews[i] atIndex:i];
}

[_insertedViews removeAllObjects];

[view didUpdateReactSubviews];
_paperView = view;
}
}

return _paperView;
}

- (NSString *)componentViewName_DO_NOT_USE_THIS_IS_BROKEN
Expand All @@ -56,6 +89,32 @@ - (NSString *)componentViewName_DO_NOT_USE_THIS_IS_BROKEN

#pragma mark - RCTComponentViewProtocol

- (void)prepareForRecycle
{
[_insertedViews removeAllObjects];
[super prepareForRecycle];
}

- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
if (self.paperView) {
[self.paperView insertReactSubview:childComponentView atIndex:index];
[self.paperView didUpdateReactSubviews];
} else {
[_insertedViews insertObject:childComponentView atIndex:index];
}
}

- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
if (self.paperView) {
[self.paperView removeReactSubview:childComponentView];
[self.paperView didUpdateReactSubviews];
} else {
[_insertedViews removeObjectAtIndex:index];
}
}

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<LegacyViewManagerInteropComponentDescriptor>();
Expand All @@ -69,25 +128,14 @@ - (void)updateState:(State::Shared const &)state oldState:(State::Shared const &
- (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
{
[super finalizeUpdates:updateMask];
assert(_props && _state);

if (!_view) {
__weak __typeof(self) weakSelf = self;
UIView *view = [self.coordinator viewWithInterceptor:^(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 = view;
_view = view;
if (!self.contentView) {
self.contentView = self.paperView;
}

if (updateMask & RNComponentViewUpdateMaskProps) {
const auto &newProps = *std::static_pointer_cast<const LegacyViewManagerInteropViewProps>(_props);
[self.coordinator setProps:newProps.otherProps forView:_view];
[self.coordinator setProps:newProps.otherProps forView:self.paperView];
}
}

Expand Down

0 comments on commit 5327c3e

Please sign in to comment.