react-native-navigation的迁移库

RNNCommandsHandler.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #import "RNNErrorHandler.h"
  11. static NSString* const setRoot = @"setRoot";
  12. static NSString* const setStackRoot = @"setStackRoot";
  13. static NSString* const push = @"push";
  14. static NSString* const preview = @"preview";
  15. static NSString* const pop = @"pop";
  16. static NSString* const popTo = @"popTo";
  17. static NSString* const popToRoot = @"popToRoot";
  18. static NSString* const showModal = @"showModal";
  19. static NSString* const dismissModal = @"dismissModal";
  20. static NSString* const dismissAllModals = @"dismissAllModals";
  21. static NSString* const showOverlay = @"showOverlay";
  22. static NSString* const dismissOverlay = @"dismissOverlay";
  23. static NSString* const mergeOptions = @"mergeOptions";
  24. static NSString* const setDefaultOptions = @"setDefaultOptions";
  25. @implementation RNNCommandsHandler {
  26. RNNControllerFactory *_controllerFactory;
  27. RNNStore *_store;
  28. RNNModalManager* _modalManager;
  29. RNNOverlayManager* _overlayManager;
  30. RNNNavigationStackManager* _stackManager;
  31. RNNEventEmitter* _eventEmitter;
  32. }
  33. -(instancetype) initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter {
  34. self = [super init];
  35. _store = store;
  36. _controllerFactory = controllerFactory;
  37. _eventEmitter = eventEmitter;
  38. _modalManager = [[RNNModalManager alloc] initWithStore:_store];
  39. _stackManager = [[RNNNavigationStackManager alloc] init];
  40. _overlayManager = [[RNNOverlayManager alloc] init];
  41. return self;
  42. }
  43. #pragma mark - public
  44. -(void) setRoot:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  45. [self assertReady];
  46. [_modalManager dismissAllModals];
  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. 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. [_controllerFactory setDefaultOptionsDict:optionsDict];
  76. }
  77. -(void)push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  78. [self assertReady];
  79. RNNRootViewController *newVc = (RNNRootViewController *)[_controllerFactory createLayoutAndSaveToStore:layout];
  80. UIViewController *fromVC = [_store findComponentForId:componentId];
  81. if ([newVc.options.preview.reactTag floatValue] > 0) {
  82. UIViewController* vc = [_store findComponentForId:componentId];
  83. if([vc isKindOfClass:[RNNRootViewController class]]) {
  84. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  85. rootVc.previewController = newVc;
  86. rootVc.previewCallback = ^(UIViewController *vcc) {
  87. RNNRootViewController* rvc = (RNNRootViewController*)vcc;
  88. [self->_eventEmitter sendOnPreviewCompleted:componentId previewComponentId:newVc.componentId];
  89. if ([newVc.options.preview.commit floatValue] > 0) {
  90. [CATransaction begin];
  91. [CATransaction setCompletionBlock:^{
  92. [self->_eventEmitter sendOnNavigationCommandCompletion:push params:@{@"componentId": componentId}];
  93. completion();
  94. }];
  95. [rvc.navigationController pushViewController:newVc animated:YES];
  96. [CATransaction commit];
  97. }
  98. };
  99. CGSize size = CGSizeMake(rootVc.view.frame.size.width, rootVc.view.frame.size.height);
  100. if (newVc.options.preview.width) {
  101. size.width = [newVc.options.preview.width floatValue];
  102. }
  103. if (newVc.options.preview.height) {
  104. size.height = [newVc.options.preview.height floatValue];
  105. }
  106. if (newVc.options.preview.width || newVc.options.preview.height) {
  107. newVc.preferredContentSize = size;
  108. }
  109. RCTExecuteOnMainQueue(^{
  110. UIView *view = [[ReactNativeNavigation getBridge].uiManager viewForReactTag:newVc.options.preview.reactTag];
  111. [rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:view];
  112. });
  113. }
  114. } else {
  115. id animationDelegate = (newVc.options.animations.push.hasCustomAnimation || newVc.isCustomTransitioned) ? newVc : nil;
  116. [newVc waitForReactViewRender:(newVc.options.animations.push.waitForRender || animationDelegate) perform:^{
  117. [_stackManager push:newVc onTop:fromVC animated:newVc.options.animations.push.enable animationDelegate:animationDelegate completion:^{
  118. [_eventEmitter sendOnNavigationCommandCompletion:push params:@{@"componentId": componentId}];
  119. completion();
  120. } rejection:rejection];
  121. }];
  122. }
  123. }
  124. -(void)setStackRoot:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  125. [self assertReady];
  126. UIViewController<RNNRootViewProtocol> *newVC = [_controllerFactory createLayoutAndSaveToStore:layout];
  127. RNNNavigationOptions* options = [newVC getLeafViewController].options;
  128. UIViewController *fromVC = [_store findComponentForId:componentId];
  129. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  130. [_stackManager setStackRoot:newVC fromViewController:fromVC animated:options.animations.push.enable completion:^{
  131. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot params:@{@"componentId": componentId}];
  132. completion();
  133. } rejection:rejection];
  134. }
  135. -(void)pop:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  136. [self assertReady];
  137. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  138. UINavigationController *nvc = vc.navigationController;
  139. if ([nvc topViewController] == vc) {
  140. if (vc.options.animations.pop) {
  141. nvc.delegate = vc;
  142. } else {
  143. nvc.delegate = nil;
  144. }
  145. } else {
  146. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  147. [vcs removeObject:vc];
  148. [nvc setViewControllers:vcs animated:vc.options.animations.pop.enable];
  149. }
  150. [_stackManager pop:vc animated:vc.options.animations.pop.enable completion:^{
  151. [_store removeComponent:componentId];
  152. [_eventEmitter sendOnNavigationCommandCompletion:pop params:@{@"componentId": componentId}];
  153. completion();
  154. } rejection:^(NSString *code, NSString *message, NSError *error) {
  155. }];
  156. }
  157. -(void) popTo:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  158. [self assertReady];
  159. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  160. [_stackManager popTo:vc animated:vc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  161. [_eventEmitter sendOnNavigationCommandCompletion:popTo params:@{@"componentId": componentId}];
  162. [self removePopedViewControllers:poppedViewControllers];
  163. completion();
  164. } rejection:rejection];
  165. }
  166. -(void) popToRoot:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  167. [self assertReady];
  168. RNNRootViewController *newVc = (RNNRootViewController*)[_store findComponentForId:componentId];
  169. [CATransaction begin];
  170. [CATransaction setCompletionBlock:^{
  171. [_eventEmitter sendOnNavigationCommandCompletion:popToRoot params:@{@"componentId": componentId}];
  172. completion();
  173. }];
  174. [_stackManager popToRoot:newVc animated:newVc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  175. [self removePopedViewControllers:poppedViewControllers];
  176. } rejection:^(NSString *code, NSString *message, NSError *error) {
  177. }];
  178. [CATransaction commit];
  179. }
  180. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  181. [self assertReady];
  182. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  183. [_modalManager showModal:newVc animated:newVc.getLeafViewController.options.animations.showModal.enable completion:^{
  184. [_eventEmitter sendOnNavigationCommandCompletion:showModal params:@{@"layout": layout}];
  185. completion();
  186. }];
  187. }
  188. -(void) dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  189. [self assertReady];
  190. [CATransaction begin];
  191. [CATransaction setCompletionBlock:^{
  192. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  193. completion();
  194. }];
  195. [_modalManager dismissModal:componentId];
  196. [CATransaction commit];
  197. }
  198. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  199. [self assertReady];
  200. [CATransaction begin];
  201. [CATransaction setCompletionBlock:^{
  202. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals params:@{}];
  203. completion();
  204. }];
  205. [_modalManager dismissAllModals];
  206. [CATransaction commit];
  207. }
  208. -(void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  209. [self assertReady];
  210. UIViewController<RNNRootViewProtocol>* overlayVC = [_controllerFactory createOverlay:layout];
  211. [_overlayManager showOverlay:overlayVC];
  212. [_eventEmitter sendOnNavigationCommandCompletion:showOverlay params:@{@"layout": layout}];
  213. completion();
  214. }
  215. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  216. [self assertReady];
  217. UIViewController* viewController = [_store findComponentForId:componentId];
  218. if (viewController) {
  219. [_overlayManager dismissOverlay:viewController];
  220. [_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay params:@{@"componentId": componentId}];
  221. completion();
  222. } else {
  223. [RNNErrorHandler reject:reject withErrorCode:1010 errorDescription:@"ComponentId not found"];
  224. }
  225. }
  226. #pragma mark - private
  227. -(void)removePopedViewControllers:(NSArray*)viewControllers {
  228. for (UIViewController *popedVC in viewControllers) {
  229. [_store removeComponentByViewControllerInstance:popedVC];
  230. }
  231. }
  232. -(void) assertReady {
  233. if (!_store.isReadyToReceiveCommands) {
  234. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  235. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  236. userInfo:nil]
  237. raise];
  238. }
  239. }
  240. @end