react-native-navigation的迁移库

RNNModalManager.m 6.0KB

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