react-native-navigation的迁移库

RNNNavigationStackManager.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #import "RNNNavigationStackManager.h"
  2. #import "RNNRootViewController.h"
  3. #import "RNNAnimator.h"
  4. dispatch_queue_t RCTGetUIManagerQueue(void);
  5. @implementation RNNNavigationStackManager {
  6. RNNStore *_store;
  7. RNNTransitionCompletionBlock _completionBlock;
  8. }
  9. -(instancetype)initWithStore:(RNNStore*)store {
  10. self = [super init];
  11. _store = store;
  12. return self;
  13. }
  14. -(void)push:(UIViewController<RNNRootViewProtocol> *)newTop onTop:(NSString *)componentId completion:(RNNTransitionCompletionBlock)completion {
  15. UIViewController *vc = [_store findComponentForId:componentId];
  16. [self preparePush:newTop onTopVC:vc completion:completion];
  17. if ([newTop isCustomViewController]) {
  18. [self pushAfterLoad:nil];
  19. } else {
  20. [self waitForContentToAppearAndThen:@selector(pushAfterLoad:)];
  21. }
  22. }
  23. -(void)preparePush:(UIViewController<RNNRootViewProtocol> *)newTop onTopVC:(UIViewController*)vc completion:(RNNTransitionCompletionBlock)completion {
  24. self.toVC = newTop;
  25. self.fromVC = vc;
  26. if (self.toVC.isCustomTransitioned) {
  27. vc.navigationController.delegate = newTop;
  28. } else {
  29. vc.navigationController.delegate = nil;
  30. self.fromVC.navigationController.interactivePopGestureRecognizer.delegate = nil;
  31. }
  32. _completionBlock = completion;
  33. }
  34. -(void)waitForContentToAppearAndThen:(SEL)nameOfSelector {
  35. [[NSNotificationCenter defaultCenter] addObserver:self
  36. selector:nameOfSelector
  37. name: @"RCTContentDidAppearNotification"
  38. object:nil];
  39. }
  40. -(void)pushAfterLoad:(NSDictionary*)notif {
  41. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RCTContentDidAppearNotification" object:nil];
  42. [CATransaction begin];
  43. [CATransaction setCompletionBlock:^{
  44. if (_completionBlock) {
  45. _completionBlock();
  46. _completionBlock = nil;
  47. }
  48. }];
  49. [[self.fromVC navigationController] pushViewController:self.toVC animated:self.toVC.isAnimated];
  50. [CATransaction commit];
  51. self.toVC = nil;
  52. self.fromVC.navigationController.interactivePopGestureRecognizer.delegate = nil;
  53. self.fromVC = nil;
  54. }
  55. -(void)pop:(NSString *)componentId withTransitionOptions:(RNNAnimationOptions *)transitionOptions {
  56. UIViewController<RNNRootViewProtocol>* vc = (UIViewController<RNNRootViewProtocol>*)[_store findComponentForId:componentId];
  57. UINavigationController* nvc = [vc navigationController];
  58. if ([nvc topViewController] == vc) {
  59. if (transitionOptions) {
  60. RNNRootViewController* RNNVC = (RNNRootViewController*)vc;
  61. nvc.delegate = RNNVC;
  62. RNNVC.animator = [[RNNAnimator alloc] initWithTransitionOptions:transitionOptions];
  63. [nvc popViewControllerAnimated:vc.isAnimated];
  64. } else {
  65. nvc.delegate = nil;
  66. [nvc popViewControllerAnimated:vc.isAnimated];
  67. }
  68. } else {
  69. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  70. [vcs removeObject:vc];
  71. [nvc setViewControllers:vcs animated:YES];
  72. }
  73. [_store removeComponent:componentId];
  74. }
  75. -(void)popTo:(NSString*)componentId {
  76. UIViewController *vc = [_store findComponentForId:componentId];
  77. if (vc) {
  78. UINavigationController *nvc = [vc navigationController];
  79. if(nvc) {
  80. NSArray *poppedVCs = [nvc popToViewController:vc animated:YES];
  81. [self removePopedViewControllers:poppedVCs];
  82. }
  83. }
  84. }
  85. -(void) popToRoot:(NSString*)componentId {
  86. UIViewController* vc = [_store findComponentForId:componentId];
  87. UINavigationController* nvc = [vc navigationController];
  88. NSArray* poppedVCs = [nvc popToRootViewControllerAnimated:YES];
  89. [self removePopedViewControllers:poppedVCs];
  90. }
  91. -(void)removePopedViewControllers:(NSArray*)viewControllers {
  92. for (UIViewController *popedVC in viewControllers) {
  93. [_store removeComponentByViewControllerInstance:popedVC];
  94. }
  95. }
  96. @end