react-native-navigation的迁移库

RNNCommandsHandler.m 14KB

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