react-native-navigation的迁移库

RNNModalManager.m 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #import "RNNModalManager.h"
  2. #import "RNNComponentViewController.h"
  3. #import "RNNAnimationsTransitionDelegate.h"
  4. #import "RNNLayoutProtocol.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. [_presentedModals addObject:viewController.navigationController ? viewController.navigationController : viewController];
  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.navigationController ? viewController.navigationController : viewController];
  97. [_delegate dismissedModal:viewController];
  98. }
  99. - (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
  100. [_presentedModals removeObject:presentationController.presentedViewController];
  101. }
  102. -(UIViewController*)topPresentedVC {
  103. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  104. while(root.presentedViewController) {
  105. root = root.presentedViewController;
  106. }
  107. return root;
  108. }
  109. -(UIViewController*)topPresentedVCLeaf {
  110. id root = [self topPresentedVC];
  111. return [root topViewController] ? [root topViewController] : root;
  112. }
  113. - (UIViewController *)topViewControllerParent:(UIViewController *)viewController {
  114. UIViewController* topParent = viewController;
  115. while (topParent.parentViewController) {
  116. topParent = topParent.parentViewController;
  117. }
  118. return topParent;
  119. }
  120. @end