react-native-navigation的迁移库

RNNCommandsHandler.m 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. [_eventEmitter sendOnNavigationCommandCompletion:setRoot params:@{@"layout": layout}];
  51. completion();
  52. }
  53. -(void) mergeOptions:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  54. [self assertReady];
  55. [_eventEmitter sendOnNavigationCommand:mergeOptions params:@{@"componentId": componentId, @"options": options}];
  56. UIViewController* vc = [_store findComponentForId:componentId];
  57. if([vc isKindOfClass:[RNNRootViewController class]]) {
  58. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  59. [rootVc.options mergeWith:options];
  60. [CATransaction begin];
  61. [CATransaction setCompletionBlock:completion];
  62. [rootVc.options applyOn:vc];
  63. [CATransaction commit];
  64. }
  65. if ([vc isKindOfClass:[RNNSplitViewController class]]) {
  66. RNNSplitViewController* splitVc = (RNNSplitViewController*)vc;
  67. [splitVc.options mergeWith:options];
  68. [CATransaction begin];
  69. [CATransaction setCompletionBlock:completion];
  70. [splitVc.options applyOn:vc];
  71. [CATransaction commit];
  72. }
  73. }
  74. -(void) setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
  75. [self assertReady];
  76. [_eventEmitter sendOnNavigationCommand:setDefaultOptions params:@{@"options": optionsDict}];
  77. [_controllerFactory setDefaultOptionsDict:optionsDict];
  78. }
  79. -(void)push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  80. [self assertReady];
  81. UIViewController<RNNRootViewProtocol, UIViewControllerPreviewingDelegate> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  82. if (newVc.options.preview.elementId) {
  83. UIViewController* vc = [_store findComponentForId:componentId];
  84. if([vc isKindOfClass:[RNNRootViewController class]]) {
  85. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  86. rootVc.previewController = newVc;
  87. RNNElementFinder* elementFinder = [[RNNElementFinder alloc] initWithFromVC:vc];
  88. RNNElementView* elementView = [elementFinder findElementForId:newVc.options.preview.elementId];
  89. CGSize size = CGSizeMake(rootVc.view.frame.size.width, rootVc.view.frame.size.height);
  90. if (newVc.options.preview.width) {
  91. size.width = [newVc.options.preview.width floatValue];
  92. }
  93. if (newVc.options.preview.height) {
  94. size.height = [newVc.options.preview.height floatValue];
  95. }
  96. if (newVc.options.preview.width || newVc.options.preview.height) {
  97. newVc.preferredContentSize = size;
  98. }
  99. [rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:elementView];
  100. }
  101. } else {
  102. [_navigationStackManager push:newVc onTop:componentId completion:^{
  103. [_eventEmitter sendOnNavigationCommandCompletion:push params:@{@"componentId": componentId}];
  104. completion();
  105. } rejection:rejection];
  106. }
  107. }
  108. -(void)setStackRoot:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  109. [self assertReady];
  110. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  111. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  112. [_navigationStackManager setStackRoot:newVc fromComponent:componentId completion:^{
  113. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot params:@{@"componentId": componentId}];
  114. completion();
  115. } rejection:rejection];
  116. }
  117. -(void)pop:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  118. [self assertReady];
  119. [CATransaction begin];
  120. [CATransaction setCompletionBlock:^{
  121. [_eventEmitter sendOnNavigationCommandCompletion:pop params:@{@"componentId": componentId}];
  122. completion();
  123. }];
  124. NSDictionary* animationData = options[@"customTransition"];
  125. RNNAnimationOptions* transitionOptions = [[RNNAnimationOptions alloc] initWithDict:animationData];
  126. if (transitionOptions.animations){
  127. [_navigationStackManager pop:componentId withTransitionOptions:transitionOptions rejection:rejection];
  128. } else {
  129. [_navigationStackManager pop:componentId withTransitionOptions:nil rejection:rejection];
  130. }
  131. [CATransaction commit];
  132. }
  133. -(void) popTo:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  134. [self assertReady];
  135. [CATransaction begin];
  136. [CATransaction setCompletionBlock:^{
  137. [_eventEmitter sendOnNavigationCommandCompletion:popTo params:@{@"componentId": componentId}];
  138. completion();
  139. }];
  140. [_navigationStackManager popTo:componentId rejection:rejection];
  141. [CATransaction commit];
  142. }
  143. -(void) popToRoot:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  144. [self assertReady];
  145. [CATransaction begin];
  146. [CATransaction setCompletionBlock:^{
  147. [_eventEmitter sendOnNavigationCommandCompletion:popToRoot params:@{@"componentId": componentId}];
  148. completion();
  149. }];
  150. [_navigationStackManager popToRoot:componentId rejection:rejection];
  151. [CATransaction commit];
  152. }
  153. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  154. [self assertReady];
  155. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  156. [_modalManager showModal:newVc completion:^{
  157. [_eventEmitter sendOnNavigationCommandCompletion:showModal params:@{@"layout": layout}];
  158. completion();
  159. }];
  160. }
  161. -(void) dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  162. [self assertReady];
  163. [CATransaction begin];
  164. [CATransaction setCompletionBlock:^{
  165. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  166. completion();
  167. }];
  168. [_modalManager dismissModal:componentId];
  169. [CATransaction commit];
  170. }
  171. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  172. [self assertReady];
  173. [CATransaction begin];
  174. [CATransaction setCompletionBlock:^{
  175. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals params:@{}];
  176. completion();
  177. }];
  178. [_modalManager dismissAllModals];
  179. [CATransaction commit];
  180. }
  181. -(void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  182. [self assertReady];
  183. UIViewController<RNNRootViewProtocol>* overlayVC = [_controllerFactory createOverlay:layout];
  184. [_overlayManager showOverlay:overlayVC completion:^{
  185. [_eventEmitter sendOnNavigationCommandCompletion:showOverlay params:@{@"layout": layout}];
  186. completion();
  187. }];
  188. }
  189. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  190. [self assertReady];
  191. [_overlayManager dismissOverlay:componentId completion:^{
  192. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  193. completion();
  194. }];
  195. }
  196. #pragma mark - private
  197. -(void) assertReady {
  198. if (!_store.isReadyToReceiveCommands) {
  199. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  200. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  201. userInfo:nil]
  202. raise];
  203. }
  204. }
  205. @end