react-native-navigation的迁移库

RNNModalManager.m 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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)waitForContentToAppearAndThen:(SEL)nameOfSelector {
  13. [[NSNotificationCenter defaultCenter] addObserver:self
  14. selector:nameOfSelector
  15. name: @"RCTContentDidAppearNotification"
  16. object:nil];
  17. }
  18. -(void)showModalAfterLoad:(NSDictionary*)notif {
  19. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RCTContentDidAppearNotification" object:nil];
  20. RNNRootViewController *topVC = (RNNRootViewController*)[self topPresentedVC];
  21. topVC.definesPresentationContext = YES;
  22. if (topVC.options.animations.showModal.hasCustomAnimation) {
  23. self.toVC.transitioningDelegate = topVC;
  24. }
  25. [topVC presentViewController:self.toVC animated:self.toVC.options.animations.showModal.enable completion:^{
  26. if (_completionBlock) {
  27. _completionBlock();
  28. _completionBlock = nil;
  29. }
  30. self.toVC = nil;
  31. }];
  32. }
  33. -(void)showModal:(UIViewController *)viewController completion:(RNNTransitionCompletionBlock)completion {
  34. self.toVC = (UIViewController<RNNRootViewProtocol>*)viewController;
  35. _completionBlock = completion;
  36. if ([self.toVC respondsToSelector:@selector(applyModalOptions)]) {
  37. [self.toVC applyModalOptions];
  38. }
  39. if ([self.toVC respondsToSelector:@selector(isCustomViewController)] &&
  40. [self.toVC isCustomViewController]
  41. ) {
  42. [self showModalAfterLoad:nil];
  43. } else {
  44. [self waitForContentToAppearAndThen:@selector(showModalAfterLoad:)];
  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. if(!modalToDismiss) {
  61. return;
  62. }
  63. UIViewController* topPresentedVC = [self topPresentedVC];
  64. if ([modalToDismiss.options.animations.showModal hasCustomAnimation]) {
  65. modalToDismiss.transitioningDelegate = modalToDismiss;
  66. }
  67. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  68. [modalToDismiss dismissViewControllerAnimated:modalToDismiss.options.animations.dismissModal.enable completion:^{
  69. [[_store pendingModalIdsToDismiss] removeObject:componentId];
  70. [_store removeComponent:componentId];
  71. [self removePendingNextModalIfOnTop];
  72. }];
  73. }
  74. }
  75. -(UIViewController*)topPresentedVC {
  76. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  77. while(root.presentedViewController) {
  78. root = root.presentedViewController;
  79. }
  80. return root;
  81. }
  82. -(UIViewController*)topPresentedVCLeaf {
  83. id root = [self topPresentedVC];
  84. return [root topViewController] ? [root topViewController] : root;
  85. }
  86. @end