react-native-navigation的迁移库

RNNCommandsHandler.m 16KB

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