react-native-navigation的迁移库

RNNModalManager.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #import "RNNModalManager.h"
  2. #import "RNNRootViewController.h"
  3. @implementation RNNModalManager {
  4. RNNStore *_store;
  5. RNNTransitionCompletionBlock _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();
  25. _completionBlock = nil;
  26. }
  27. self.toVC = nil;
  28. }];
  29. }
  30. -(void)showModal:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)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 {
  48. [[_store pendingModalIdsToDismiss] addObject:componentId];
  49. [self removePendingNextModalIfOnTop];
  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 {
  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. [self removePendingNextModalIfOnTop];
  73. }];
  74. }
  75. }
  76. -(UIViewController*)topPresentedVC {
  77. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  78. while(root.presentedViewController) {
  79. root = root.presentedViewController;
  80. }
  81. return root;
  82. }
  83. -(UIViewController*)topPresentedVCLeaf {
  84. id root = [self topPresentedVC];
  85. return [root topViewController] ? [root topViewController] : root;
  86. }
  87. @end