react-native-navigation的迁移库

RNNCommandsHandler.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.resolveOptionsWithDefault.animations.pop) {
  171. nvc.delegate = vc;
  172. } else {
  173. nvc.delegate = nil;
  174. }
  175. } else {
  176. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  177. [vcs removeObject:vc];
  178. [nvc setViewControllers:vcs animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES]];
  179. }
  180. [_stackManager pop:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^{
  181. [_eventEmitter sendOnNavigationCommandCompletion:pop commandId:commandId params:@{@"componentId": componentId}];
  182. completion();
  183. } rejection:rejection];
  184. }
  185. - (void)popTo:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  186. [self assertReady];
  187. RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
  188. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  189. [vc overrideOptions:options];
  190. [_stackManager popTo:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^(NSArray *poppedViewControllers) {
  191. [_eventEmitter sendOnNavigationCommandCompletion:popTo commandId:commandId params:@{@"componentId": componentId}];
  192. completion();
  193. } rejection:rejection];
  194. }
  195. - (void)popToRoot:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  196. [self assertReady];
  197. RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
  198. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  199. [vc overrideOptions:options];
  200. [CATransaction begin];
  201. [CATransaction setCompletionBlock:^{
  202. [_eventEmitter sendOnNavigationCommandCompletion:popToRoot commandId:commandId params:@{@"componentId": componentId}];
  203. completion();
  204. }];
  205. [_stackManager popToRoot:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^(NSArray *poppedViewControllers) {
  206. } rejection:^(NSString *code, NSString *message, NSError *error) {
  207. }];
  208. [CATransaction commit];
  209. }
  210. - (void)showModal:(NSDictionary*)layout commandId:(NSString *)commandId completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  211. [self assertReady];
  212. UIViewController *newVc = [_controllerFactory createLayout:layout];
  213. newVc.waitForRender = [newVc.resolveOptionsWithDefault.animations.showModal.waitForRender getWithDefaultValue:NO];
  214. [newVc setReactViewReadyCallback:^{
  215. [_modalManager showModal:newVc animated:[newVc.resolveOptionsWithDefault.animations.showModal.enable getWithDefaultValue:YES] hasCustomAnimation:newVc.resolveOptionsWithDefault.animations.showModal.hasCustomAnimation completion:^(NSString *componentId) {
  216. [self->_eventEmitter sendOnNavigationCommandCompletion:showModal commandId:commandId params:@{@"layout": layout}];
  217. completion(newVc.layoutInfo.componentId);
  218. }];
  219. }];
  220. [newVc render];
  221. }
  222. - (void)dismissModal:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  223. [self assertReady];
  224. UIViewController *modalToDismiss = (UIViewController *)[RNNLayoutManager findComponentForId:componentId];
  225. if (!modalToDismiss.isModal) {
  226. [RNNErrorHandler reject:reject withErrorCode:1013 errorDescription:@"component is not a modal"];
  227. return;
  228. }
  229. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  230. [modalToDismiss.getCurrentChild overrideOptions:options];
  231. [CATransaction begin];
  232. [CATransaction setCompletionBlock:^{
  233. [self->_eventEmitter sendOnNavigationCommandCompletion:dismissModal commandId:commandId params:@{@"componentId": componentId}];
  234. }];
  235. [_modalManager dismissModal:modalToDismiss completion:completion];
  236. [CATransaction commit];
  237. }
  238. - (void)dismissAllModals:(NSDictionary *)mergeOptions commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion {
  239. [self assertReady];
  240. [CATransaction begin];
  241. [CATransaction setCompletionBlock:^{
  242. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals commandId:commandId params:@{}];
  243. completion();
  244. }];
  245. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  246. [_modalManager dismissAllModalsAnimated:[options.animations.dismissModal.enable getWithDefaultValue:YES] completion:nil];
  247. [CATransaction commit];
  248. }
  249. - (void)showOverlay:(NSDictionary *)layout commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion {
  250. [self assertReady];
  251. UIViewController* overlayVC = [_controllerFactory createLayout:layout];
  252. [overlayVC setReactViewReadyCallback:^{UIWindow* overlayWindow = [[RNNOverlayWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  253. overlayWindow.rootViewController = overlayVC;
  254. if ([overlayVC.resolveOptionsWithDefault.overlay.handleKeyboardEvents getWithDefaultValue:NO]) {
  255. [self->_overlayManager showOverlayWindowAsKeyWindow:overlayWindow];
  256. } else {
  257. [self->_overlayManager showOverlayWindow:overlayWindow];
  258. }
  259. [self->_eventEmitter sendOnNavigationCommandCompletion:showOverlay commandId:commandId params:@{@"layout": layout}];
  260. completion();
  261. }];
  262. [overlayVC render];
  263. }
  264. - (void)dismissOverlay:(NSString*)componentId commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  265. [self assertReady];
  266. UIViewController* viewController = [RNNLayoutManager findComponentForId:componentId];
  267. if (viewController) {
  268. [_overlayManager dismissOverlay:viewController];
  269. [_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay commandId:commandId params:@{@"componentId": componentId}];
  270. completion();
  271. } else {
  272. [RNNErrorHandler reject:reject withErrorCode:1010 errorDescription:@"ComponentId not found"];
  273. }
  274. }
  275. #pragma mark - private
  276. - (void)assertReady {
  277. if (!self.readyToReceiveCommands) {
  278. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  279. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  280. userInfo:nil]
  281. raise];
  282. }
  283. }
  284. #pragma mark - RNNModalManagerDelegate
  285. - (void)dismissedModal:(UIViewController *)viewController {
  286. [_eventEmitter sendModalsDismissedEvent:viewController.layoutInfo.componentId numberOfModalsDismissed:@(1)];
  287. }
  288. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  289. if (viewControllers && viewControllers.count) {
  290. [_eventEmitter sendModalsDismissedEvent:((UIViewController *)viewControllers.lastObject).layoutInfo.componentId numberOfModalsDismissed:@(viewControllers.count)];
  291. }
  292. }
  293. @end