react-native-navigation的迁移库

RNNNavigationStackManager.m 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. [self waitForContentToAppearAndThen:@selector(pushAfterLoad:)];
  18. }
  19. -(void)preparePush:(UIViewController<RNNRootViewProtocol> *)newTop onTopVC:(UIViewController*)vc completion:(RNNTransitionCompletionBlock)completion {
  20. self.toVC = newTop;
  21. self.fromVC = vc;
  22. if (self.toVC.isCustomTransitioned) {
  23. vc.navigationController.delegate = newTop;
  24. } else {
  25. vc.navigationController.delegate = nil;
  26. self.fromVC.navigationController.interactivePopGestureRecognizer.delegate = nil;
  27. }
  28. _completionBlock = completion;
  29. }
  30. -(void)waitForContentToAppearAndThen:(SEL)nameOfSelector {
  31. [[NSNotificationCenter defaultCenter] addObserver:self
  32. selector:nameOfSelector
  33. name: @"RCTContentDidAppearNotification"
  34. object:nil];
  35. }
  36. -(void)pushAfterLoad:(NSDictionary*)notif {
  37. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RCTContentDidAppearNotification" object:nil];
  38. [CATransaction begin];
  39. [CATransaction setCompletionBlock:^{
  40. if (_completionBlock) {
  41. _completionBlock();
  42. _completionBlock = nil;
  43. }
  44. }];
  45. [[self.fromVC navigationController] pushViewController:self.toVC animated:self.toVC.isAnimated];
  46. [CATransaction commit];
  47. self.toVC = nil;
  48. self.fromVC.navigationController.interactivePopGestureRecognizer.delegate = nil;
  49. self.fromVC = nil;
  50. }
  51. -(void)pop:(NSString *)componentId withTransitionOptions:(RNNTransitionOptions *)transitionOptions {
  52. UIViewController<RNNRootViewProtocol>* vc = (UIViewController<RNNRootViewProtocol>*)[_store findComponentForId:componentId];
  53. UINavigationController* nvc = [vc navigationController];
  54. if ([nvc topViewController] == vc) {
  55. if (transitionOptions) {
  56. RNNRootViewController* RNNVC = (RNNRootViewController*)vc;
  57. nvc.delegate = RNNVC;
  58. RNNVC.animator = [[RNNAnimator alloc] initWithTransitionOptions:transitionOptions];
  59. [nvc popViewControllerAnimated:vc.isAnimated];
  60. } else {
  61. nvc.delegate = nil;
  62. [nvc popViewControllerAnimated:vc.isAnimated];
  63. }
  64. } else {
  65. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  66. [vcs removeObject:vc];
  67. [nvc setViewControllers:vcs animated:YES];
  68. }
  69. [_store removeComponent:componentId];
  70. }
  71. -(void)popTo:(NSString*)componentId {
  72. UIViewController *vc = [_store findComponentForId:componentId];
  73. if (vc) {
  74. UINavigationController *nvc = [vc navigationController];
  75. if(nvc) {
  76. NSArray *poppedVCs = [nvc popToViewController:vc animated:YES];
  77. [self removePopedViewControllers:poppedVCs];
  78. }
  79. }
  80. }
  81. -(void) popToRoot:(NSString*)componentId {
  82. UIViewController* vc = [_store findComponentForId:componentId];
  83. UINavigationController* nvc = [vc navigationController];
  84. NSArray* poppedVCs = [nvc popToRootViewControllerAnimated:YES];
  85. [self removePopedViewControllers:poppedVCs];
  86. }
  87. -(void)removePopedViewControllers:(NSArray*)viewControllers {
  88. for (UIViewController *popedVC in viewControllers) {
  89. [_store removeComponentByViewControllerInstance:popedVC];
  90. }
  91. }
  92. @end