react-native-navigation的迁移库

RNNModalManager.m 3.2KB

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