react-native-navigation的迁移库

RNNCommandsHandler.m 14KB

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