react-native-navigation的迁移库

RNNModalManager.m 2.3KB

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