react-native-navigation的迁移库

RNNModalManager.m 2.8KB

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