react-native-navigation的迁移库

RNNCommandsHandler.m 14KB

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