react-native-navigation的迁移库

RNNCommandsHandler.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. static NSString* const setRoot = @"setRoot";
  11. static NSString* const setStackRoot = @"setStackRoot";
  12. static NSString* const push = @"push";
  13. static NSString* const preview = @"preview";
  14. static NSString* const pop = @"pop";
  15. static NSString* const popTo = @"popTo";
  16. static NSString* const popToRoot = @"popToRoot";
  17. static NSString* const showModal = @"showModal";
  18. static NSString* const dismissModal = @"dismissModal";
  19. static NSString* const dismissAllModals = @"dismissAllModals";
  20. static NSString* const showOverlay = @"showOverlay";
  21. static NSString* const dismissOverlay = @"dismissOverlay";
  22. static NSString* const mergeOptions = @"mergeOptions";
  23. static NSString* const setDefaultOptions = @"setDefaultOptions";
  24. @interface RNNCommandsHandler() <RNNModalManagerDelegate>
  25. @end
  26. @implementation RNNCommandsHandler {
  27. RNNControllerFactory *_controllerFactory;
  28. RNNStore *_store;
  29. RNNModalManager* _modalManager;
  30. RNNOverlayManager* _overlayManager;
  31. RNNNavigationStackManager* _stackManager;
  32. RNNEventEmitter* _eventEmitter;
  33. UIWindow* _mainWindow;
  34. }
  35. - (instancetype)initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter stackManager:(RNNNavigationStackManager *)stackManager modalManager:(RNNModalManager *)modalManager overlayManager:(RNNOverlayManager *)overlayManager mainWindow:(UIWindow *)mainWindow {
  36. self = [super init];
  37. _store = store;
  38. _controllerFactory = controllerFactory;
  39. _eventEmitter = eventEmitter;
  40. _modalManager = modalManager;
  41. _modalManager.delegate = self;
  42. _stackManager = stackManager;
  43. _overlayManager = overlayManager;
  44. _mainWindow = mainWindow;
  45. return self;
  46. }
  47. #pragma mark - public
  48. - (void)setRoot:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  49. [self assertReady];
  50. [_modalManager dismissAllModalsAnimated:NO];
  51. [_store removeAllComponentsFromWindow:_mainWindow];
  52. UIViewController *vc = [_controllerFactory createLayout:layout[@"root"] saveToStore:_store];
  53. _mainWindow.rootViewController = vc;
  54. [_eventEmitter sendOnNavigationCommandCompletion:setRoot params:@{@"layout": layout}];
  55. completion();
  56. }
  57. - (void)mergeOptions:(NSString*)componentId options:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
  58. [self assertReady];
  59. UIViewController<RNNLayoutProtocol>* vc = (UIViewController<RNNLayoutProtocol>*)[_store findComponentForId:componentId];
  60. RNNNavigationOptions* newOptions = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  61. if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] || [vc isKindOfClass:[RNNRootViewController class]]) {
  62. [CATransaction begin];
  63. [CATransaction setCompletionBlock:completion];
  64. [vc overrideOptions:newOptions];
  65. [vc mergeOptions:newOptions];
  66. [CATransaction commit];
  67. }
  68. }
  69. - (void)setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
  70. [self assertReady];
  71. RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initWithDict:optionsDict];
  72. [_controllerFactory setDefaultOptions:defaultOptions];
  73. UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window.rootViewController;
  74. [RNNDefaultOptionsHelper recrusivelySetDefaultOptions:defaultOptions onRootViewController:rootViewController];
  75. completion();
  76. }
  77. - (void)push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  78. [self assertReady];
  79. UIViewController<RNNLayoutProtocol> *newVc = [_controllerFactory createLayout:layout saveToStore:_store];
  80. UIViewController *fromVC = [_store findComponentForId:componentId];
  81. if ([[newVc.resolveOptions.preview.reactTag getWithDefaultValue:@(0)] floatValue] > 0) {
  82. UIViewController* vc = [_store findComponentForId:componentId];
  83. if([vc isKindOfClass:[RNNRootViewController class]]) {
  84. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  85. rootVc.previewController = newVc;
  86. rootVc.previewCallback = ^(UIViewController *vcc) {
  87. RNNRootViewController* rvc = (RNNRootViewController*)vcc;
  88. [self->_eventEmitter sendOnPreviewCompleted:componentId previewComponentId:newVc.layoutInfo.componentId];
  89. if ([newVc.resolveOptions.preview.commit getWithDefaultValue:NO]) {
  90. [CATransaction begin];
  91. [CATransaction setCompletionBlock:^{
  92. [self->_eventEmitter sendOnNavigationCommandCompletion:push params:@{@"componentId": componentId}];
  93. completion();
  94. }];
  95. [rvc.navigationController pushViewController:newVc animated:YES];
  96. [CATransaction commit];
  97. }
  98. };
  99. CGSize size = CGSizeMake(rootVc.view.frame.size.width, rootVc.view.frame.size.height);
  100. if (newVc.resolveOptions.preview.width.hasValue) {
  101. size.width = [newVc.resolveOptions.preview.width.get floatValue];
  102. }
  103. if (newVc.resolveOptions.preview.height.hasValue) {
  104. size.height = [newVc.resolveOptions.preview.height.get floatValue];
  105. }
  106. if (newVc.resolveOptions.preview.width.hasValue || newVc.resolveOptions.preview.height.hasValue) {
  107. newVc.preferredContentSize = size;
  108. }
  109. RCTExecuteOnMainQueue(^{
  110. UIView *view = [[ReactNativeNavigation getBridge].uiManager viewForReactTag:newVc.resolveOptions.preview.reactTag.get];
  111. [rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:view];
  112. });
  113. }
  114. } else {
  115. id animationDelegate = (newVc.resolveOptions.animations.push.hasCustomAnimation || newVc.getCurrentLeaf.isCustomTransitioned) ? newVc : nil;
  116. [newVc.getCurrentLeaf waitForReactViewRender:(newVc.resolveOptions.animations.push.waitForRender || animationDelegate) perform:^{
  117. [_stackManager push:newVc onTop:fromVC animated:newVc.resolveOptions.animations.push.enable animationDelegate:animationDelegate completion:^{
  118. [_eventEmitter sendOnNavigationCommandCompletion:push params:@{@"componentId": componentId}];
  119. completion();
  120. } rejection:rejection];
  121. }];
  122. }
  123. }
  124. - (void)setStackRoot:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  125. [self assertReady];
  126. UIViewController<RNNParentProtocol> *newVC = [_controllerFactory createLayout:layout saveToStore:_store];
  127. RNNNavigationOptions* options = [newVC getCurrentChild].resolveOptions;
  128. UIViewController *fromVC = [_store findComponentForId:componentId];
  129. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  130. [_stackManager setStackRoot:newVC fromViewController:fromVC animated:options.animations.setStackRoot.enable completion:^{
  131. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot params:@{@"componentId": componentId}];
  132. completion();
  133. } rejection:rejection];
  134. }
  135. - (void)pop:(NSString*)componentId mergeOptions:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  136. [self assertReady];
  137. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  138. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  139. [vc overrideOptions:options];
  140. UINavigationController *nvc = vc.navigationController;
  141. if ([nvc topViewController] == vc) {
  142. if (vc.resolveOptions.animations.pop) {
  143. nvc.delegate = vc;
  144. } else {
  145. nvc.delegate = nil;
  146. }
  147. } else {
  148. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  149. [vcs removeObject:vc];
  150. [nvc setViewControllers:vcs animated:vc.resolveOptions.animations.pop.enable];
  151. }
  152. [_stackManager pop:vc animated:vc.resolveOptions.animations.pop.enable completion:^{
  153. [_store removeComponent:componentId];
  154. [_eventEmitter sendOnNavigationCommandCompletion:pop params:@{@"componentId": componentId}];
  155. completion();
  156. } rejection:rejection];
  157. }
  158. - (void)popTo:(NSString*)componentId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  159. [self assertReady];
  160. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  161. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  162. [vc overrideOptions:options];
  163. [_stackManager popTo:vc animated:vc.resolveOptions.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  164. [_eventEmitter sendOnNavigationCommandCompletion:popTo params:@{@"componentId": componentId}];
  165. [self removePopedViewControllers:poppedViewControllers];
  166. completion();
  167. } rejection:rejection];
  168. }
  169. - (void)popToRoot:(NSString*)componentId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  170. [self assertReady];
  171. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  172. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  173. [vc overrideOptions:options];
  174. [CATransaction begin];
  175. [CATransaction setCompletionBlock:^{
  176. [_eventEmitter sendOnNavigationCommandCompletion:popToRoot params:@{@"componentId": componentId}];
  177. completion();
  178. }];
  179. [_stackManager popToRoot:vc animated:vc.resolveOptions.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  180. [self removePopedViewControllers:poppedViewControllers];
  181. } rejection:^(NSString *code, NSString *message, NSError *error) {
  182. }];
  183. [CATransaction commit];
  184. }
  185. - (void)showModal:(NSDictionary*)layout completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  186. [self assertReady];
  187. UIViewController<RNNParentProtocol> *newVc = [_controllerFactory createLayout:layout saveToStore:_store];
  188. [newVc.getCurrentLeaf waitForReactViewRender:newVc.getCurrentLeaf.resolveOptions.animations.showModal.waitForRender perform:^{
  189. [_modalManager showModal:newVc animated:newVc.getCurrentChild.resolveOptions.animations.showModal.enable hasCustomAnimation:newVc.getCurrentChild.resolveOptions.animations.showModal.hasCustomAnimation completion:^(NSString *componentId) {
  190. [_eventEmitter sendOnNavigationCommandCompletion:showModal params:@{@"layout": layout}];
  191. completion(componentId);
  192. }];
  193. }];
  194. }
  195. - (void)dismissModal:(NSString*)componentId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  196. [self assertReady];
  197. UIViewController<RNNParentProtocol> *modalToDismiss = (UIViewController<RNNParentProtocol>*)[_store findComponentForId:componentId];
  198. if (!modalToDismiss.isModal) {
  199. [RNNErrorHandler reject:reject withErrorCode:1013 errorDescription:@"component is not a modal"];
  200. return;
  201. }
  202. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  203. [modalToDismiss.getCurrentChild overrideOptions:options];
  204. [self removePopedViewControllers:modalToDismiss.navigationController.viewControllers];
  205. [CATransaction begin];
  206. [CATransaction setCompletionBlock:^{
  207. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  208. }];
  209. [_modalManager dismissModal:modalToDismiss completion:completion];
  210. [CATransaction commit];
  211. }
  212. - (void)dismissAllModals:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
  213. [self assertReady];
  214. [CATransaction begin];
  215. [CATransaction setCompletionBlock:^{
  216. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals params:@{}];
  217. completion();
  218. }];
  219. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  220. [_modalManager dismissAllModalsAnimated:options.animations.dismissModal.enable];
  221. [CATransaction commit];
  222. }
  223. - (void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  224. [self assertReady];
  225. UIViewController<RNNParentProtocol>* overlayVC = [_controllerFactory createLayout:layout saveToStore:_store];
  226. UIWindow* overlayWindow = [[RNNOverlayWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  227. overlayWindow.rootViewController = overlayVC;
  228. [_overlayManager showOverlayWindow:overlayWindow];
  229. [_eventEmitter sendOnNavigationCommandCompletion:showOverlay params:@{@"layout": layout}];
  230. completion();
  231. }
  232. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  233. [self assertReady];
  234. UIViewController* viewController = [_store findComponentForId:componentId];
  235. if (viewController) {
  236. [_overlayManager dismissOverlay:viewController];
  237. [_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay params:@{@"componentId": componentId}];
  238. completion();
  239. } else {
  240. [RNNErrorHandler reject:reject withErrorCode:1010 errorDescription:@"ComponentId not found"];
  241. }
  242. }
  243. #pragma mark - private
  244. - (void)removePopedViewControllers:(NSArray*)viewControllers {
  245. for (UIViewController *popedVC in viewControllers) {
  246. [_store removeComponentByViewControllerInstance:popedVC];
  247. }
  248. }
  249. - (void)assertReady {
  250. if (!_store.isReadyToReceiveCommands) {
  251. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  252. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  253. userInfo:nil]
  254. raise];
  255. }
  256. }
  257. #pragma mark - RNNModalManagerDelegate
  258. - (void)dismissedModal:(UIViewController<RNNParentProtocol> *)viewController {
  259. [_eventEmitter sendModalsDismissedEvent:viewController.layoutInfo.componentId numberOfModalsDismissed:@(1)];
  260. }
  261. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  262. if (viewControllers && viewControllers.count) {
  263. [_eventEmitter sendModalsDismissedEvent:((UIViewController<RNNParentProtocol> *)viewControllers.lastObject).layoutInfo.componentId numberOfModalsDismissed:@(viewControllers.count)];
  264. }
  265. }
  266. @end