react-native-navigation的迁移库

RNNModalManager.m 5.2KB

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