react-native-navigation的迁移库

RNNModalManager.m 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. UIViewController *topVC = [self topPresentedVC];
  21. [topVC presentViewController:self.toVC animated:self.toVC.isAnimated completion:^{
  22. if (_completionBlock) {
  23. _completionBlock();
  24. _completionBlock = nil;
  25. }
  26. }];
  27. }
  28. -(void)showModal:(UIViewController *)viewController completion:(RNNTransitionCompletionBlock)completion {
  29. self.toVC = (UIViewController<RNNRootViewProtocol>*)viewController;
  30. _completionBlock = completion;
  31. [self waitForContentToAppearAndThen:@selector(showModalAfterLoad:)];
  32. }
  33. -(void)dismissModal:(NSString *)componentId {
  34. [[_store pendingModalIdsToDismiss] addObject:componentId];
  35. [self removePendingNextModalIfOnTop];
  36. }
  37. -(void)dismissAllModals {
  38. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  39. [root dismissViewControllerAnimated:YES completion:nil];
  40. [[_store pendingModalIdsToDismiss] removeAllObjects];
  41. }
  42. #pragma mark - private
  43. -(void)removePendingNextModalIfOnTop {
  44. NSString *componentId = [[_store pendingModalIdsToDismiss] lastObject];
  45. UIViewController<RNNRootViewProtocol> *modalToDismiss = (UIViewController<RNNRootViewProtocol>*)[_store findComponentForId:componentId];
  46. if(!modalToDismiss) {
  47. return;
  48. }
  49. UIViewController* topPresentedVC = [self topPresentedVC];
  50. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  51. [modalToDismiss dismissViewControllerAnimated:modalToDismiss.isAnimated completion:^{
  52. [[_store pendingModalIdsToDismiss] removeObject:componentId];
  53. [self removePendingNextModalIfOnTop];
  54. }];
  55. }
  56. }
  57. -(UIViewController*)topPresentedVC {
  58. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  59. while(root.presentedViewController) {
  60. root = root.presentedViewController;
  61. }
  62. return root;
  63. }
  64. @end