react-native-navigation的迁移库

RNNModalManager.m 3.3KB

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