react-native-navigation的迁移库

RNNModalManager.m 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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)showModalAfterLoad:(NSDictionary*)notif {
  13. RNNRootViewController *topVC = (RNNRootViewController*)[self topPresentedVC];
  14. topVC.definesPresentationContext = YES;
  15. if (topVC.options.animations.showModal.hasCustomAnimation) {
  16. self.toVC.transitioningDelegate = topVC;
  17. }
  18. [topVC presentViewController:self.toVC animated:self.toVC.options.animations.showModal.enable completion:^{
  19. if (_completionBlock) {
  20. _completionBlock();
  21. _completionBlock = nil;
  22. }
  23. self.toVC = nil;
  24. }];
  25. }
  26. -(void)showModal:(UIViewController *)viewController completion:(RNNTransitionCompletionBlock)completion {
  27. self.toVC = (UIViewController<RNNRootViewProtocol>*)viewController;
  28. _completionBlock = completion;
  29. if ([self.toVC respondsToSelector:@selector(applyModalOptions)]) {
  30. [self.toVC applyModalOptions];
  31. }
  32. if ([self.toVC respondsToSelector:@selector(isCustomViewController)] &&
  33. [self.toVC isCustomViewController]
  34. ) {
  35. [self showModalAfterLoad:nil];
  36. } else {
  37. [self.toVC waitForReactViewRender:self.toVC.options.animations.showModal.waitForRender perform:^{
  38. [self showModalAfterLoad:nil];
  39. }];
  40. }
  41. }
  42. -(void)dismissModal:(NSString *)componentId {
  43. [[_store pendingModalIdsToDismiss] addObject:componentId];
  44. [self removePendingNextModalIfOnTop];
  45. }
  46. -(void)dismissAllModals {
  47. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  48. [root dismissViewControllerAnimated:YES completion:nil];
  49. [[_store pendingModalIdsToDismiss] removeAllObjects];
  50. }
  51. #pragma mark - private
  52. -(void)removePendingNextModalIfOnTop {
  53. NSString *componentId = [[_store pendingModalIdsToDismiss] lastObject];
  54. UIViewController<RNNRootViewProtocol> *modalToDismiss = (UIViewController<RNNRootViewProtocol>*)[_store findComponentForId:componentId];
  55. if(!modalToDismiss) {
  56. return;
  57. }
  58. UIViewController* topPresentedVC = [self topPresentedVC];
  59. if ([modalToDismiss.options.animations.showModal hasCustomAnimation]) {
  60. modalToDismiss.transitioningDelegate = modalToDismiss;
  61. }
  62. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  63. [modalToDismiss dismissViewControllerAnimated:modalToDismiss.options.animations.dismissModal.enable completion:^{
  64. [[_store pendingModalIdsToDismiss] removeObject:componentId];
  65. [_store removeComponent:componentId];
  66. [self removePendingNextModalIfOnTop];
  67. }];
  68. }
  69. }
  70. -(UIViewController*)topPresentedVC {
  71. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  72. while(root.presentedViewController) {
  73. root = root.presentedViewController;
  74. }
  75. return root;
  76. }
  77. -(UIViewController*)topPresentedVCLeaf {
  78. id root = [self topPresentedVC];
  79. return [root topViewController] ? [root topViewController] : root;
  80. }
  81. @end