react-native-navigation的迁移库

RNNCommandsHandler.m 7.9KB

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