react-native-navigation的迁移库

RNNModalManager.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #import "RNNModalManager.h"
  2. #import "RNNRootViewController.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. RNNAnimationsTransitionDelegate* tr = [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:viewController.resolveOptions.animations.showModal isDismiss:NO];
  25. if (hasCustomAnimation) {
  26. viewController.transitioningDelegate = tr;
  27. }
  28. [topVC presentViewController:viewController animated:animated completion:^{
  29. if (completion) {
  30. completion(nil);
  31. }
  32. [_presentedModals addObject:viewController.navigationController ? viewController.navigationController : viewController];
  33. }];
  34. }
  35. - (void)dismissModal:(UIViewController *)viewController completion:(RNNTransitionCompletionBlock)completion {
  36. if (viewController) {
  37. [_pendingModalIdsToDismiss addObject:viewController];
  38. [self removePendingNextModalIfOnTop:completion];
  39. }
  40. }
  41. -(void)dismissAllModalsAnimated:(BOOL)animated {
  42. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  43. [root dismissViewControllerAnimated:animated completion:nil];
  44. [_delegate dismissedMultipleModals:_presentedModals];
  45. [_pendingModalIdsToDismiss removeAllObjects];
  46. [_presentedModals removeAllObjects];
  47. }
  48. #pragma mark - private
  49. -(void)removePendingNextModalIfOnTop:(RNNTransitionCompletionBlock)completion {
  50. UIViewController<RNNLayoutProtocol> *modalToDismiss = [_pendingModalIdsToDismiss lastObject];
  51. RNNNavigationOptions* options = modalToDismiss.resolveOptions;
  52. if(!modalToDismiss) {
  53. return;
  54. }
  55. UIViewController* topPresentedVC = [self topPresentedVC];
  56. RNNAnimationsTransitionDelegate* tr = [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:modalToDismiss.resolveOptions.animations.dismissModal isDismiss:YES];
  57. if ([options.animations.dismissModal hasCustomAnimation]) {
  58. [self topViewControllerParent:modalToDismiss].transitioningDelegate = tr;
  59. }
  60. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  61. [modalToDismiss dismissViewControllerAnimated:[options.animations.dismissModal.enable getWithDefaultValue:YES] completion:^{
  62. [_pendingModalIdsToDismiss removeObject:modalToDismiss];
  63. if (modalToDismiss.view) {
  64. [self dismissedModal:modalToDismiss];
  65. }
  66. if (completion) {
  67. completion();
  68. }
  69. [self removePendingNextModalIfOnTop:nil];
  70. }];
  71. } else {
  72. [modalToDismiss.view removeFromSuperview];
  73. modalToDismiss.view = nil;
  74. modalToDismiss.getCurrentChild.resolveOptions.animations.dismissModal.enable = [[Bool alloc] initWithBOOL:NO];
  75. [self dismissedModal:modalToDismiss];
  76. if (completion) {
  77. completion();
  78. }
  79. }
  80. }
  81. - (void)dismissedModal:(UIViewController *)viewController {
  82. [_presentedModals removeObject:viewController.navigationController ? viewController.navigationController : viewController];
  83. [_delegate dismissedModal:viewController];
  84. }
  85. -(UIViewController*)topPresentedVC {
  86. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  87. while(root.presentedViewController) {
  88. root = root.presentedViewController;
  89. }
  90. return root;
  91. }
  92. -(UIViewController*)topPresentedVCLeaf {
  93. id root = [self topPresentedVC];
  94. return [root topViewController] ? [root topViewController] : root;
  95. }
  96. - (UIViewController *)topViewControllerParent:(UIViewController *)viewController {
  97. UIViewController* topParent = viewController;
  98. while (topParent.parentViewController) {
  99. topParent = topParent.parentViewController;
  100. }
  101. return topParent;
  102. }
  103. @end