react-native-navigation的迁移库

RNNCommandsHandler.m 15KB

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