react-native-navigation的迁移库

RNNCommandsHandler.m 4.2KB

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