Skip to content

Commit

Permalink
Dismiss presented modals and clear the component store on reloading (#…
Browse files Browse the repository at this point in the history
…5682)

* Dismiss presented modals and clear the component store on reload

* Detach all overlays on react reload

* Move synchronous dismissing modals to modalManager

* Fix iOS unit tests
  • Loading branch information
yogevbd authored Nov 26, 2019
1 parent b66ff1d commit 9970853
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 12 deletions.
14 changes: 11 additions & 3 deletions lib/ios/RNNBridgeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ @interface RNNBridgeManager() <RCTBridgeDelegate>
@property (nonatomic, strong, readwrite) RCTBridge *bridge;
@property (nonatomic, strong, readwrite) RNNExternalComponentStore *store;
@property (nonatomic, strong, readwrite) RNNReactComponentRegistry *componentRegistry;
@property (nonatomic, strong, readonly) RNNOverlayManager *overlayManager;
@property (nonatomic, strong, readonly) RNNModalManager *modalManager;

@end

Expand All @@ -37,6 +39,9 @@ - (instancetype)initWithJsCodeLocation:(NSURL *)jsCodeLocation launchOptions:(NS
_launchOptions = launchOptions;
_delegate = delegate;

_overlayManager = [RNNOverlayManager new];
_modalManager = [RNNModalManager new];

_store = [RNNExternalComponentStore new];
_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];

Expand Down Expand Up @@ -85,8 +90,8 @@ - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
id<RNNComponentViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
_componentRegistry = [[RNNReactComponentRegistry alloc] initWithCreator:rootViewCreator];
RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator eventEmitter:eventEmitter store:_store componentRegistry:_componentRegistry andBridge:bridge];
_commandsHandler = [[RNNCommandsHandler alloc] initWithControllerFactory:controllerFactory eventEmitter:eventEmitter stackManager:[RNNNavigationStackManager new] modalManager:[RNNModalManager new] overlayManager:[RNNOverlayManager new] mainWindow:_mainWindow];

_commandsHandler = [[RNNCommandsHandler alloc] initWithControllerFactory:controllerFactory eventEmitter:eventEmitter stackManager:[RNNNavigationStackManager new] modalManager:_modalManager overlayManager:_overlayManager mainWindow:_mainWindow];
RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:_commandsHandler];

return [@[bridgeModule,eventEmitter] arrayByAddingObjectsFromArray:[self extraModulesFromDelegate]];
Expand All @@ -104,7 +109,10 @@ - (void)onJavaScriptLoaded {
}

- (void)onBridgeWillReload {
UIApplication.sharedApplication.delegate.window.rootViewController = nil;
[_overlayManager dismissAllOverlays];
[_modalManager dismissAllModalsSynchronosly];
[_componentRegistry clear];
UIApplication.sharedApplication.delegate.window.rootViewController = nil;
}

@end
Expand Down
4 changes: 2 additions & 2 deletions lib/ios/RNNCommandsHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ - (void)setRoot:(NSDictionary*)layout commandId:(NSString*)commandId completion:
}
}

[_modalManager dismissAllModalsAnimated:NO];
[_modalManager dismissAllModalsAnimated:NO completion:nil];

UIViewController *vc = [_controllerFactory createLayout:layout[@"root"]];

Expand Down Expand Up @@ -291,7 +291,7 @@ - (void)dismissAllModals:(NSDictionary *)mergeOptions commandId:(NSString*)comma
completion();
}];
RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
[_modalManager dismissAllModalsAnimated:[options.animations.dismissModal.enable getWithDefaultValue:YES]];
[_modalManager dismissAllModalsAnimated:[options.animations.dismissModal.enable getWithDefaultValue:YES] completion:nil];

