react-native-navigation的迁移库

RNNModalManager.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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)showModalAfterLoad:(NSDictionary*)notif {
  13. UIViewController* topVC = [self topPresentedVC];
  14. topVC.definesPresentationContext = YES;
  15. BOOL animated = true;
  16. if ([topVC conformsToProtocol:@protocol(RNNRootViewProtocol)]) {
  17. UIViewController<RNNRootViewProtocol> *navigationTopVC = (UIViewController<RNNRootViewProtocol>*)topVC;
  18. RNNNavigationOptions* options = navigationTopVC.getLeafViewController.options;
  19. if (options.animations.showModal.hasCustomAnimation) {
  20. self.toVC.transitioningDelegate = navigationTopVC;
  21. }
  22. animated = options.animations.showModal.enable;
  23. }
  24. [topVC presentViewController:self.toVC animated:animated completion:^{
  25. if (_completionBlock) {
  26. _completionBlock();
  27. _completionBlock = nil;
  28. }
  29. self.toVC = nil;
  30. }];
  31. }
  32. -(void)showModal:(UIViewController *)viewController completion:(RNNTransitionCompletionBlock)completion {
  33. self.toVC = (UIViewController<RNNRootViewProtocol>*)viewController;
  34. RNNNavigationOptions* options = self.toVC.getLeafViewController.options;
  35. _completionBlock = completion;
  36. if ([self.toVC respondsToSelector:@selector(applyModalOptions)]) {
  37. [self.toVC.getLeafViewController applyModalOptions];
  38. }
  39. if ([self.toVC respondsToSelector:@selector(isCustomViewController)] &&
  40. [self.toVC.getLeafViewController isCustomViewController]
  41. ) {
  42. [self showModalAfterLoad:nil];
  43. } else {
  44. [self.toVC.getLeafViewController waitForReactViewRender:options.animations.showModal.waitForRender perform:^{
  45. [self showModalAfterLoad:nil];
  46. }];
  47. }
  48. }
  49. -(void)dismissModal:(NSString *)componentId {
  50. [[_store pendingModalIdsToDismiss] addObject:componentId];
  51. [self removePendingNextModalIfOnTop];
  52. }
  53. -(void)dismissAllModals {
  54. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  55. [root dismissViewControllerAnimated:YES completion:nil];
  56. [[_store pendingModalIdsToDismiss] removeAllObjects];
  57. }
  58. #pragma mark - private
  59. -(void)removePendingNextModalIfOnTop {
  60. NSString *componentId = [[_store pendingModalIdsToDismiss] lastObject];
  61. UIViewController<RNNRootViewProtocol> *modalToDismiss = (UIViewController<RNNRootViewProtocol>*)[_store findComponentForId:componentId];
  62. RNNNavigationOptions* options = modalToDismiss.getLeafViewController.options;
  63. if(!modalToDismiss) {
  64. return;
  65. }
  66. UIViewController* topPresentedVC = [self topPresentedVC];
  67. if ([options.animations.showModal hasCustomAnimation]) {
  68. modalToDismiss.transitioningDelegate = modalToDismiss;
  69. }
  70. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  71. [modalToDismiss dismissViewControllerAnimated:options.animations.dismissModal.enable completion:^{
  72. [[_store pendingModalIdsToDismiss] removeObject:componentId];
  73. [_store removeComponent:componentId];
  74. [self removePendingNextModalIfOnTop];
  75. }];
  76. }
  77. }
  78. -(UIViewController*)topPresentedVC {
  79. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  80. while(root.presentedViewController) {
  81. root = root.presentedViewController;
  82. }
  83. return root;
  84. }
  85. -(UIViewController*)topPresentedVCLeaf {
  86. id root = [self topPresentedVC];
  87. return [root topViewController] ? [root topViewController] : root;
  88. }
  89. @end