react-native-navigation的迁移库

RNNCommandsHandler.m 9.0KB

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