react-native-navigation的迁移库

RNNCommandsHandler.m 17KB

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