react-native-navigation的迁移库

RNNCommandsHandler.m 13KB

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