react-native-navigation的迁移库

RNNCommandsHandler.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 "UINavigationController+RNNCommands.h"
  11. #import "RNNAssert.h"
  12. static NSString* const setRoot = @"setRoot";
  13. static NSString* const setStackRoot = @"setStackRoot";
  14. static NSString* const push = @"push";
  15. static NSString* const preview = @"preview";
  16. static NSString* const pop = @"pop";
  17. static NSString* const popTo = @"popTo";
  18. static NSString* const popToRoot = @"popToRoot";
  19. static NSString* const showModal = @"showModal";
  20. static NSString* const dismissModal = @"dismissModal";
  21. static NSString* const dismissAllModals = @"dismissAllModals";
  22. static NSString* const showOverlay = @"showOverlay";
  23. static NSString* const dismissOverlay = @"dismissOverlay";
  24. static NSString* const mergeOptions = @"mergeOptions";
  25. static NSString* const setDefaultOptions = @"setDefaultOptions";
  26. @interface RNNCommandsHandler() <RNNModalManagerDelegate>
  27. @end
  28. @implementation RNNCommandsHandler {
  29. RNNControllerFactory *_controllerFactory;
  30. RNNModalManager* _modalManager;
  31. RNNOverlayManager* _overlayManager;
  32. RNNEventEmitter* _eventEmitter;
  33. UIWindow* _mainWindow;
  34. }
  35. - (instancetype)initWithControllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter 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. _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. RNNAssertMainQueue();
  49. if (@available(iOS 9, *)) {
  50. if(_controllerFactory.defaultOptions.layout.direction.hasValue) {
  51. if ([_controllerFactory.defaultOptions.layout.direction.get isEqualToString:@"rtl"]) {
  52. [[RCTI18nUtil sharedInstance] allowRTL:YES];
  53. [[RCTI18nUtil sharedInstance] forceRTL:YES];
  54. [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
  55. [[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
  56. } else {
  57. [[RCTI18nUtil sharedInstance] allowRTL:NO];
  58. [[RCTI18nUtil sharedInstance] forceRTL:NO];
  59. [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  60. [[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  61. }
  62. }
  63. }
  64. [_modalManager dismissAllModalsAnimated:NO completion:nil];
  65. UIViewController *vc = [_controllerFactory createLayout:layout[@"root"]];
  66. vc.waitForRender = [vc.resolveOptionsWithDefault.animations.setRoot.waitForRender getWithDefaultValue:NO];
  67. [vc setReactViewReadyCallback:^{
  68. _mainWindow.rootViewController = vc;
  69. [_eventEmitter sendOnNavigationCommandCompletion:setRoot commandId:commandId params:@{@"layout": layout}];
  70. completion();
  71. }];
  72. [vc render];
  73. }
  74. - (void)mergeOptions:(NSString*)componentId options:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
  75. [self assertReady];
  76. RNNAssertMainQueue();
  77. UIViewController<RNNLayoutProtocol>* vc = [RNNLayoutManager findComponentForId:componentId];
  78. RNNNavigationOptions* newOptions = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  79. if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] || [vc isKindOfClass:[RNNComponentViewController class]]) {
  80. [CATransaction begin];
  81. [CATransaction setCompletionBlock:completion];
  82. [vc mergeOptions:newOptions];
  83. [CATransaction commit];
  84. }
  85. }
  86. - (void)setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
  87. [self assertReady];
  88. RNNAssertMainQueue();
  89. RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initWithDict:optionsDict];
  90. [_controllerFactory setDefaultOptions:defaultOptions];
  91. UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window.rootViewController;
  92. [RNNDefaultOptionsHelper recrusivelySetDefaultOptions:defaultOptions onRootViewController:rootViewController];
  93. completion();
  94. }
  95. - (void)push:(NSString*)componentId commandId:(NSString*)commandId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  96. [self assertReady];
  97. RNNAssertMainQueue();
  98. UIViewController *newVc = [_controllerFactory createLayout:layout];
  99. UIViewController *fromVC = [RNNLayoutManager findComponentForId:componentId];
  100. if ([[newVc.resolveOptionsWithDefault.preview.reactTag getWithDefaultValue:@(0)] floatValue] > 0) {
  101. if ([fromVC isKindOfClass:[RNNComponentViewController class]]) {
  102. RNNComponentViewController* rootVc = (RNNComponentViewController*)fromVC;
  103. rootVc.previewController = newVc;
  104. [newVc render];
  105. rootVc.previewCallback = ^(UIViewController *vcc) {
  106. RNNComponentViewController* rvc = (RNNComponentViewController*)vcc;
  107. [self->_eventEmitter sendOnPreviewCompleted:componentId previewComponentId:newVc.layoutInfo.componentId];
  108. if ([newVc.resolveOptionsWithDefault.preview.commit getWithDefaultValue:NO]) {
  109. [CATransaction begin];
  110. [CATransaction setCompletionBlock:^{
  111. [self->_eventEmitter sendOnNavigationCommandCompletion:push commandId:commandId params:@{@"componentId": componentId}];
  112. completion();
  113. }];
  114. [rvc.navigationController pushViewController:newVc animated:YES];
  115. [CATransaction commit];
  116. }
  117. };
  118. CGSize size = CGSizeMake(rootVc.view.frame.size.width, rootVc.view.frame.size.height);
  119. if (newVc.resolveOptionsWithDefault.preview.width.hasValue) {
  120. size.width = [newVc.resolveOptionsWithDefault.preview.width.get floatValue];
  121. }
  122. if (newVc.resolveOptionsWithDefault.preview.height.hasValue) {
  123. size.height = [newVc.resolveOptionsWithDefault.preview.height.get floatValue];
  124. }
  125. if (newVc.resolveOptionsWithDefault.preview.width.hasValue || newVc.resolveOptionsWithDefault.preview.height.hasValue) {
  126. newVc.preferredContentSize = size;
  127. }
  128. RCTExecuteOnMainQueue(^{
  129. UIView *view = [[ReactNativeNavigation getBridge].uiManager viewForReactTag:newVc.resolveOptionsWithDefault.preview.reactTag.get];
  130. [rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:view];
  131. });
  132. }
  133. } else {
  134. newVc.waitForRender = newVc.resolveOptionsWithDefault.animations.push.shouldWaitForRender;
  135. [newVc setReactViewReadyCallback:^{
  136. [fromVC.stack push:newVc onTop:fromVC animated:[newVc.resolveOptionsWithDefault.animations.push.enable getWithDefaultValue:YES] completion:^{
  137. [self->_eventEmitter sendOnNavigationCommandCompletion:push commandId:commandId params:@{@"componentId": componentId}];
  138. completion();
  139. } rejection:rejection];
  140. }];
  141. [newVc render];
  142. }
  143. }
  144. - (void)setStackRoot:(NSString*)componentId commandId:(NSString*)commandId children:(NSArray*)children completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  145. [self assertReady];
  146. RNNAssertMainQueue();
  147. NSArray<UIViewController *> *childViewControllers = [_controllerFactory createChildrenLayout:children];
  148. for (UIViewController<RNNLayoutProtocol>* viewController in childViewControllers) {
  149. if (![viewController isEqual:childViewControllers.lastObject]) {
  150. [viewController render];
  151. }
  152. }
  153. UIViewController *newVC = childViewControllers.lastObject;
  154. UIViewController *fromVC = [RNNLayoutManager findComponentForId:componentId];
  155. RNNNavigationOptions* options = newVC.resolveOptionsWithDefault;
  156. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  157. newVC.waitForRender = ([options.animations.setStackRoot.waitForRender getWithDefaultValue:NO]);
  158. [newVC setReactViewReadyCallback:^{
  159. [fromVC.stack setStackChildren:childViewControllers fromViewController:fromVC animated:[options.animations.setStackRoot.enable getWithDefaultValue:YES] completion:^{
  160. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot commandId:commandId params:@{@"componentId": componentId}];
  161. completion();
  162. } rejection:rejection];
  163. }];
  164. [newVC render];
  165. }
  166. - (void)pop:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  167. [self assertReady];
  168. RNNAssertMainQueue();
  169. RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
  170. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  171. [vc overrideOptions:options];
  172. [vc.stack pop:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^{
  173. [_eventEmitter sendOnNavigationCommandCompletion:pop commandId:commandId params:@{@"componentId": componentId}];
  174. completion();
  175. } rejection:rejection];
  176. }
  177. - (void)popTo:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  178. [self assertReady];
  179. RNNAssertMainQueue();
  180. RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
  181. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  182. [vc overrideOptions:options];
  183. [vc.stack popTo:vc animated:[vc.resolveOptionsWithDefault.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. RNNAssertMainQueue();
  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. [vc.stack popToRoot:vc animated:[vc.resolveOptionsWithDefault.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. RNNAssertMainQueue();
  207. UIViewController *newVc = [_controllerFactory createLayout:layout];
  208. newVc.waitForRender = [newVc.resolveOptionsWithDefault.animations.showModal.waitForRender getWithDefaultValue:NO];
  209. [newVc setReactViewReadyCallback:^{
  210. [_modalManager showModal:newVc animated:[newVc.resolveOptionsWithDefault.animations.showModal.enable getWithDefaultValue:YES] completion:^(NSString *componentId) {
  211. [self->_eventEmitter sendOnNavigationCommandCompletion:showModal commandId:commandId params:@{@"layout": layout}];
  212. completion(newVc.layoutInfo.componentId);
  213. }];
  214. }];
  215. [newVc render];
  216. }
  217. - (void)dismissModal:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  218. [self assertReady];
  219. RNNAssertMainQueue();
  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. [self->_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. RNNAssertMainQueue();
  237. [CATransaction begin];
  238. [CATransaction setCompletionBlock:^{
  239. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals commandId:commandId params:@{}];
  240. completion();
  241. }];
  242. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  243. [_modalManager dismissAllModalsAnimated:[options.animations.dismissModal.enable getWithDefaultValue:YES] completion:nil];
  244. [CATransaction commit];
  245. }
  246. - (void)showOverlay:(NSDictionary *)layout commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion {
  247. [self assertReady];
  248. RNNAssertMainQueue();
  249. UIViewController* overlayVC = [_controllerFactory createLayout:layout];
  250. [overlayVC setReactViewReadyCallback:^{UIWindow* overlayWindow = [[RNNOverlayWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  251. overlayWindow.rootViewController = overlayVC;
  252. if ([overlayVC.resolveOptionsWithDefault.overlay.handleKeyboardEvents getWithDefaultValue:NO]) {
  253. [self->_overlayManager showOverlayWindowAsKeyWindow:overlayWindow];
  254. } else {
  255. [self->_overlayManager showOverlayWindow:overlayWindow];
  256. }
  257. [self->_eventEmitter sendOnNavigationCommandCompletion:showOverlay commandId:commandId params:@{@"layout": layout}];
  258. completion();
  259. }];
  260. [overlayVC render];
  261. }
  262. - (void)dismissOverlay:(NSString*)componentId commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  263. [self assertReady];
  264. RNNAssertMainQueue();
  265. UIViewController* viewController = [RNNLayoutManager findComponentForId:componentId];
  266. if (viewController) {
  267. [_overlayManager dismissOverlay:viewController];
  268. [_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay commandId:commandId params:@{@"componentId": componentId}];
  269. completion();
  270. } else {
  271. [RNNErrorHandler reject:reject withErrorCode:1010 errorDescription:@"ComponentId not found"];
  272. }
  273. }
  274. #pragma mark - private
  275. - (void)assertReady {
  276. if (!self.readyToReceiveCommands) {
  277. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  278. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  279. userInfo:nil]
  280. raise];
  281. }
  282. }
  283. #pragma mark - RNNModalManagerDelegate
  284. - (void)dismissedModal:(UIViewController *)viewController {
  285. [_eventEmitter sendModalsDismissedEvent:viewController.layoutInfo.componentId componentName:viewController.layoutInfo.name numberOfModalsDismissed:@(1)];
  286. }
  287. - (void)attemptedToDismissModal:(UIViewController *)viewController {
  288. [_eventEmitter sendModalAttemptedToDismissEvent:viewController.layoutInfo.componentId];
  289. }
  290. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  291. if (viewControllers && viewControllers.count) {
  292. UIViewController* lastViewController = [viewControllers.lastObject presentedComponentViewController];
  293. [_eventEmitter sendModalsDismissedEvent:lastViewController.layoutInfo.componentId componentName:lastViewController.layoutInfo.name numberOfModalsDismissed:@(viewControllers.count)];
  294. }
  295. }
  296. @end