react-native-navigation的迁移库

RNNNavigationStackManager.m 3.4KB

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