react-native-navigation的迁移库

RNNCommandsHandler.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #import "RNNCommandsHandler.h"
  2. #import "RNNNavigationOptions.h"
  3. #import "RNNRootViewController.h"
  4. #import "RNNSplitViewController.h"
  5. #import "RNNElementFinder.h"
  6. #import "React/RCTUIManager.h"
  7. #import "RNNErrorHandler.h"
  8. #import "RNNDefaultOptionsHelper.h"
  9. #import "UIViewController+RNNOptions.h"
  10. #import "React/RCTI18nUtil.h"
  11. #import "UIViewController+LayoutProtocol.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. RNNStore *_store;
  31. RNNModalManager* _modalManager;
  32. RNNOverlayManager* _overlayManager;
  33. RNNNavigationStackManager* _stackManager;
  34. RNNEventEmitter* _eventEmitter;
  35. UIWindow* _mainWindow;
  36. }
  37. - (instancetype)initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter stackManager:(RNNNavigationStackManager *)stackManager modalManager:(RNNModalManager *)modalManager overlayManager:(RNNOverlayManager *)overlayManager mainWindow:(UIWindow *)mainWindow {
  38. self = [super init];
  39. _store = store;
  40. _controllerFactory = controllerFactory;
  41. _eventEmitter = eventEmitter;
  42. _modalManager = modalManager;
  43. _modalManager.delegate = self;
  44. _stackManager = stackManager;
  45. _overlayManager = overlayManager;
  46. _mainWindow = mainWindow;
  47. return self;
  48. }
  49. #pragma mark - public
  50. - (void)setRoot:(NSDictionary*)layout commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion {
  51. [self assertReady];
  52. if (@available(iOS 9, *)) {
  53. if(_controllerFactory.defaultOptions.layout.direction.hasValue) {
  54. if ([_controllerFactory.defaultOptions.layout.direction.get isEqualToString:@"rtl"]) {
  55. [[RCTI18nUtil sharedInstance] allowRTL:YES];
  56. [[RCTI18nUtil sharedInstance] forceRTL:YES];
  57. [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
  58. [[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
  59. } else {
  60. [[RCTI18nUtil sharedInstance] allowRTL:NO];
  61. [[RCTI18nUtil sharedInstance] forceRTL:NO];
  62. [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  63. [[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  64. }
  65. }
  66. }
  67. [_modalManager dismissAllModalsAnimated:NO];
  68. UIViewController *vc = [_controllerFactory createLayout:layout[@"root"]];
  69. [vc renderTreeAndWait:[vc.resolveOptions.animations.setRoot.waitForRender getWithDefaultValue:NO] perform:^{
  70. _mainWindow.rootViewController = vc;
  71. [_eventEmitter sendOnNavigationCommandCompletion:setRoot commandId:commandId params:@{@"layout": layout}];
  72. completion() ;
  73. }];
  74. }
  75. - (void)mergeOptions:(NSString*)componentId options:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
  76. [self assertReady];
  77. UIViewController<RNNLayoutProtocol>* vc = (UIViewController<RNNLayoutProtocol>*)[_store findComponentForId:componentId];
  78. RNNNavigationOptions* newOptions = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  79. if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] || [vc isKindOfClass:[RNNRootViewController class]]) {
  80. [CATransaction begin];
  81. [CATransaction setCompletionBlock:completion];
  82. [vc overrideOptions:newOptions];
  83. [vc mergeOptions:newOptions];
  84. [CATransaction commit];
  85. }
  86. }
  87. - (void)setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
  88. [self assertReady];
  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. UIViewController *newVc = [_controllerFactory createLayout:layout];
  98. UIViewController *fromVC = [_store findComponentForId:componentId];
  99. if ([[newVc.resolveOptions.preview.reactTag getWithDefaultValue:@(0)] floatValue] > 0) {
  100. UIViewController* vc = [_store findComponentForId:componentId];
  101. if([vc isKindOfClass:[RNNRootViewController class]]) {
  102. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  103. rootVc.previewController = newVc;
  104. [newVc renderTreeAndWait:NO perform:nil];
  105. rootVc.previewCallback = ^(UIViewController *vcc) {
  106. RNNRootViewController* rvc = (RNNRootViewController*)vcc;
  107. [self->_eventEmitter sendOnPreviewCompleted:componentId previewComponentId:newVc.layoutInfo.componentId];
  108. if ([newVc.resolveOptions.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.resolveOptions.preview.width.hasValue) {
  120. size.width = [newVc.resolveOptions.preview.width.get floatValue];
  121. }
  122. if (newVc.resolveOptions.preview.height.hasValue) {
  123. size.height = [newVc.resolveOptions.preview.height.get floatValue];
  124. }
  125. if (newVc.resolveOptions.preview.width.hasValue || newVc.resolveOptions.preview.height.hasValue) {
  126. newVc.preferredContentSize = size;
  127. }
  128. RCTExecuteOnMainQueue(^{
  129. UIView *view = [[ReactNativeNavigation getBridge].uiManager viewForReactTag:newVc.resolveOptions.preview.reactTag.get];
  130. [rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:view];
  131. });
  132. }
  133. } else {
  134. id animationDelegate = (newVc.resolveOptions.animations.push.hasCustomAnimation || newVc.resolveOptions.customTransition.animations) ? newVc : nil;
  135. [newVc renderTreeAndWait:([newVc.resolveOptions.animations.push.waitForRender getWithDefaultValue:NO] || animationDelegate) perform:^{
  136. [_stackManager push:newVc onTop:fromVC animated:[newVc.resolveOptions.animations.push.enable getWithDefaultValue:YES] animationDelegate:animationDelegate completion:^{
  137. [_eventEmitter sendOnNavigationCommandCompletion:push commandId:commandId params:@{@"componentId": componentId}];
  138. completion();
  139. } rejection:rejection];
  140. }];
  141. }
  142. }
  143. - (void)setStackRoot:(NSString*)componentId commandId:(NSString*)commandId children:(NSArray*)children completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  144. [self assertReady];
  145. NSArray *childViewControllers = [_controllerFactory createChildrenLayout:children];
  146. for (UIViewController<RNNLayoutProtocol>* viewController in childViewControllers) {
  147. [viewController renderTreeAndWait:NO perform:nil];
  148. }
  149. RNNNavigationOptions* options = [childViewControllers.lastObject getCurrentChild].resolveOptions;
  150. UIViewController *fromVC = [_store findComponentForId:componentId];
  151. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  152. [_stackManager setStackChildren:childViewControllers fromViewController:fromVC animated:[options.animations.setStackRoot.enable getWithDefaultValue:YES] completion:^{
  153. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot commandId:commandId params:@{@"componentId": componentId}];
  154. completion();
  155. } rejection:rejection];
  156. }
  157. - (void)pop:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  158. [self assertReady];
  159. RNNRootViewController *vc = (RNNRootViewController*)[_store 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. RNNRootViewController *vc = (RNNRootViewController*)[_store 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. RNNRootViewController *vc = (RNNRootViewController*)[_store 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.getCurrentChild.resolveOptions.animations.showModal.enable getWithDefaultValue:YES] hasCustomAnimation:newVc.getCurrentChild.resolveOptions.animations.showModal.hasCustomAnimation completion:^(NSString *componentId) {
  209. [_eventEmitter sendOnNavigationCommandCompletion:showModal commandId:commandId params:@{@"layout": layout}];
  210. completion(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 *)[_store 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. [_overlayManager showOverlayWindow:overlayWindow];
  248. [_eventEmitter sendOnNavigationCommandCompletion:showOverlay commandId:commandId params:@{@"layout": layout}];
  249. completion();
  250. }];
  251. }
  252. - (void)dismissOverlay:(NSString*)componentId commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  253. [self assertReady];
  254. UIViewController* viewController = [_store findComponentForId:componentId];
  255. if (viewController) {
  256. [_overlayManager dismissOverlay:viewController];
  257. [_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay commandId:commandId params:@{@"componentId": componentId}];
  258. completion();
  259. } else {
  260. [RNNErrorHandler reject:reject withErrorCode:1010 errorDescription:@"ComponentId not found"];
  261. }
  262. }
  263. #pragma mark - private
  264. - (void)assertReady {
  265. if (!_store.isReadyToReceiveCommands) {
  266. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  267. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  268. userInfo:nil]
  269. raise];
  270. }
  271. }
  272. #pragma mark - RNNModalManagerDelegate
  273. - (void)dismissedModal:(UIViewController *)viewController {
  274. [_eventEmitter sendModalsDismissedEvent:viewController.layoutInfo.componentId numberOfModalsDismissed:@(1)];
  275. }
  276. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  277. if (viewControllers && viewControllers.count) {
  278. [_eventEmitter sendModalsDismissedEvent:((UIViewController *)viewControllers.lastObject).layoutInfo.componentId numberOfModalsDismissed:@(viewControllers.count)];
  279. }
  280. }
  281. @end