From b01b67a3fcec615a16a96c76e30809f538168dd7 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Thu, 16 Nov 2023 09:22:54 +0100 Subject: [PATCH] Fix it & hold it with a knee so it does not strike back --- ios/RNSScreenStack.mm | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/ios/RNSScreenStack.mm b/ios/RNSScreenStack.mm index 422cd74c03..207742a196 100644 --- a/ios/RNSScreenStack.mm +++ b/ios/RNSScreenStack.mm @@ -356,8 +356,11 @@ - (void)setModalViewControllers:(NSArray *)controllers // for the duration of transition. There are couple of scenarios: // (1) No modals are presented or all modals were presented by this RNSNavigationController, // (2) There are modals presented by other RNSNavigationControllers (nested/outer) - NSUInteger changeRootIndex = 0; + + // Last controller that is common for both _presentedModals & controllers UIViewController *changeRootController = _controller; + // Last common controller index + 1 + NSUInteger changeRootIndex = 0; for (NSUInteger i = 0; i < MIN(_presentedModals.count, controllers.count); i++) { if (_presentedModals[i] == controllers[i]) { changeRootController = controllers[i]; @@ -457,6 +460,30 @@ - (void)setModalViewControllers:(NSArray *)controllers } }; + // if (changeRootController.presentedViewController != nil) { + // BOOL shouldAnimate = changeRootIndex == controllers.count && + // [changeRootController.presentedViewController isKindOfClass:[RNSScreen class]] && + // ((RNSScreen *)changeRootController.presentedViewController).screenView.stackAnimation != + // RNSScreenStackAnimationNone; + // + // if ([_presentedModals containsObject:changeRootController.presentedViewController]) { + // BOOL shouldAnimate = changeRootIndex == controllers.count && + // [changeRootController.presentedViewController isKindOfClass:[RNSScreen class]] && + // ((RNSScreen *)changeRootController.presentedViewController).screenView.stackAnimation != + // RNSScreenStackAnimationNone; + // [changeRootController dismissViewControllerAnimated:shouldAnimate completion:finish]; + // } else if (newControllers.count > 0) { + // finish(); + // } else { + // // So the currently presented view controller is not in _presentedModals ==> it was presented by + // // different stack ==> we need to apply the changes from there. + // // Please note that this code is not robust (TODO explanation) + // [changeRootController.presentedViewController dismissViewControllerAnimated:shouldAnimate completion:finish]; + // } + // } else { + // finish(); + // } + if (changeRootController.presentedViewController != nil && [_presentedModals containsObject:changeRootController.presentedViewController]) { BOOL shouldAnimate = changeRootIndex == controllers.count && @@ -468,6 +495,16 @@ - (void)setModalViewControllers:(NSArray *)controllers // we present modals on top of changeRootController (which may be the this stack VC) NSLog(@"StackView %p is dismissing all VCs on top of %p", self, changeRootController); [changeRootController dismissViewControllerAnimated:shouldAnimate completion:finish]; + } else if ( + changeRootController.presentedViewController != nil && + changeRootController.presentedViewController.presentedViewController != nil) { + BOOL shouldAnimate = changeRootIndex == controllers.count && + [changeRootController.presentedViewController isKindOfClass:[RNSScreen class]] && + ((RNSScreen *)changeRootController.presentedViewController).screenView.stackAnimation != + RNSScreenStackAnimationNone; + [changeRootController.presentedViewController.presentedViewController dismissViewControllerAnimated:shouldAnimate + completion:finish]; + // RCTAssert(realVisibleViewController != nil, @"NO REAL VISIBLE VIEW CONTROLLER"); } else { NSLog(@"StackView %p is only presenting new modals", self); finish();