react-native-navigation的迁移库

RNNModalManager.m 4.4KB

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