react-native-navigation的迁移库

RNNModalManager.m 3.0KB

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