react-native-navigation的迁移库

RNNCommandsHandler.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. 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. RNNStore *_store;
  30. RNNModalManager* _modalManager;
  31. RNNOverlayManager* _overlayManager;
  32. RNNNavigationStackManager* _stackManager;
  33. RNNEventEmitter* _eventEmitter;
  34. UIWindow* _mainWindow;
  35. }
  36. - (instancetype)initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter stackManager:(RNNNavigationStackManager *)stackManager modalManager:(RNNModalManager *)modalManager overlayManager:(RNNOverlayManager *)overlayManager mainWindow:(UIWindow *)mainWindow {
  37. self = [super init];
  38. _store = store;
  39. _controllerFactory = controllerFactory;
  40. _eventEmitter = eventEmitter;
  41. _modalManager = modalManager;
  42. _modalManager.delegate = self;
  43. _stackManager = stackManager;
  44. _overlayManager = overlayManager;
  45. _mainWindow = mainWindow;
  46. return self;
  47. }
  48. #pragma mark - public
  49. - (void)setRoot:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  50. [self assertReady];
  51. if (@available(iOS 9, *)) {
  52. if(_controllerFactory.defaultOptions.layout.direction.hasValue) {
  53. if ([_controllerFactory.defaultOptions.layout.direction.get isEqualToString:@"rtl"]) {
  54. [[RCTI18nUtil sharedInstance] allowRTL:YES];
  55. [[RCTI18nUtil sharedInstance] forceRTL:YES];
  56. [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
  57. [[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
  58. } else {
  59. [[RCTI18nUtil sharedInstance] allowRTL:NO];
  60. [[RCTI18nUtil sharedInstance] forceRTL:NO];
  61. [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  62. [[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
  63. }
  64. }
  65. }
  66. [_modalManager dismissAllModalsAnimated:NO];
  67. [_store removeAllComponentsFromWindow:_mainWindow];
  68. UIViewController<RNNLayoutProtocol> *vc = [_controllerFactory createLayout:layout[@"root"]];
  69. [vc renderTreeAndWait:[vc.resolveOptions.animations.setRoot.waitForRender getWithDefaultValue:NO] perform:^{
  70. _mainWindow.rootViewController = vc;
  71. [_eventEmitter sendOnNavigationCommandCompletion:setRoot 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 layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  96. [self assertReady];
  97. UIViewController<RNNLayoutProtocol> *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 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 params:@{@"componentId": componentId}];
  138. completion();
  139. } rejection:rejection];
  140. }];
  141. }
  142. }
  143. - (void)setStackRoot:(NSString*)componentId children:(NSArray*)children completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  144. [self assertReady];
  145. NSArray<RNNLayoutProtocol> *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 params:@{@"componentId": componentId}];
  154. completion();
  155. } rejection:rejection];
  156. }
  157. - (void)pop:(NSString*)componentId 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. [_store removeComponent:componentId];
  176. [_eventEmitter sendOnNavigationCommandCompletion:pop params:@{@"componentId": componentId}];
  177. completion();
  178. } rejection:rejection];
  179. }
  180. - (void)popTo:(NSString*)componentId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  181. [self assertReady];
  182. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  183. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  184. [vc overrideOptions:options];
  185. [_stackManager popTo:vc animated:[vc.resolveOptions.animations.pop.enable getWithDefaultValue:YES] completion:^(NSArray *poppedViewControllers) {
  186. [_eventEmitter sendOnNavigationCommandCompletion:popTo params:@{@"componentId": componentId}];
  187. [self removePopedViewControllers:poppedViewControllers];
  188. completion();
  189. } rejection:rejection];
  190. }
  191. - (void)popToRoot:(NSString*)componentId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  192. [self assertReady];
  193. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  194. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  195. [vc overrideOptions:options];
  196. [CATransaction begin];
  197. [CATransaction setCompletionBlock:^{
  198. [_eventEmitter sendOnNavigationCommandCompletion:popToRoot params:@{@"componentId": componentId}];
  199. completion();
  200. }];
  201. [_stackManager popToRoot:vc animated:[vc.resolveOptions.animations.pop.enable getWithDefaultValue:YES] completion:^(NSArray *poppedViewControllers) {
  202. [self removePopedViewControllers:poppedViewControllers];
  203. } rejection:^(NSString *code, NSString *message, NSError *error) {
  204. }];
  205. [CATransaction commit];
  206. }
  207. - (void)showModal:(NSDictionary*)layout completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  208. [self assertReady];
  209. UIViewController<RNNParentProtocol> *newVc = [_controllerFactory createLayout:layout];
  210. [newVc renderTreeAndWait:[newVc.resolveOptions.animations.showModal.waitForRender getWithDefaultValue:NO] perform:^{
  211. [_modalManager showModal:newVc animated:[newVc.getCurrentChild.resolveOptions.animations.showModal.enable getWithDefaultValue:YES] hasCustomAnimation:newVc.getCurrentChild.resolveOptions.animations.showModal.hasCustomAnimation completion:^(NSString *componentId) {
  212. [_eventEmitter sendOnNavigationCommandCompletion:showModal params:@{@"layout": layout}];
  213. completion(componentId);
  214. }];
  215. }];
  216. }
  217. - (void)dismissModal:(NSString*)componentId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  218. [self assertReady];
  219. UIViewController<RNNParentProtocol> *modalToDismiss = (UIViewController<RNNParentProtocol>*)[_store findComponentForId:componentId];
  220. if (!modalToDismiss.isModal) {
  221. [RNNErrorHandler reject:reject withErrorCode:1013 errorDescription:@"component is not a modal"];
  222. return;
  223. }
  224. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  225. [modalToDismiss.getCurrentChild overrideOptions:options];
  226. [self removePopedViewControllers:modalToDismiss.navigationController.viewControllers];
  227. [CATransaction begin];
  228. [CATransaction setCompletionBlock:^{
  229. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  230. }];
  231. [_modalManager dismissModal:modalToDismiss completion:completion];
  232. [CATransaction commit];
  233. }
  234. - (void)dismissAllModals:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
  235. [self assertReady];
  236. [CATransaction begin];
  237. [CATransaction setCompletionBlock:^{
  238. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals params:@{}];
  239. completion();
  240. }];
  241. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  242. [_modalManager dismissAllModalsAnimated:[options.animations.dismissModal.enable getWithDefaultValue:YES]];
  243. [CATransaction commit];
  244. }
  245. - (void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  246. [self assertReady];
  247. UIViewController<RNNParentProtocol>* overlayVC = [_controllerFactory createLayout:layout];
  248. [overlayVC renderTreeAndWait:NO perform:^{
  249. UIWindow* overlayWindow = [[RNNOverlayWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  250. overlayWindow.rootViewController = overlayVC;
  251. [_overlayManager showOverlayWindow:overlayWindow];
  252. [_eventEmitter sendOnNavigationCommandCompletion:showOverlay params:@{@"layout": layout}];
  253. completion();
  254. }];
  255. }
  256. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  257. [self assertReady];
  258. UIViewController* viewController = [_store findComponentForId:componentId];
  259. if (viewController) {
  260. [_overlayManager dismissOverlay:viewController];
  261. [_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay params:@{@"componentId": componentId}];
  262. completion();
  263. } else {
  264. [RNNErrorHandler reject:reject withErrorCode:1010 errorDescription:@"ComponentId not found"];
  265. }
  266. }
  267. #pragma mark - private
  268. - (void)removePopedViewControllers:(NSArray*)viewControllers {
  269. for (UIViewController *popedVC in viewControllers) {
  270. [_store removeComponentByViewControllerInstance:popedVC];
  271. }
  272. }
  273. - (void)assertReady {
  274. if (!_store.isReadyToReceiveCommands) {
  275. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  276. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  277. userInfo:nil]
  278. raise];
  279. }
  280. }
  281. #pragma mark - RNNModalManagerDelegate
  282. - (void)dismissedModal:(UIViewController<RNNParentProtocol> *)viewController {
  283. [_eventEmitter sendModalsDismissedEvent:viewController.layoutInfo.componentId numberOfModalsDismissed:@(1)];
  284. }
  285. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  286. if (viewControllers && viewControllers.count) {
  287. [_eventEmitter sendModalsDismissedEvent:((UIViewController<RNNParentProtocol> *)viewControllers.lastObject).layoutInfo.componentId numberOfModalsDismissed:@(viewControllers.count)];
  288. }
  289. }
  290. @end