react-native-navigation的迁移库

RNNCommandsHandler.m 14KB

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