react-native-navigation的迁移库

RNNNavigationStackManager.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #import "RNNNavigationStackManager.h"
  2. #import "RNNRootViewController.h"
  3. #import "React/RCTUIManager.h"
  4. #import "RNNAnimator.h"
  5. dispatch_queue_t RCTGetUIManagerQueue(void);
  6. @implementation RNNNavigationStackManager {
  7. RNNStore *_store;
  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 customAnimationData:(NSDictionary*)customAnimationData bridge:(RCTBridge*)bridge {
  15. UIViewController *vc = [_store findContainerForId:containerId];
  16. [self preparePush:newTop onTopVC:vc customAnimationData:customAnimationData bridge:bridge];
  17. [self waitForContentToAppearAndThen:@selector(pushAfterLoad:)];
  18. }
  19. -(void)preparePush:(UIViewController *)newTop onTopVC:(UIViewController*)vc customAnimationData:(NSDictionary*)customAnimationData bridge:(RCTBridge*)bridge {
  20. if (customAnimationData) {
  21. RNNRootViewController* newTopRootView = (RNNRootViewController*)newTop;
  22. self.fromVC = vc;
  23. self.toVC = newTopRootView;
  24. vc.navigationController.delegate = newTopRootView;
  25. [newTopRootView.animator setupTransition:customAnimationData];
  26. RCTUIManager *uiManager = bridge.uiManager;
  27. CGRect screenBound = [vc.view bounds];
  28. CGSize screenSize = screenBound.size;
  29. [uiManager setAvailableSize:screenSize forRootView:self.toVC.view];
  30. } else {
  31. self.fromVC = vc;
  32. self.toVC = (RNNRootViewController*)newTop;
  33. vc.navigationController.delegate = nil;
  34. self.fromVC.navigationController.interactivePopGestureRecognizer.delegate = nil;
  35. }
  36. }
  37. -(void)waitForContentToAppearAndThen:(SEL)nameOfSelector {
  38. [[NSNotificationCenter defaultCenter] addObserver:self
  39. selector:nameOfSelector
  40. name: @"RCTContentDidAppearNotification"
  41. object:nil];
  42. }
  43. -(void)pushAfterLoad:(NSDictionary*)notif {
  44. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RCTContentDidAppearNotification" object:nil];
  45. [[self.fromVC navigationController] pushViewController:self.toVC animated:YES];
  46. self.toVC = nil;
  47. self.fromVC.navigationController.interactivePopGestureRecognizer.delegate = nil;
  48. self.fromVC = nil;
  49. }
  50. -(void)pop:(NSString *)containerId withAnimationData:(NSDictionary *)animationData {
  51. UIViewController* vc = [_store findContainerForId:containerId];
  52. UINavigationController* nvc = [vc navigationController];
  53. if ([nvc topViewController] == vc) {
  54. if (animationData) {
  55. RNNRootViewController* RNNVC = (RNNRootViewController*)vc;
  56. nvc.delegate = RNNVC;
  57. [RNNVC.animator setupTransition:animationData];
  58. [nvc popViewControllerAnimated:YES];
  59. } else {
  60. nvc.delegate = nil;
  61. [nvc popViewControllerAnimated:YES];
  62. }
  63. } else {
  64. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  65. [vcs removeObject:vc];
  66. [nvc setViewControllers:vcs animated:YES];
  67. }
  68. [_store removeContainer:containerId];
  69. }
  70. -(void)popTo:(NSString*)containerId {
  71. UIViewController *vc = [_store findContainerForId:containerId];
  72. if (vc) {
  73. UINavigationController *nvc = [vc navigationController];
  74. if(nvc) {
  75. NSArray *poppedVCs = [nvc popToViewController:vc animated:YES];
  76. [self removePopedViewControllers:poppedVCs];
  77. }
  78. }
  79. }
  80. -(void) popToRoot:(NSString*)containerId {
  81. UIViewController* vc = [_store findContainerForId:containerId];
  82. UINavigationController* nvc = [vc navigationController];
  83. NSArray* poppedVCs = [nvc popToRootViewControllerAnimated:YES];
  84. [self removePopedViewControllers:poppedVCs];
  85. }
  86. -(void)removePopedViewControllers:(NSArray*)viewControllers {
  87. for (UIViewController *popedVC in viewControllers) {
  88. [_store removeContainerByViewControllerInstance:popedVC];
  89. }
  90. }
  91. @end