react-native-navigation的迁移库

RNNModalManager.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }];
  30. }
  31. -(void)showModal:(UIViewController *)viewController completion:(RNNTransitionCompletionBlock)completion {
  32. self.toVC = (UIViewController<RNNRootViewProtocol>*)viewController;
  33. _completionBlock = completion;
  34. if ([self.toVC respondsToSelector:@selector(applyModalOptions)]) {
  35. [self.toVC applyModalOptions];
  36. }
  37. if ([self.toVC respondsToSelector:@selector(isCustomViewController)] &&
  38. [self.toVC isCustomViewController]
  39. ) {
  40. [self showModalAfterLoad:nil];
  41. } else {
  42. [self waitForContentToAppearAndThen:@selector(showModalAfterLoad:)];
  43. }
  44. }
  45. -(void)dismissModal:(NSString *)componentId {
  46. [[_store pendingModalIdsToDismiss] addObject:componentId];
  47. [self removePendingNextModalIfOnTop];
  48. }
  49. -(void)dismissAllModals {
  50. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  51. [root dismissViewControllerAnimated:YES completion:nil];
  52. [[_store pendingModalIdsToDismiss] removeAllObjects];
  53. }
  54. #pragma mark - private
  55. -(void)removePendingNextModalIfOnTop {
  56. NSString *componentId = [[_store pendingModalIdsToDismiss] lastObject];
  57. UIViewController<RNNRootViewProtocol> *modalToDismiss = (UIViewController<RNNRootViewProtocol>*)[_store findComponentForId:componentId];
  58. if(!modalToDismiss) {
  59. return;
  60. }
  61. UIViewController* topPresentedVC = [self topPresentedVC];
  62. if (modalToDismiss.options.animations.showModal) {
  63. modalToDismiss.transitioningDelegate = modalToDismiss;
  64. }
  65. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  66. [modalToDismiss dismissViewControllerAnimated:modalToDismiss.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