Skip to content

Commit

Permalink
Fix crash caused by <Modal> trying to present view controller twice
Browse files Browse the repository at this point in the history
Summary:
Changelog: [Internal]

`_viewController` was being presented twice causing following exception

```
'Application tried to present modally an active controller <FBReactRootViewController: 0x7fe741818b80;
```

Reviewed By: shergin

Differential Revision: D20820395

fbshipit-source-id: 5c9489011e5f99d8bd37befbd544d2d55a650589
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Apr 2, 2020
1 parent b145a82 commit 2c3a6ab
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ @implementation RCTModalHostViewComponentView {
RCTFabricModalHostViewController *_viewController;
ModalHostViewShadowNode::ConcreteState::Shared _state;
BOOL _shouldAnimatePresentation;
BOOL _isPresented;
}

- (instancetype)initWithFrame:(CGRect)frame
Expand All @@ -113,21 +114,18 @@ - (instancetype)initWithFrame:(CGRect)frame
_viewController = [RCTFabricModalHostViewController new];
_viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
_viewController.delegate = self;
_isPresented = NO;
}

return self;
}

- (BOOL)isViewControllerPresented
{
return _viewController.presentingViewController != nil;
}

- (void)ensurePresentedOnlyIfNeeded
{
BOOL shouldBePresented = !self.isViewControllerPresented && self.window;
BOOL shouldBePresented = !_isPresented && self.window;
if (shouldBePresented) {
UIViewController *controller = [self reactViewController];
_isPresented = YES;
return [controller
presentViewController:_viewController
animated:_shouldAnimatePresentation
Expand All @@ -142,8 +140,9 @@ - (void)ensurePresentedOnlyIfNeeded
}];
}

BOOL shouldBeHidden = self.isViewControllerPresented && !self.superview;
BOOL shouldBeHidden = _isPresented && !self.superview;
if (shouldBeHidden) {
_isPresented = NO;
[_viewController dismissViewControllerAnimated:_shouldAnimatePresentation completion:nil];
}
}
Expand Down Expand Up @@ -184,6 +183,13 @@ + (ComponentDescriptorProvider)componentDescriptorProvider
return concreteComponentDescriptorProvider<ModalHostViewComponentDescriptor>();
}

- (void)prepareForRecycle
{
[super prepareForRecycle];
_state.reset();
_isPresented = NO;
}

- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &newProps = *std::static_pointer_cast<const ModalHostViewProps>(props);
Expand Down

0 comments on commit 2c3a6ab

Please sign in to comment.