react-native-navigation的迁移库

RNNNavigationStackManager.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 = (RNNRootViewController*)newTop;
  25. self.fromVC = vc;
  26. if (self.toVC.options.animations.push) {
  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. RNNRootViewController* vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  57. UINavigationController* nvc = [vc navigationController];
  58. if ([nvc topViewController] == vc) {
  59. if (vc.options.animations.pop) {
  60. nvc.delegate = vc;
  61. [nvc popViewControllerAnimated:vc.isAnimated];
  62. } else {
  63. nvc.delegate = nil;
  64. [nvc popViewControllerAnimated:vc.isAnimated];
  65. }
  66. } else {
  67. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  68. [vcs removeObject:vc];
  69. [nvc setViewControllers:vcs animated:YES];
  70. }
  71. [_store removeComponent:componentId];
  72. }
  73. -(void)popTo:(NSString*)componentId {
  74. UIViewController *vc = [_store findComponentForId:componentId];
  75. if (vc) {
  76. UINavigationController *nvc = [vc navigationController];
  77. if(nvc) {
  78. NSArray *poppedVCs = [nvc popToViewController:vc animated:YES];
  79. [self removePopedViewControllers:poppedVCs];
  80. }
  81. }
  82. }
  83. -(void) popToRoot:(NSString*)componentId {
  84. UIViewController* vc = [_store findComponentForId:componentId];
  85. UINavigationController* nvc = [vc navigationController];
  86. NSArray* poppedVCs = [nvc popToRootViewControllerAnimated:YES];
  87. [self removePopedViewControllers:poppedVCs];
  88. }
  89. -(void)removePopedViewControllers:(NSArray*)viewControllers {
  90. for (UIViewController *popedVC in viewControllers) {
  91. [_store removeComponentByViewControllerInstance:popedVC];
  92. }
  93. }
  94. @end