Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix modal and alert dismissal bugs #22666

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions React/Modules/RCTAlertManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ @interface RCTAlertManager()
@implementation RCTAlertManager
{
NSHashTable *_alertControllers;
UIWindow *_window;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retaining anything in ViewManager is very bad idea in general (and especially for UIKit things).

}

RCT_EXPORT_MODULE()
Expand Down Expand Up @@ -90,11 +91,16 @@ - (void)invalidate
}
}

UIViewController *presentingController = RCTPresentedViewController();
if (presentingController == nil) {
RCTLogError(@"Tried to display alert view but there is no application window. args: %@", args);
return;
}
CGSize screenSize = [UIScreen mainScreen].bounds.size;
self->_window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
#if TARGET_OS_TV
self->_window.windowLevel = UIWindowLevelNormal + 1;
#else
self->_window.windowLevel = UIWindowLevelStatusBar + 1;
#endif
UIViewController *presentingController = [UIViewController new];
self->_window.rootViewController = presentingController;
self->_window.hidden = NO;

UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:title
Expand Down Expand Up @@ -152,6 +158,7 @@ - (void)invalidate
[alertController addAction:[UIAlertAction actionWithTitle:buttonTitle
style:buttonStyle
handler:^(__unused UIAlertAction *action) {
self->_window = nil;
switch (type) {
case RCTAlertViewStylePlainTextInput:
case RCTAlertViewStyleSecureTextInput:
Expand Down
9 changes: 7 additions & 2 deletions React/Views/RCTModalHostView.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@

@protocol RCTModalHostViewInteractor <NSObject>

- (void)presentModalHostView:(RCTModalHostView *)modalHostView withViewController:(RCTModalHostViewController *)viewController animated:(BOOL)animated;
- (void)dismissModalHostView:(RCTModalHostView *)modalHostView withViewController:(RCTModalHostViewController *)viewController animated:(BOOL)animated;
- (void)presentModalHostView:(RCTModalHostView *)modalHostView
withViewController:(RCTModalHostViewController *)viewController
animated:(BOOL)animated;
- (void)dismissModalHostView:(RCTModalHostView *)modalHostView
withViewController:(RCTModalHostViewController *)viewController
animated:(BOOL)animated
completion:(void (^)(void))completionHandler;

@end
8 changes: 6 additions & 2 deletions React/Views/RCTModalHostView.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ - (void)didUpdateReactSubviews
- (void)dismissModalViewController
{
if (_isPresented) {
[_delegate dismissModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
_isPresented = NO;
[_delegate dismissModalHostView:self
withViewController:_modalViewController
animated:[self hasAnimationType]
completion:^{
self->_isPresented = NO;
}];
}
}

Expand Down
1 change: 0 additions & 1 deletion React/Views/RCTModalHostViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,4 @@ - (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif // RCT_DEV
#endif // !TARGET_OS_TV


@end
24 changes: 19 additions & 5 deletions React/Views/RCTModalHostViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ - (UIView *)view
return view;
}

- (void)presentModalHostView:(RCTModalHostView *)modalHostView withViewController:(RCTModalHostViewController *)viewController animated:(BOOL)animated
- (void)presentModalHostView:(RCTModalHostView *)modalHostView
withViewController:(RCTModalHostViewController *)viewController
animated:(BOOL)animated
{
dispatch_block_t completionBlock = ^{
if (modalHostView.onShow) {
Expand All @@ -75,25 +77,37 @@ - (void)presentModalHostView:(RCTModalHostView *)modalHostView withViewControlle
if (_presentationBlock) {
_presentationBlock([modalHostView reactViewController], viewController, animated, completionBlock);
} else {
[[modalHostView reactViewController] presentViewController:viewController animated:animated completion:completionBlock];
[[modalHostView reactViewController]
presentViewController:viewController animated:animated completion:completionBlock];
}
}

- (void)dismissModalHostView:(RCTModalHostView *)modalHostView withViewController:(RCTModalHostViewController *)viewController animated:(BOOL)animated
- (void)dismissModalHostView:(RCTModalHostView *)modalHostView
withViewController:(RCTModalHostViewController *)viewController
animated:(BOOL)animated
completion:(void (^)(void))completionHandler
{
dispatch_block_t completionBlock = ^{
if (modalHostView.identifier) {
[[self.bridge moduleForClass:[RCTModalManager class]] modalDismissed:modalHostView.identifier];
}
if (completionHandler) {
completionHandler();
}
};
if (_dismissalBlock) {
_dismissalBlock([modalHostView reactViewController], viewController, animated, completionBlock);
} else {
[viewController dismissViewControllerAnimated:animated completion:completionBlock];
if (viewController.presentedViewController && viewController.presentingViewController) {
// Ask the presenting view controller to dismiss any view controllers presented on top of the modal host
// together with the host itself.
[viewController.presentingViewController dismissViewControllerAnimated:animated completion:completionBlock];
} else {
[viewController dismissViewControllerAnimated:animated completion:completionBlock];
}
}
}


- (RCTShadowView *)shadowView
{
return [RCTModalHostShadowView new];
Expand Down