react-native-navigation的迁移库

RNNCommandsHandler.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #import "RNNCommandsHandler.h"
  2. #import "RNNModalManager.h"
  3. #import "RNNNavigationStackManager.h"
  4. #import "RNNNavigationOptions.h"
  5. #import "RNNRootViewController.h"
  6. #import "React/RCTUIManager.h"
  7. @implementation RNNCommandsHandler {
  8. RNNControllerFactory *_controllerFactory;
  9. RNNStore *_store;
  10. RCTBridge* _bridge;
  11. RNNNavigationStackManager* _navigationStackManager;
  12. RNNModalManager* _modalManager;
  13. }
  14. -(instancetype) initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory andBridge:(RCTBridge*)bridge {
  15. self = [super init];
  16. _bridge = bridge;
  17. _store = store;
  18. _controllerFactory = controllerFactory;
  19. _navigationStackManager = [[RNNNavigationStackManager alloc] initWithStore:_store];
  20. _modalManager = [[RNNModalManager alloc] initWithStore:_store];
  21. return self;
  22. }
  23. #pragma mark - public
  24. -(void) setRoot:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  25. [self assertReady];
  26. [_modalManager dismissAllModals];
  27. UIViewController *vc = [_controllerFactory createLayoutAndSaveToStore:layout];
  28. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  29. [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
  30. completion();
  31. }
  32. -(void) setOptions:(NSString*)containerId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  33. [self assertReady];
  34. UIViewController* vc = [_store findContainerForId:containerId];
  35. if([vc isKindOfClass:[RNNRootViewController class]]) {
  36. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  37. [rootVc.navigationOptions mergeWith:options];
  38. [CATransaction begin];
  39. [CATransaction setCompletionBlock:completion];
  40. [rootVc.navigationOptions applyOn:vc];
  41. [rootVc applyNavigationButtons];
  42. [CATransaction commit];
  43. }
  44. }
  45. -(void) push:(NSString*)containerId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  46. [self assertReady];
  47. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  48. UIViewController *fromVc = [_store findContainerForId:containerId];
  49. [_bridge.uiManager setAvailableSize:fromVc.view.bounds.size forRootView:newVc.view];
  50. [_navigationStackManager push:newVc onTop:containerId completion:completion];
  51. }
  52. -(void)pop:(NSString*)containerId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  53. [self assertReady];
  54. [CATransaction begin];
  55. [CATransaction setCompletionBlock:completion];
  56. NSDictionary* animationData = options[@"customTransition"];
  57. if (animationData){
  58. if ([animationData objectForKey:@"animations"]) {
  59. [_navigationStackManager pop:containerId withAnimationData:animationData];
  60. } else {
  61. [[NSException exceptionWithName:NSInvalidArgumentException reason:@"unsupported transitionAnimation" userInfo:nil] raise];
  62. }
  63. } else {
  64. [_navigationStackManager pop:containerId withAnimationData:nil];
  65. }
  66. [CATransaction commit];
  67. }
  68. -(void) popTo:(NSString*)containerId completion:(RNNTransitionCompletionBlock)completion {
  69. [self assertReady];
  70. [CATransaction begin];
  71. [CATransaction setCompletionBlock:completion];
  72. [_navigationStackManager popTo:containerId];
  73. [CATransaction commit];
  74. }
  75. -(void) popToRoot:(NSString*)containerId completion:(RNNTransitionCompletionBlock)completion {
  76. [self assertReady];
  77. [CATransaction begin];
  78. [CATransaction setCompletionBlock:completion];
  79. [_navigationStackManager popToRoot:containerId];
  80. [CATransaction commit];
  81. }
  82. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  83. [self assertReady];
  84. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  85. [_modalManager showModal:newVc completion:completion];
  86. }
  87. -(void) dismissModal:(NSString*)containerId completion:(RNNTransitionCompletionBlock)completion {
  88. [self assertReady];
  89. [CATransaction begin];
  90. [CATransaction setCompletionBlock:completion];
  91. [_modalManager dismissModal:containerId];
  92. [CATransaction commit];
  93. }
  94. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  95. [self assertReady];
  96. [CATransaction begin];
  97. [CATransaction setCompletionBlock:completion];
  98. [_modalManager dismissAllModals];
  99. [CATransaction commit];
  100. }
  101. #pragma mark - private
  102. -(void) assertReady {
  103. if (!_store.isReadyToReceiveCommands) {
  104. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  105. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  106. userInfo:nil]
  107. raise];
  108. }
  109. }
  110. @end