react-native-navigation的迁移库

RNNCommandsHandler.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. __weak UIViewController* weakVC = vc;
  68. [vc setReactViewReadyCallback:^{
  69. self->_mainWindow.rootViewController = weakVC;
  70. [self->_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. __weak UIViewController* weakNewVC = newVc;
  137. [newVc setReactViewReadyCallback:^{
  138. [fromVC.stack push:weakNewVC onTop:fromVC animated:[weakNewVC.resolveOptionsWithDefault.animations.push.enable getWithDefaultValue:YES] completion:^{
  139. [self->_eventEmitter sendOnNavigationCommandCompletion:push commandId:commandId params:@{@"componentId": componentId}];
  140. completion();
  141. } rejection:rejection];
  142. }];
  143. [newVc render];
  144. }
  145. }
  146. - (void)setStackRoot:(NSString*)componentId commandId:(NSString*)commandId children:(NSArray*)children completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  147. [self assertReady];
  148. RNNAssertMainQueue();
  149. NSArray<UIViewController *> *childViewControllers = [_controllerFactory createChildrenLayout:children];
  150. for (UIViewController<RNNLayoutProtocol>* viewController in childViewControllers) {
  151. if (![viewController isEqual:childViewControllers.lastObject]) {
  152. [viewController render];
  153. }
  154. }
  155. UIViewController *newVC = childViewControllers.lastObject;
  156. UIViewController *fromVC = [RNNLayoutManager findComponentForId:componentId];
  157. RNNNavigationOptions* options = newVC.resolveOptionsWithDefault;
  158. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  159. newVC.waitForRender = ([options.animations.setStackRoot.waitForRender getWithDefaultValue:NO]);
  160. [newVC setReactViewReadyCallback:^{
  161. [fromVC.stack setStackChildren:childViewControllers fromViewController:fromVC animated:[options.animations.setStackRoot.enable getWithDefaultValue:YES] completion:^{
  162. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot commandId:commandId params:@{@"componentId": componentId}];
  163. completion();
  164. } rejection:rejection];
  165. }];
  166. [newVC render];
  167. }
  168. - (void)pop:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  169. [self assertReady];
  170. RNNAssertMainQueue();
  171. RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
  172. if (vc) {
  173. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  174. [vc overrideOptions:options];
  175. [vc.stack pop:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^{
  176. [self->_eventEmitter sendOnNavigationCommandCompletion:pop commandId:commandId params:@{@"componentId": componentId}];
  177. completion();
  178. } rejection:rejection];
  179. } else {
  180. [RNNErrorHandler reject:rejection withErrorCode:1012 errorDescription:[NSString stringWithFormat:@"Popping component failed - componentId '%@' not found", componentId]];
  181. }
  182. }
  183. - (void)popTo:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  184. [self assertReady];
  185. RNNAssertMainQueue();
  186. RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
  187. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  188. [vc overrideOptions:options];
  189. [vc.stack popTo:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^(NSArray *poppedViewControllers) {
  190. [self->_eventEmitter sendOnNavigationCommandCompletion:popTo commandId:commandId params:@{@"componentId": componentId}];
  191. completion();
  192. } rejection:rejection];
  193. }
  194. - (void)popToRoot:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  195. [self assertReady];
  196. RNNAssertMainQueue();
  197. RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
  198. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  199. [vc overrideOptions:options];
  200. [CATransaction begin];
  201. [CATransaction setCompletionBlock:^{
  202. [self->_eventEmitter sendOnNavigationCommandCompletion:popToRoot commandId:commandId params:@{@"componentId": componentId}];
  203. completion();
  204. }];
  205. [vc.stack popToRoot:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^(NSArray *poppedViewControllers) {
  206. } rejection:^(NSString *code, NSString *message, NSError *error) {
  207. }];
  208. [CATransaction commit];
  209. }
  210. - (void)showModal:(NSDictionary*)layout commandId:(NSString *)commandId completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  211. [self assertReady];
  212. RNNAssertMainQueue();
  213. UIViewController *newVc = [_controllerFactory createLayout:layout];
  214. __weak UIViewController* weakNewVC = newVc;
  215. newVc.waitForRender = [newVc.resolveOptionsWithDefault.animations.showModal.waitForRender getWithDefaultValue:NO];
  216. [newVc setReactViewReadyCallback:^{
  217. [self->_modalManager showModal:weakNewVC animated:[weakNewVC.resolveOptionsWithDefault.animations.showModal.enable getWithDefaultValue:YES] completion:^(NSString *componentId) {
  218. [self->_eventEmitter sendOnNavigationCommandCompletion:showModal commandId:commandId params:@{@"layout": layout}];
  219. completion(weakNewVC.layoutInfo.componentId);
  220. }];
  221. }];
  222. [newVc render];
  223. }
  224. - (void)dismissModal:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  225. [self assertReady];
  226. RNNAssertMainQueue();
  227. UIViewController *modalToDismiss = (UIViewController *)[RNNLayoutManager findComponentForId:componentId];
  228. if (!modalToDismiss.isModal) {
  229. [RNNErrorHandler reject:reject withErrorCode:1013 errorDescription:@"component is not a modal"];
  230. return;
  231. }
  232. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  233. [modalToDismiss.getCurrentChild overrideOptions:options];
  234. [CATransaction begin];
  235. [CATransaction setCompletionBlock:^{
  236. [self->_eventEmitter sendOnNavigationCommandCompletion:dismissModal commandId:commandId params:@{@"componentId": componentId}];
  237. }];
  238. [_modalManager dismissModal:modalToDismiss completion:completion];
  239. [CATransaction commit];
  240. }
  241. - (void)dismissAllModals:(NSDictionary *)mergeOptions commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion {
  242. [self assertReady];
  243. RNNAssertMainQueue();
  244. [CATransaction begin];
  245. [CATransaction setCompletionBlock:^{
  246. [self->_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals commandId:commandId params:@{}];
  247. completion();
  248. }];
  249. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  250. [_modalManager dismissAllModalsAnimated:[options.animations.dismissModal.enable getWithDefaultValue:YES] completion:nil];
  251. [CATransaction commit];
  252. }
  253. - (void)showOverlay:(NSDictionary *)layout commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion {
  254. [self assertReady];
  255. RNNAssertMainQueue();
  256. UIViewController* overlayVC = [_controllerFactory createLayout:layout];
  257. __weak UIViewController* weakOverlayVC = overlayVC;
  258. [overlayVC setReactViewReadyCallback:^{UIWindow* overlayWindow = [[RNNOverlayWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  259. overlayWindow.rootViewController = weakOverlayVC;
  260. if ([weakOverlayVC.resolveOptionsWithDefault.overlay.handleKeyboardEvents getWithDefaultValue:NO]) {
  261. [self->_overlayManager showOverlayWindowAsKeyWindow:overlayWindow];
  262. } else {
  263. [self->_overlayManager showOverlayWindow:overlayWindow];
  264. }
  265. [self->_eventEmitter sendOnNavigationCommandCompletion:showOverlay commandId:commandId params:@{@"layout": layout}];
  266. completion();
  267. }];
  268. [overlayVC render];
  269. }
  270. - (void)dismissOverlay:(NSString*)componentId commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  271. [self assertReady];
  272. RNNAssertMainQueue();
  273. UIViewController* viewController = [RNNLayoutManager findComponentForId:componentId];
  274. if (viewController) {
  275. [_overlayManager dismissOverlay:viewController];
  276. [_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay commandId:commandId params:@{@"componentId": componentId}];
  277. completion();
  278. } else {
  279. [RNNErrorHandler reject:reject withErrorCode:1010 errorDescription:@"ComponentId not found"];
  280. }
  281. }
  282. #pragma mark - private
  283. - (void)assertReady {
  284. if (!self.readyToReceiveCommands) {
  285. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  286. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  287. userInfo:nil]
  288. raise];
  289. }
  290. }
  291. #pragma mark - RNNModalManagerDelegate
  292. - (void)dismissedModal:(UIViewController *)viewController {
  293. [_eventEmitter sendModalsDismissedEvent:viewController.layoutInfo.componentId componentName:viewController.layoutInfo.name numberOfModalsDismissed:@(1)];
  294. }
  295. - (void)attemptedToDismissModal:(UIViewController *)viewController {
  296. [_eventEmitter sendModalAttemptedToDismissEvent:viewController.layoutInfo.componentId];
  297. }
  298. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  299. if (viewControllers && viewControllers.count) {
  300. UIViewController* lastViewController = [viewControllers.lastObject presentedComponentViewController];
  301. [_eventEmitter sendModalsDismissedEvent:lastViewController.layoutInfo.componentId componentName:lastViewController.layoutInfo.name numberOfModalsDismissed:@(viewControllers.count)];
  302. }
  303. }
  304. @end