[CATransaction commit];
}
Expand Down
3 changes: 2 additions & 1 deletion lib/ios/RNNModalManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef void (^RNNTransitionRejectionBlock)(NSString *code, NSString *message, N
- (void)showModal:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNTransitionWithComponentIdCompletionBlock)completion;
- (void)showModal:(UIViewController *)viewController animated:(BOOL)animated hasCustomAnimation:(BOOL)hasCustomAnimation completion:(RNNTransitionWithComponentIdCompletionBlock)completion;
- (void)dismissModal:(UIViewController *)viewController completion:(RNNTransitionCompletionBlock)completion;
- (void)dismissAllModalsAnimated:(BOOL)animated;
- (void)dismissAllModalsAnimated:(BOOL)animated completion:(void (^ __nullable)(void))completion;
- (void)dismissAllModalsSynchronosly;

@end
17 changes: 15 additions & 2 deletions lib/ios/RNNModalManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,27 @@ - (void)dismissModal:(UIViewController *)viewController completion:(RNNTransitio
}
}

-(void)dismissAllModalsAnimated:(BOOL)animated {
- (void)dismissAllModalsAnimated:(BOOL)animated completion:(void (^ __nullable)(void))completion {
UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
[root dismissViewControllerAnimated:animated completion:nil];
[root dismissViewControllerAnimated:animated completion:completion];
[_delegate dismissedMultipleModals:_presentedModals];
[_pendingModalIdsToDismiss removeAllObjects];
[_presentedModals removeAllObjects];
}

- (void)dismissAllModalsSynchronosly {
if (_presentedModals.count) {
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[self dismissAllModalsAnimated:NO completion:^{
dispatch_semaphore_signal(sem);
}];

while (dispatch_semaphore_wait(sem, DISPATCH_TIME_NOW)) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0]];
}
}
}

#pragma mark - private


Expand Down
1 change: 1 addition & 0 deletions lib/ios/RNNOverlayManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- (void)showOverlayWindow:(UIWindow*)viewController;
- (void)showOverlayWindowAsKeyWindow:(UIWindow *)overlayWindow;
- (void)dismissOverlay:(UIViewController*)viewController;
- (void)dismissAllOverlays;

@property (nonatomic, retain) NSMutableArray* overlayWindows;

Expand Down
10 changes: 8 additions & 2 deletions lib/ios/RNNOverlayManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ - (void)showOverlayWindowAsKeyWindow:(RNNOverlayWindow *)overlayWindow {

- (void)dismissOverlay:(UIViewController*)viewController {
RNNOverlayWindow* overlayWindow = [self findWindowByRootViewController:viewController];
[overlayWindow.previousWindow makeKeyWindow];
[self detachOverlayWindow:overlayWindow];
}

- (void)dismissAllOverlays {
for (RNNOverlayWindow* overlayWindow in _overlayWindows) {
[self detachOverlayWindow:overlayWindow];
}
}

#pragma mark - private

- (void)detachOverlayWindow:(UIWindow *)overlayWindow {
- (void)detachOverlayWindow:(RNNOverlayWindow *)overlayWindow {
[overlayWindow.previousWindow makeKeyWindow];
[overlayWindow setHidden:YES];
[overlayWindow setRootViewController:nil];
[_overlayWindows removeObject:overlayWindow];
Expand Down
4 changes: 2 additions & 2 deletions lib/ios/ReactNativeNavigationTests/RNNModalManagerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (void)testDismissMultipleModalsInvokeDelegateWithCorrectParameters {
[_modalManager showModal:_vc3 animated:NO completion:nil];

_modalManager.delegate = self;
[_modalManager dismissAllModalsAnimated:NO];
[_modalManager dismissAllModalsAnimated:NO completion:nil];

XCTAssertTrue(_modalDismissedCount == 3);
}
Expand Down Expand Up @@ -87,7 +87,7 @@ - (void)testDismissAllModals_AfterDismissingPreviousModal_InvokeDelegateWithCorr
[_modalManager dismissModal:_vc2 completion:nil];

XCTAssertTrue(_modalDismissedCount == 1);
[_modalManager dismissAllModalsAnimated:NO];
[_modalManager dismissAllModalsAnimated:NO completion:nil];
XCTAssertTrue(_modalDismissedCount == 2);
}

Expand Down

0 comments on commit 9970853

Please sign in to comment.