react-native-navigation的迁移库

RNNModalManager.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #import "RNNModalManager.h"
  2. #import "RNNRootViewController.h"
  3. @implementation RNNModalManager {
  4. NSMutableArray* _pendingModalIdsToDismiss;
  5. NSMutableArray* _presentedModals;
  6. }
  7. -(instancetype)init {
  8. self = [super init];
  9. _pendingModalIdsToDismiss = [[NSMutableArray alloc] init];
  10. _presentedModals = [[NSMutableArray alloc] init];
  11. return self;
  12. }
  13. -(void)showModal:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  14. [self showModal:viewController animated:animated hasCustomAnimation:NO completion:completion];
  15. }
  16. -(void)showModal:(UIViewController *)viewController animated:(BOOL)animated hasCustomAnimation:(BOOL)hasCustomAnimation completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  17. if (!viewController) {
  18. @throw [NSException exceptionWithName:@"ShowUnknownModal" reason:@"showModal called with nil viewController" userInfo:nil];
  19. }
  20. UIViewController* topVC = [self topPresentedVC];
  21. topVC.definesPresentationContext = YES;
  22. if (hasCustomAnimation) {
  23. viewController.transitioningDelegate = (UIViewController<UIViewControllerTransitioningDelegate>*)topVC;
  24. }
  25. [topVC presentViewController:viewController animated:animated completion:^{
  26. if (completion) {
  27. completion(nil);
  28. }
  29. [_presentedModals addObject:viewController.navigationController ? viewController.navigationController : viewController];
  30. }];
  31. }
  32. - (void)dismissModal:(UIViewController *)viewController completion:(RNNTransitionCompletionBlock)completion {
  33. if (viewController) {
  34. [_pendingModalIdsToDismiss addObject:viewController];
  35. [self removePendingNextModalIfOnTop:completion];
  36. }
  37. }
  38. -(void)dismissAllModalsAnimated:(BOOL)animated {
  39. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  40. [root dismissViewControllerAnimated:animated completion:nil];
  41. [_delegate dismissedMultipleModals:_presentedModals];
  42. [_pendingModalIdsToDismiss removeAllObjects];
  43. [_presentedModals removeAllObjects];
  44. }
  45. #pragma mark - private
  46. -(void)removePendingNextModalIfOnTop:(RNNTransitionCompletionBlock)completion {
  47. UIViewController<RNNParentProtocol> *modalToDismiss = [_pendingModalIdsToDismiss lastObject];
  48. RNNNavigationOptions* options = modalToDismiss.getLeafViewController.options;
  49. if(!modalToDismiss) {
  50. return;
  51. }
  52. UIViewController* topPresentedVC = [self topPresentedVC];
  53. if ([options.animations.showModal hasCustomAnimation]) {
  54. modalToDismiss.transitioningDelegate = modalToDismiss;
  55. }
  56. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  57. [modalToDismiss dismissViewControllerAnimated:options.animations.dismissModal.enable completion:^{
  58. [_pendingModalIdsToDismiss removeObject:modalToDismiss];
  59. if (modalToDismiss.view) {
  60. [self dismissedModal:modalToDismiss];
  61. }
  62. if (completion) {
  63. completion();
  64. }
  65. [self removePendingNextModalIfOnTop:nil];
  66. }];
  67. } else {
  68. [modalToDismiss.view removeFromSuperview];
  69. modalToDismiss.view = nil;
  70. modalToDismiss.getLeafViewController.options.animations.dismissModal.enable = NO;
  71. [self dismissedModal:modalToDismiss];
  72. if (completion) {
  73. completion();
  74. }
  75. }
  76. }
  77. - (void)dismissedModal:(UIViewController *)viewController {
  78. [_presentedModals removeObject:viewController.navigationController ? viewController.navigationController : viewController];
  79. [_delegate dismissedModal:viewController];
  80. }
  81. -(UIViewController*)topPresentedVC {
  82. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  83. while(root.presentedViewController) {
  84. root = root.presentedViewController;
  85. }
  86. return root;
  87. }
  88. -(UIViewController*)topPresentedVCLeaf {
  89. id root = [self topPresentedVC];
  90. return [root topViewController] ? [root topViewController] : root;
  91. }
  92. @end