react-native-navigation的迁移库

RNNCommandsHandler.m 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #import "RNNCommandsHandler.h"
  2. #import "RNNModalManager.h"
  3. #import "RNNNavigationStackManager.h"
  4. #import "RNNOverlayManager.h"
  5. #import "RNNNavigationOptions.h"
  6. #import "RNNRootViewController.h"
  7. #import "React/RCTUIManager.h"
  8. static NSString* const setRoot = @"setRoot";
  9. static NSString* const setStackRoot = @"setStackRoot";
  10. static NSString* const push = @"push";
  11. static NSString* const pop = @"pop";
  12. static NSString* const popTo = @"popTo";
  13. static NSString* const popToRoot = @"popToRoot";
  14. static NSString* const showModal = @"showModal";
  15. static NSString* const dismissModal = @"dismissModal";
  16. static NSString* const dismissAllModals = @"dismissAllModals";
  17. static NSString* const showOverlay = @"showOverlay";
  18. static NSString* const dismissOverlay = @"dismissOverlay";
  19. static NSString* const mergeOptions = @"mergeOptions";
  20. static NSString* const setDefaultOptions = @"setDefaultOptions";
  21. @implementation RNNCommandsHandler {
  22. RNNControllerFactory *_controllerFactory;
  23. RNNStore *_store;
  24. RNNNavigationStackManager* _navigationStackManager;
  25. RNNModalManager* _modalManager;
  26. RNNOverlayManager* _overlayManager;
  27. RNNEventEmitter* _eventEmitter;
  28. }
  29. -(instancetype) initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter {
  30. self = [super init];
  31. _store = store;
  32. _controllerFactory = controllerFactory;
  33. _eventEmitter = eventEmitter;
  34. _navigationStackManager = [[RNNNavigationStackManager alloc] initWithStore:_store];
  35. _modalManager = [[RNNModalManager alloc] initWithStore:_store];
  36. _overlayManager = [[RNNOverlayManager alloc] initWithStore:_store];
  37. return self;
  38. }
  39. #pragma mark - public
  40. -(void) setRoot:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  41. [self assertReady];
  42. [_modalManager dismissAllModals];
  43. [_eventEmitter sendOnNavigationCommand:setRoot params:@{@"layout": layout}];
  44. UIViewController *vc = [_controllerFactory createLayoutAndSaveToStore:layout];
  45. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  46. [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
  47. completion();
  48. }
  49. -(void) mergeOptions:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  50. [self assertReady];
  51. [_eventEmitter sendOnNavigationCommand:mergeOptions params:@{@"componentId": componentId, @"options": options}];
  52. UIViewController* vc = [_store findComponentForId:componentId];
  53. if([vc isKindOfClass:[RNNRootViewController class]]) {
  54. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  55. [rootVc.options mergeWith:options];
  56. [CATransaction begin];
  57. [CATransaction setCompletionBlock:completion];
  58. if (rootVc.isViewLoaded && rootVc.view.window) {
  59. [rootVc.options applyOn:vc];
  60. }
  61. [CATransaction commit];
  62. }
  63. }
  64. -(void) setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
  65. [self assertReady];
  66. [_eventEmitter sendOnNavigationCommand:setDefaultOptions params:@{@"options": optionsDict}];
  67. [_controllerFactory setDefaultOptionsDict:optionsDict];
  68. }
  69. -(void)push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  70. [self assertReady];
  71. [_eventEmitter sendOnNavigationCommand:push params:@{@"componentId": componentId}];
  72. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  73. [_navigationStackManager push:newVc onTop:componentId completion:^{
  74. completion();
  75. } rejection:rejection];
  76. }
  77. -(void)setStackRoot:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  78. [self assertReady];
  79. [_eventEmitter sendOnNavigationCommand:setStackRoot params:@{@"componentId": componentId}];
  80. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  81. [_navigationStackManager setStackRoot:newVc fromComponent:componentId completion:^{
  82. completion();
  83. } rejection:rejection];
  84. }
  85. -(void)pop:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  86. [self assertReady];
  87. [_eventEmitter sendOnNavigationCommand:pop params:@{@"componentId": componentId}];
  88. [CATransaction begin];
  89. [CATransaction setCompletionBlock:^{
  90. completion();
  91. }];
  92. NSDictionary* animationData = options[@"customTransition"];
  93. RNNAnimationOptions* transitionOptions = [[RNNAnimationOptions alloc] initWithDict:animationData];
  94. if (transitionOptions.animations){
  95. [_navigationStackManager pop:componentId withTransitionOptions:transitionOptions rejection:rejection];
  96. } else {
  97. [_navigationStackManager pop:componentId withTransitionOptions:nil rejection:rejection];
  98. }
  99. [CATransaction commit];
  100. }
  101. -(void) popTo:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  102. [self assertReady];
  103. [_eventEmitter sendOnNavigationCommand:popTo params:@{@"componentId": componentId}];
  104. [CATransaction begin];
  105. [CATransaction setCompletionBlock:^{
  106. completion();
  107. }];
  108. [_navigationStackManager popTo:componentId rejection:rejection];
  109. [CATransaction commit];
  110. }
  111. -(void) popToRoot:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  112. [self assertReady];
  113. [_eventEmitter sendOnNavigationCommand:popToRoot params:@{@"componentId": componentId}];
  114. [CATransaction begin];
  115. [CATransaction setCompletionBlock:^{
  116. completion();
  117. }];
  118. [_navigationStackManager popToRoot:componentId rejection:rejection];
  119. [CATransaction commit];
  120. }
  121. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  122. [self assertReady];
  123. [_eventEmitter sendOnNavigationCommand:showModal params:@{@"layout": layout}];
  124. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  125. [_modalManager showModal:newVc completion:^{
  126. completion();
  127. }];
  128. }
  129. -(void) dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  130. [self assertReady];
  131. [_eventEmitter sendOnNavigationCommand:dismissModal params:@{@"componentId": componentId}];
  132. [CATransaction begin];
  133. [CATransaction setCompletionBlock:^{
  134. completion();
  135. }];
  136. [_modalManager dismissModal:componentId];
  137. [CATransaction commit];
  138. }
  139. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  140. [self assertReady];
  141. [_eventEmitter sendOnNavigationCommand:dismissAllModals params:@{}];
  142. [CATransaction begin];
  143. [CATransaction setCompletionBlock:^{
  144. completion();
  145. }];
  146. [_modalManager dismissAllModals];
  147. [CATransaction commit];
  148. }
  149. -(void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  150. [self assertReady];
  151. [_eventEmitter sendOnNavigationCommand:showOverlay params:@{@"layout": layout}];
  152. UIViewController<RNNRootViewProtocol>* overlayVC = [_controllerFactory createOverlay:layout];
  153. [_overlayManager showOverlay:overlayVC completion:^{
  154. completion();
  155. }];
  156. }
  157. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  158. [self assertReady];
  159. [_eventEmitter sendOnNavigationCommand:dismissModal params:@{@"componentId": componentId}];
  160. [_overlayManager dismissOverlay:componentId completion:^{
  161. completion();
  162. }];
  163. }
  164. #pragma mark - private
  165. -(void) assertReady {
  166. if (!_store.isReadyToReceiveCommands) {
  167. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  168. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  169. userInfo:nil]
  170. raise];
  171. }
  172. }
  173. @end