-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix RCTModalHostViewController crash while presenting react-native mo…
…dals (#7423) In some specific (and hard to reproduce) cases, RN modal presentation block tries to present a modal that is already presented in the hierarchy which caused that crash. Addresses facebook/react-native#32366
- Loading branch information
Showing
9 changed files
with
115 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#import "RNNModalManager.h" | ||
#import <Foundation/Foundation.h> | ||
#import <React/RCTModalHostViewManager.h> | ||
|
||
@interface RNNModalHostViewManagerHandler : NSObject | ||
|
||
- (instancetype)initWithModalManager:(RNNModalManager *)modalManager; | ||
|
||
- (void)connectModalHostViewManager:(RCTModalHostViewManager *)modalHostViewManager; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#import "RNNModalHostViewManagerHandler.h" | ||
|
||
@implementation RNNModalHostViewManagerHandler { | ||
RNNModalManager *_modalManager; | ||
} | ||
|
||
- (instancetype)initWithModalManager:(RNNModalManager *)modalManager { | ||
self = [super init]; | ||
_modalManager = modalManager; | ||
return self; | ||
} | ||
|
||
- (void)connectModalHostViewManager:(RCTModalHostViewManager *)modalHostViewManager { | ||
modalHostViewManager.presentationBlock = | ||
^(UIViewController *reactViewController, UIViewController *viewController, BOOL animated, | ||
dispatch_block_t completionBlock) { | ||
if (reactViewController.presentedViewController != viewController && | ||
[self->_modalManager topPresentedVC] != viewController) { | ||
[self->_modalManager showModal:viewController | ||
animated:animated | ||
completion:^(NSString *_Nonnull componentId) { | ||
if (completionBlock) | ||
completionBlock(); | ||
}]; | ||
} | ||
}; | ||
|
||
modalHostViewManager.dismissalBlock = | ||
^(UIViewController *reactViewController, UIViewController *viewController, BOOL animated, | ||
dispatch_block_t completionBlock) { | ||
[self->_modalManager dismissModal:viewController | ||
animated:animated | ||
completion:^{ | ||
if (completionBlock) | ||
completionBlock(); | ||
}]; | ||
}; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
playground/ios/NavigationTests/RNNModalHostViewManagerHandlerTest.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#import "RNNModalHostViewManagerHandler.h" | ||
#import <OCMock/OCMock.h> | ||
#import <XCTest/XCTest.h> | ||
|
||
@interface RNNModalHostViewManagerHandlerTest : XCTestCase | ||
@end | ||
|
||
@implementation RNNModalHostViewManagerHandlerTest { | ||
RNNModalHostViewManagerHandler *_uut; | ||
RNNModalManager *_modalManager; | ||
RCTModalHostViewManager *_modalHostViewManager; | ||
} | ||
|
||
- (void)setUp { | ||
_modalManager = [OCMockObject partialMockForObject:[[RNNModalManager alloc] init]]; | ||
_uut = [[RNNModalHostViewManagerHandler alloc] initWithModalManager:_modalManager]; | ||
_modalHostViewManager = [RCTModalHostViewManager new]; | ||
[_uut connectModalHostViewManager:_modalHostViewManager]; | ||
} | ||
|
||
- (void)testPresentationBlock_shouldNotPresentModalTwice { | ||
[[(id)_modalManager reject] showModal:OCMArg.any animated:NO completion:OCMArg.any]; | ||
_modalHostViewManager.presentationBlock(nil, _modalManager.topPresentedVC, NO, nil); | ||
} | ||
|
||
- (void)testPresentationBlock_shouldShowModal { | ||
UIViewController *vc = UIViewController.new; | ||
_modalHostViewManager.presentationBlock(nil, vc, NO, nil); | ||
XCTAssertEqual(vc, _modalManager.topPresentedVC); | ||
} | ||
|
||
- (void)testPresentationBlock_shouldShowAndDismissModal { | ||
XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"]; | ||
UIViewController *vc = UIViewController.new; | ||
_modalHostViewManager.presentationBlock(nil, vc, NO, nil); | ||
XCTAssertEqual(vc, _modalManager.topPresentedVC); | ||
_modalHostViewManager.dismissalBlock(nil, vc, NO, ^{ | ||
XCTAssertNotEqual(vc, self->_modalManager.topPresentedVC); | ||
[expectation fulfill]; | ||
}); | ||
[self waitForExpectationsWithTimeout:5 handler:nil]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters