react-native-navigation的迁移库

RNNModalManager.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #import "RNNModalManager.h"
  2. #import "RNNComponentViewController.h"
  3. #import "UIViewController+LayoutProtocol.h"
  4. #import "ModalTransitionDelegate.h"
  5. #import "ModalDismissTransitionDelegate.h"
  6. @interface RNNModalManager ()
  7. @property (nonatomic, strong) ModalTransitionDelegate* modalTransitionDelegate;
  8. @end
  9. @implementation RNNModalManager {
  10. NSMutableArray* _pendingModalIdsToDismiss;
  11. NSMutableArray* _presentedModals;
  12. RCTUIManager* _uiManager;
  13. }
  14. - (instancetype)init {
  15. self = [super init];
  16. _pendingModalIdsToDismiss = [[NSMutableArray alloc] init];
  17. _presentedModals = [[NSMutableArray alloc] init];
  18. return self;
  19. }
  20. - (instancetype)initWithUIManager:(RCTUIManager *)uiManager {
  21. self = [self init];
  22. _uiManager = uiManager;
  23. return self;
  24. }
  25. - (void)showModal:(UIViewController<RNNLayoutProtocol> *)viewController animated:(BOOL)animated completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  26. if (!viewController) {
  27. @throw [NSException exceptionWithName:@"ShowUnknownModal" reason:@"showModal called with nil viewController" userInfo:nil];
  28. }
  29. UIViewController* topVC = [self topPresentedVC];
  30. if (viewController.presentationController) {
  31. viewController.presentationController.delegate = self;
  32. }
  33. if (viewController.resolveOptionsWithDefault.animations.showModal.hasAnimation) {
  34. _modalTransitionDelegate = [[ModalTransitionDelegate alloc] initWithContentTransition:viewController.resolveOptionsWithDefault.animations.showModal uiManager:_uiManager];
  35. viewController.transitioningDelegate = _modalTransitionDelegate;
  36. viewController.modalPresentationStyle = UIModalPresentationCustom;
  37. }
  38. [topVC presentViewController:viewController animated:animated completion:^{
  39. if (completion) {
  40. completion(nil);
  41. }
  42. [self->_presentedModals addObject:[viewController topMostViewController]];
  43. }];
  44. }
  45. - (void)dismissModal:(UIViewController *)viewController completion:(RNNTransitionCompletionBlock)completion {
  46. if (viewController) {
  47. [_pendingModalIdsToDismiss addObject:viewController];
  48. [self removePendingNextModalIfOnTop:completion];
  49. }
  50. }
  51. - (void)dismissAllModalsAnimated:(BOOL)animated completion:(void (^ __nullable)(void))completion {
  52. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  53. [root dismissViewControllerAnimated:animated completion:completion];
  54. [_delegate dismissedMultipleModals:_presentedModals];
  55. [_pendingModalIdsToDismiss removeAllObjects];
  56. [_presentedModals removeAllObjects];
  57. }
  58. - (void)dismissAllModalsSynchronosly {
  59. if (_presentedModals.count) {
  60. dispatch_semaphore_t sem = dispatch_semaphore_create(0);
  61. [self dismissAllModalsAnimated:NO completion:^{
  62. dispatch_semaphore_signal(sem);
  63. }];
  64. while (dispatch_semaphore_wait(sem, DISPATCH_TIME_NOW)) {
  65. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0]];
  66. }
  67. }
  68. }
  69. #pragma mark - private
  70. - (void)removePendingNextModalIfOnTop:(RNNTransitionCompletionBlock)completion {
  71. UIViewController<RNNLayoutProtocol> *modalToDismiss = [_pendingModalIdsToDismiss lastObject];
  72. RNNNavigationOptions* optionsWithDefault = modalToDismiss.resolveOptionsWithDefault;
  73. if(!modalToDismiss) {
  74. return;
  75. }
  76. UIViewController* topPresentedVC = [self topPresentedVC];
  77. if (optionsWithDefault.animations.dismissModal.hasAnimation) {
  78. _modalTransitionDelegate = [[ModalDismissTransitionDelegate alloc] initWithContentTransition:modalToDismiss.resolveOptionsWithDefault.animations.dismissModal uiManager:_uiManager];
  79. [self topViewControllerParent:modalToDismiss].transitioningDelegate = _modalTransitionDelegate;
  80. }
  81. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  82. [modalToDismiss dismissViewControllerAnimated:[optionsWithDefault.animations.dismissModal.enable getWithDefaultValue:YES] completion:^{
  83. [_pendingModalIdsToDismiss removeObject:modalToDismiss];
  84. if (modalToDismiss.view) {
  85. [self dismissedModal:modalToDismiss];
  86. }
  87. if (completion) {
  88. completion();
  89. }
  90. [self removePendingNextModalIfOnTop:nil];
  91. }];
  92. } else {
  93. [modalToDismiss.view removeFromSuperview];
  94. modalToDismiss.view = nil;
  95. modalToDismiss.getCurrentChild.resolveOptions.animations.dismissModal.enable = [[Bool alloc] initWithBOOL:NO];
  96. [self dismissedModal:modalToDismiss];
  97. if (completion) {
  98. completion();
  99. }
  100. }
  101. }
  102. - (void)dismissedModal:(UIViewController *)viewController {
  103. [_presentedModals removeObject:[viewController topMostViewController]];
  104. [_delegate dismissedModal:viewController.presentedComponentViewController];
  105. }
  106. - (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
  107. [_presentedModals removeObject:presentationController.presentedViewController];
  108. [_delegate dismissedModal:presentationController.presentedViewController.presentedComponentViewController];
  109. }
  110. - (void)presentationControllerDidAttemptToDismiss:(UIPresentationController *)presentationController {
  111. [_delegate attemptedToDismissModal:presentationController.presentedViewController.presentedComponentViewController];
  112. }
  113. -(UIViewController*)topPresentedVC {
  114. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  115. while(root.presentedViewController) {
  116. root = root.presentedViewController;
  117. }
  118. return root;
  119. }
  120. -(UIViewController*)topPresentedVCLeaf {
  121. id root = [self topPresentedVC];
  122. return [root topViewController] ? [root topViewController] : root;
  123. }
  124. - (UIViewController *)topViewControllerParent:(UIViewController *)viewController {
  125. UIViewController* topParent = viewController;
  126. while (topParent.parentViewController) {
  127. topParent = topParent.parentViewController;
  128. }
  129. return topParent;
  130. }
  131. @end