react-native-navigation的迁移库

RNNCommandsHandler.m 15KB

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