react-native-navigation的迁移库

RNNModalManager.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 {
  45. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  46. [root dismissViewControllerAnimated:animated completion:nil];
  47. [_delegate dismissedMultipleModals:_presentedModals];
  48. [_pendingModalIdsToDismiss removeAllObjects];
  49. [_presentedModals removeAllObjects];
  50. }
  51. #pragma mark - private
  52. -(void)removePendingNextModalIfOnTop:(RNNTransitionCompletionBlock)completion {
  53. UIViewController<RNNLayoutProtocol> *modalToDismiss = [_pendingModalIdsToDismiss lastObject];
  54. RNNNavigationOptions* options = modalToDismiss.resolveOptions;
  55. if(!modalToDismiss) {
  56. return;
  57. }
  58. UIViewController* topPresentedVC = [self topPresentedVC];
  59. RNNAnimationsTransitionDelegate* tr = [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:modalToDismiss.resolveOptions.animations.dismissModal isDismiss:YES];
  60. if ([options.animations.dismissModal hasCustomAnimation]) {
  61. [self topViewControllerParent:modalToDismiss].transitioningDelegate = tr;
  62. }
  63. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  64. [modalToDismiss dismissViewControllerAnimated:[options.animations.dismissModal.enable getWithDefaultValue:YES] completion:^{
  65. [_pendingModalIdsToDismiss removeObject:modalToDismiss];
  66. if (modalToDismiss.view) {
  67. [self dismissedModal:modalToDismiss];
  68. }
  69. if (completion) {
  70. completion();
  71. }
  72. [self removePendingNextModalIfOnTop:nil];
  73. }];
  74. } else {
  75. [modalToDismiss.view removeFromSuperview];
  76. modalToDismiss.view = nil;
  77. modalToDismiss.getCurrentChild.resolveOptions.animations.dismissModal.enable = [[Bool alloc] initWithBOOL:NO];
  78. [self dismissedModal:modalToDismiss];
  79. if (completion) {
  80. completion();
  81. }
  82. }
  83. }
  84. - (void)dismissedModal:(UIViewController *)viewController {
  85. [_presentedModals removeObject:viewController.navigationController ? viewController.navigationController : viewController];
  86. [_delegate dismissedModal:viewController];
  87. }
  88. - (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
  89. [_presentedModals removeObject:presentationController.presentedViewController];
  90. }
  91. -(UIViewController*)topPresentedVC {
  92. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  93. while(root.presentedViewController) {
  94. root = root.presentedViewController;
  95. }
  96. return root;
  97. }
  98. -(UIViewController*)topPresentedVCLeaf {
  99. id root = [self topPresentedVC];
  100. return [root topViewController] ? [root topViewController] : root;
  101. }
  102. - (UIViewController *)topViewControllerParent:(UIViewController *)viewController {
  103. UIViewController* topParent = viewController;
  104. while (topParent.parentViewController) {
  105. topParent = topParent.parentViewController;
  106. }
  107. return topParent;
  108. }
  109. @end