react-native-navigation的迁移库

RNNCommandsHandler.m 16KB

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