react-native-navigation的迁移库

RNNCommandsHandler.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #import "RNNCommandsHandler.h"
  2. #import "RNNModalManager.h"
  3. #import "RNNNavigationStackManager.h"
  4. #import "RNNOverlayManager.h"
  5. #import "RNNNavigationOptions.h"
  6. #import "RNNRootViewController.h"
  7. #import "RNNSplitViewController.h"
  8. #import "RNNElementFinder.h"
  9. #import "React/RCTUIManager.h"
  10. #import "RNNErrorHandler.h"
  11. #import "RNNDefaultOptionsHelper.h"
  12. #import "UIViewController+RNNOptions.h"
  13. static NSString* const setRoot = @"setRoot";
  14. static NSString* const setStackRoot = @"setStackRoot";
  15. static NSString* const push = @"push";
  16. static NSString* const preview = @"preview";
  17. static NSString* const pop = @"pop";
  18. static NSString* const popTo = @"popTo";
  19. static NSString* const popToRoot = @"popToRoot";
  20. static NSString* const showModal = @"showModal";
  21. static NSString* const dismissModal = @"dismissModal";
  22. static NSString* const dismissAllModals = @"dismissAllModals";
  23. static NSString* const showOverlay = @"showOverlay";
  24. static NSString* const dismissOverlay = @"dismissOverlay";
  25. static NSString* const mergeOptions = @"mergeOptions";
  26. static NSString* const setDefaultOptions = @"setDefaultOptions";
  27. @interface RNNCommandsHandler() <RNNModalManagerDelegate>
  28. @end
  29. @implementation RNNCommandsHandler {
  30. RNNControllerFactory *_controllerFactory;
  31. RNNStore *_store;
  32. RNNModalManager* _modalManager;
  33. RNNOverlayManager* _overlayManager;
  34. RNNNavigationStackManager* _stackManager;
  35. RNNEventEmitter* _eventEmitter;
  36. }
  37. - (instancetype)initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter {
  38. self = [super init];
  39. _store = store;
  40. _controllerFactory = controllerFactory;
  41. _eventEmitter = eventEmitter;
  42. _modalManager = [[RNNModalManager alloc] init];
  43. _modalManager.delegate = self;
  44. _stackManager = [[RNNNavigationStackManager alloc] init];
  45. _overlayManager = [[RNNOverlayManager alloc] init];
  46. return self;
  47. }
  48. #pragma mark - public
  49. - (void)setRoot:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  50. [self assertReady];
  51. [_modalManager dismissAllModalsAnimated:NO];
  52. [_store removeAllComponents];
  53. UIViewController *vc = [_controllerFactory createLayoutAndSaveToStore:layout[@"root"]];
  54. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  55. [UIApplication.sharedApplication.delegate.window makeKeyWindow];
  56. [_eventEmitter sendOnNavigationCommandCompletion:setRoot params:@{@"layout": layout}];
  57. completion();
  58. }
  59. - (void)mergeOptions:(NSString*)componentId options:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
  60. [self assertReady];
  61. UIViewController<RNNLayoutProtocol>* vc = (UIViewController<RNNLayoutProtocol>*)[_store findComponentForId:componentId];
  62. RNNNavigationOptions* newOptions = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  63. if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] || [vc isKindOfClass:[RNNRootViewController class]]) {
  64. [CATransaction begin];
  65. [CATransaction setCompletionBlock:completion];
  66. [vc.options overrideOptions:newOptions];
  67. [vc mergeOptions:newOptions];
  68. [CATransaction commit];
  69. }
  70. }
  71. - (void)setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
  72. [self assertReady];
  73. RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initWithDict:optionsDict];
  74. [_controllerFactory setDefaultOptions:defaultOptions];
  75. UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window.rootViewController;
  76. [RNNDefaultOptionsHelper recrusivelySetDefaultOptions:defaultOptions onRootViewController:rootViewController];
  77. completion();
  78. }
  79. - (void)push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  80. [self assertReady];
  81. UIViewController<RNNLayoutProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  82. UIViewController *fromVC = [_store findComponentForId:componentId];
  83. if ([newVc.options.preview.reactTag floatValue] > 0) {
  84. UIViewController* vc = [_store findComponentForId:componentId];
  85. if([vc isKindOfClass:[RNNRootViewController class]]) {
  86. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  87. rootVc.previewController = newVc;
  88. rootVc.previewCallback = ^(UIViewController *vcc) {
  89. RNNRootViewController* rvc = (RNNRootViewController*)vcc;
  90. [self->_eventEmitter sendOnPreviewCompleted:componentId previewComponentId:newVc.layoutInfo.componentId];
  91. if ([newVc.options.preview.commit floatValue] > 0) {
  92. [CATransaction begin];
  93. [CATransaction setCompletionBlock:^{
  94. [self->_eventEmitter sendOnNavigationCommandCompletion:push params:@{@"componentId": componentId}];
  95. completion();
  96. }];
  97. [rvc.navigationController pushViewController:newVc animated:YES];
  98. [CATransaction commit];
  99. }
  100. };
  101. CGSize size = CGSizeMake(rootVc.view.frame.size.width, rootVc.view.frame.size.height);
  102. if (newVc.options.preview.width) {
  103. size.width = [newVc.options.preview.width floatValue];
  104. }
  105. if (newVc.options.preview.height) {
  106. size.height = [newVc.options.preview.height floatValue];
  107. }
  108. if (newVc.options.preview.width || newVc.options.preview.height) {
  109. newVc.preferredContentSize = size;
  110. }
  111. RCTExecuteOnMainQueue(^{
  112. UIView *view = [[ReactNativeNavigation getBridge].uiManager viewForReactTag:newVc.options.preview.reactTag];
  113. [rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:view];
  114. });
  115. }
  116. } else {
  117. id animationDelegate = (newVc.options.animations.push.hasCustomAnimation || newVc.getCurrentChild.isCustomTransitioned) ? newVc : nil;
  118. [newVc.getCurrentChild waitForReactViewRender:(newVc.options.animations.push.waitForRender || animationDelegate) perform:^{
  119. [_stackManager push:newVc onTop:fromVC animated:newVc.options.animations.push.enable animationDelegate:animationDelegate completion:^{
  120. [_eventEmitter sendOnNavigationCommandCompletion:push params:@{@"componentId": componentId}];
  121. completion();
  122. } rejection:rejection];
  123. }];
  124. }
  125. }
  126. - (void)setStackRoot:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  127. [self assertReady];
  128. UIViewController<RNNParentProtocol> *newVC = [_controllerFactory createLayoutAndSaveToStore:layout];
  129. RNNNavigationOptions* options = [newVC getCurrentChild].options;
  130. UIViewController *fromVC = [_store findComponentForId:componentId];
  131. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  132. [_stackManager setStackRoot:newVC fromViewController:fromVC animated:options.animations.setStackRoot.enable completion:^{
  133. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot params:@{@"componentId": componentId}];
  134. completion();
  135. } rejection:rejection];
  136. }
  137. - (void)pop:(NSString*)componentId mergeOptions:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  138. [self assertReady];
  139. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  140. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  141. [vc.options overrideOptions:options];
  142. UINavigationController *nvc = vc.navigationController;
  143. if ([nvc topViewController] == vc) {
  144. if (vc.options.animations.pop) {
  145. nvc.delegate = vc;
  146. } else {
  147. nvc.delegate = nil;
  148. }
  149. } else {
  150. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  151. [vcs removeObject:vc];
  152. [nvc setViewControllers:vcs animated:vc.options.animations.pop.enable];
  153. }
  154. [_stackManager pop:vc animated:vc.options.animations.pop.enable completion:^{
  155. [_store removeComponent:componentId];
  156. [_eventEmitter sendOnNavigationCommandCompletion:pop params:@{@"componentId": componentId}];
  157. completion();
  158. } rejection:rejection];
  159. }
  160. - (void)popTo:(NSString*)componentId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  161. [self assertReady];
  162. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  163. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  164. [vc.options overrideOptions:options];
  165. [_stackManager popTo:vc animated:vc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  166. [_eventEmitter sendOnNavigationCommandCompletion:popTo params:@{@"componentId": componentId}];
  167. [self removePopedViewControllers:poppedViewControllers];
  168. completion();
  169. } rejection:rejection];
  170. }
  171. - (void)popToRoot:(NSString*)componentId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  172. [self assertReady];
  173. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  174. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  175. [vc.options overrideOptions:options];
  176. [CATransaction begin];
  177. [CATransaction setCompletionBlock:^{
  178. [_eventEmitter sendOnNavigationCommandCompletion:popToRoot params:@{@"componentId": componentId}];
  179. completion();
  180. }];
  181. [_stackManager popToRoot:vc animated:vc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  182. [self removePopedViewControllers:poppedViewControllers];
  183. } rejection:^(NSString *code, NSString *message, NSError *error) {
  184. }];
  185. [CATransaction commit];
  186. }
  187. - (void)showModal:(NSDictionary*)layout completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  188. [self assertReady];
  189. UIViewController<RNNParentProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  190. [newVc.getCurrentChild waitForReactViewRender:newVc.getCurrentChild.options.animations.showModal.waitForRender perform:^{
  191. [_modalManager showModal:newVc animated:newVc.getCurrentChild.options.animations.showModal.enable hasCustomAnimation:newVc.getCurrentChild.options.animations.showModal.hasCustomAnimation completion:^(NSString *componentId) {
  192. [_eventEmitter sendOnNavigationCommandCompletion:showModal params:@{@"layout": layout}];
  193. completion(componentId);
  194. }];
  195. }];
  196. }
  197. - (void)dismissModal:(NSString*)componentId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  198. [self assertReady];
  199. UIViewController<RNNParentProtocol> *modalToDismiss = (UIViewController<RNNParentProtocol>*)[_store findComponentForId:componentId];
  200. if (!modalToDismiss.isModal) {
  201. [RNNErrorHandler reject:reject withErrorCode:1013 errorDescription:@"component is not a modal"];
  202. return;
  203. }
  204. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  205. [modalToDismiss.getCurrentChild.options overrideOptions:options];
  206. [self removePopedViewControllers:modalToDismiss.navigationController.viewControllers];
  207. [CATransaction begin];
  208. [CATransaction setCompletionBlock:^{
  209. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  210. }];
  211. [_modalManager dismissModal:modalToDismiss completion:completion];
  212. [CATransaction commit];
  213. }
  214. - (void)dismissAllModals:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
  215. [self assertReady];
  216. [CATransaction begin];
  217. [CATransaction setCompletionBlock:^{
  218. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals params:@{}];
  219. completion();
  220. }];
  221. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
  222. [_modalManager dismissAllModalsAnimated:options.animations.dismissModal.enable];
  223. [CATransaction commit];
  224. }
  225. - (void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  226. [self assertReady];
  227. UIViewController<RNNParentProtocol>* overlayVC = [_controllerFactory createLayoutAndSaveToStore:layout];
  228. [_overlayManager showOverlay:overlayVC];
  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