react-native-navigation的迁移库

RNNCommandsHandler.m 16KB

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