react-native-navigation的迁移库

RNNCommandsHandler.m 15KB

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