react-native-navigation的迁移库

RNNModalManager.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #import "RNNModalManager.h"
  2. #import "RNNRootViewController.h"
  3. @implementation RNNModalManager {
  4. RNNStore *_store;
  5. RNNTransitionWithComponentIdCompletionBlock _completionBlock;
  6. }
  7. -(instancetype)initWithStore:(RNNStore*)store {
  8. self = [super init];
  9. _store = store;
  10. return self;
  11. }
  12. -(void)showModal:(BOOL)animated {
  13. UIViewController* topVC = [self topPresentedVC];
  14. topVC.definesPresentationContext = YES;
  15. if ([topVC conformsToProtocol:@protocol(RNNRootViewProtocol)]) {
  16. UIViewController<RNNRootViewProtocol> *navigationTopVC = (UIViewController<RNNRootViewProtocol>*)topVC;
  17. RNNNavigationOptions* options = navigationTopVC.getLeafViewController.options;
  18. if (options.animations.showModal.hasCustomAnimation) {
  19. self.toVC.transitioningDelegate = navigationTopVC;
  20. }
  21. }
  22. [topVC presentViewController:self.toVC animated:animated completion:^{
  23. if (_completionBlock) {
  24. _completionBlock(self.toVC.getLeafViewController.componentId);
  25. _completionBlock = nil;
  26. }
  27. self.toVC = nil;
  28. }];
  29. }
  30. -(void)showModal:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  31. self.toVC = (UIViewController<RNNRootViewProtocol>*)viewController;
  32. RNNNavigationOptions* options = self.toVC.getLeafViewController.options;
  33. _completionBlock = completion;
  34. if ([self.toVC respondsToSelector:@selector(applyModalOptions)]) {
  35. [self.toVC.getLeafViewController applyModalOptions];
  36. }
  37. if ([self.toVC respondsToSelector:@selector(isCustomViewController)] &&
  38. [self.toVC.getLeafViewController isCustomViewController]
  39. ) {
  40. [self showModal:animated];
  41. } else {
  42. [self.toVC.getLeafViewController waitForReactViewRender:options.animations.showModal.waitForRender perform:^{
  43. [self showModal:animated];
  44. }];
  45. }
  46. }
  47. - (void)dismissModal:(NSString *)componentId completion:(RNNTransitionCompletionBlock)completion {
  48. [[_store pendingModalIdsToDismiss] addObject:componentId];
  49. [self removePendingNextModalIfOnTop:completion];
  50. }
  51. -(void)dismissAllModals {
  52. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  53. [root dismissViewControllerAnimated:YES completion:nil];
  54. [[_store pendingModalIdsToDismiss] removeAllObjects];
  55. }
  56. #pragma mark - private
  57. -(void)removePendingNextModalIfOnTop:(RNNTransitionCompletionBlock)completion {
  58. NSString *componentId = [[_store pendingModalIdsToDismiss] lastObject];
  59. UIViewController<RNNRootViewProtocol> *modalToDismiss = (UIViewController<RNNRootViewProtocol>*)[_store findComponentForId:componentId];
  60. RNNNavigationOptions* options = modalToDismiss.getLeafViewController.options;
  61. if(!modalToDismiss) {
  62. return;
  63. }
  64. UIViewController* topPresentedVC = [self topPresentedVC];
  65. if ([options.animations.showModal hasCustomAnimation]) {
  66. modalToDismiss.transitioningDelegate = modalToDismiss;
  67. }
  68. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  69. [modalToDismiss dismissViewControllerAnimated:options.animations.dismissModal.enable completion:^{
  70. [[_store pendingModalIdsToDismiss] removeObject:componentId];
  71. [_store removeComponent:componentId];
  72. if (completion) {
  73. completion();
  74. }
  75. [self removePendingNextModalIfOnTop:nil];
  76. }];
  77. } else {
  78. [modalToDismiss.view removeFromSuperview];
  79. modalToDismiss.view = nil;
  80. if (completion) {
  81. completion();
  82. }
  83. }
  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. @end