react-native-navigation的迁移库

RNNCommandsHandler.m 17KB

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