react-native-navigation的迁移库

RNNCommandsHandler.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. 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. }
  35. - (instancetype)initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter {
  36. self = [super init];
  37. _store = store;
  38. _controllerFactory = controllerFactory;
  39. _eventEmitter = eventEmitter;
  40. _modalManager = [[RNNModalManager alloc] init];
  41. _modalManager.delegate = self;
  42. _stackManager = [[RNNNavigationStackManager alloc] init];
  43. _overlayManager = [[RNNOverlayManager alloc] init];
  44. return self;
  45. }
  46. #pragma mark - public
  47. - (void)setRoot:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  48. [self assertReady];
  49. [_modalManager dismissAllModals];
  50. UIViewController *vc = [_controllerFactory createLayoutAndSaveToStore:layout[@"root"]];
  51. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  52. [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
  53. [_eventEmitter sendOnNavigationCommandCompletion:setRoot params:@{@"layout": layout}];
  54. completion();
  55. }
  56. - (void)mergeOptions:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  57. [self assertReady];
  58. UIViewController* vc = [_store findComponentForId:componentId];
  59. if([vc isKindOfClass:[RNNRootViewController class]]) {
  60. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  61. [rootVc.options mergeWith:options];
  62. [CATransaction begin];
  63. [CATransaction setCompletionBlock:completion];
  64. [rootVc.options applyOn:vc];
  65. [CATransaction commit];
  66. }
  67. if ([vc isKindOfClass:[RNNSplitViewController class]]) {
  68. RNNSplitViewController* splitVc = (RNNSplitViewController*)vc;
  69. [splitVc.options mergeWith:options];
  70. [CATransaction begin];
  71. [CATransaction setCompletionBlock:completion];
  72. [splitVc.options applyOn:vc];
  73. [CATransaction commit];
  74. }
  75. }
  76. - (void)setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
  77. [self assertReady];
  78. [_controllerFactory setDefaultOptionsDict:optionsDict];
  79. }
  80. - (void)push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  81. [self assertReady];
  82. RNNRootViewController *newVc = (RNNRootViewController *)[_controllerFactory createLayoutAndSaveToStore:layout];
  83. UIViewController *fromVC = [_store findComponentForId:componentId];
  84. if (newVc.options.preview.elementId) {
  85. UIViewController* vc = [_store findComponentForId:componentId];
  86. if([vc isKindOfClass:[RNNRootViewController class]]) {
  87. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  88. rootVc.previewController = newVc;
  89. RNNElementFinder* elementFinder = [[RNNElementFinder alloc] initWithFromVC:vc];
  90. RNNElementView* elementView = [elementFinder findElementForId:newVc.options.preview.elementId];
  91. CGSize size = CGSizeMake(rootVc.view.frame.size.width, rootVc.view.frame.size.height);
  92. if (newVc.options.preview.width) {
  93. size.width = [newVc.options.preview.width floatValue];
  94. }
  95. if (newVc.options.preview.height) {
  96. size.height = [newVc.options.preview.height floatValue];
  97. }
  98. if (newVc.options.preview.width || newVc.options.preview.height) {
  99. newVc.preferredContentSize = size;
  100. }
  101. [rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:elementView];
  102. }
  103. } else {
  104. id animationDelegate = (newVc.options.animations.push.hasCustomAnimation || newVc.isCustomTransitioned) ? newVc : nil;
  105. [newVc waitForReactViewRender:(newVc.options.animations.push.waitForRender || animationDelegate) perform:^{
  106. [_stackManager push:newVc onTop:fromVC animated:newVc.options.animations.push.enable animationDelegate:animationDelegate completion:^{
  107. [_eventEmitter sendOnNavigationCommandCompletion:push params:@{@"componentId": componentId}];
  108. completion();
  109. } rejection:rejection];
  110. }];
  111. }
  112. }
  113. - (void)setStackRoot:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  114. [self assertReady];
  115. UIViewController<RNNRootViewProtocol> *newVC = [_controllerFactory createLayoutAndSaveToStore:layout];
  116. RNNNavigationOptions* options = [newVC getLeafViewController].options;
  117. UIViewController *fromVC = [_store findComponentForId:componentId];
  118. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  119. [_stackManager setStackRoot:newVC fromViewController:fromVC animated:options.animations.setStackRoot.enable completion:^{
  120. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot params:@{@"componentId": componentId}];
  121. completion();
  122. } rejection:rejection];
  123. }
  124. - (void)pop:(NSString*)componentId options:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  125. [self assertReady];
  126. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  127. [vc.options mergeWith:optionsDict];
  128. UINavigationController *nvc = vc.navigationController;
  129. if ([nvc topViewController] == vc) {
  130. if (vc.options.animations.pop) {
  131. nvc.delegate = vc;
  132. } else {
  133. nvc.delegate = nil;
  134. }
  135. } else {
  136. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  137. [vcs removeObject:vc];
  138. [nvc setViewControllers:vcs animated:vc.options.animations.pop.enable];
  139. }
  140. [_stackManager pop:vc animated:vc.options.animations.pop.enable completion:^{
  141. [_store removeComponent:componentId];
  142. [_eventEmitter sendOnNavigationCommandCompletion:pop params:@{@"componentId": componentId}];
  143. completion();
  144. } rejection:^(NSString *code, NSString *message, NSError *error) {
  145. }];
  146. }
  147. - (void)popTo:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  148. [self assertReady];
  149. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  150. [_stackManager popTo:vc animated:vc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  151. [_eventEmitter sendOnNavigationCommandCompletion:popTo params:@{@"componentId": componentId}];
  152. [self removePopedViewControllers:poppedViewControllers];
  153. completion();
  154. } rejection:rejection];
  155. }
  156. -(void) popToRoot:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  157. [self assertReady];
  158. RNNRootViewController *newVc = (RNNRootViewController*)[_store findComponentForId:componentId];
  159. [CATransaction begin];
  160. [CATransaction setCompletionBlock:^{
  161. [_eventEmitter sendOnNavigationCommandCompletion:popToRoot params:@{@"componentId": componentId}];
  162. completion();
  163. }];
  164. [_stackManager popToRoot:newVc animated:newVc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  165. [self removePopedViewControllers:poppedViewControllers];
  166. } rejection:^(NSString *code, NSString *message, NSError *error) {
  167. }];
  168. [CATransaction commit];
  169. }
  170. - (void)showModal:(NSDictionary*)layout completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
  171. [self assertReady];
  172. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  173. [_modalManager showModal:newVc animated:newVc.getLeafViewController.options.animations.showModal.enable completion:^(NSString *componentId) {
  174. [_eventEmitter sendOnNavigationCommandCompletion:showModal params:@{@"layout": layout}];
  175. completion(componentId);
  176. }];
  177. }
  178. - (void)dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  179. [self assertReady];
  180. [CATransaction begin];
  181. [CATransaction setCompletionBlock:^{
  182. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  183. }];
  184. UIViewController<RNNRootViewProtocol> *modalToDismiss = (UIViewController<RNNRootViewProtocol>*)[_store findComponentForId:componentId];
  185. [self removePopedViewControllers:modalToDismiss.navigationController.viewControllers];
  186. [_modalManager dismissModal:modalToDismiss completion:completion];
  187. [CATransaction commit];
  188. }
  189. - (void)dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  190. [self assertReady];
  191. [CATransaction begin];
  192. [CATransaction setCompletionBlock:^{
  193. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals params:@{}];
  194. completion();
  195. }];
  196. [_modalManager dismissAllModals];
  197. [CATransaction commit];
  198. }
  199. - (void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  200. [self assertReady];
  201. UIViewController<RNNRootViewProtocol>* overlayVC = [_controllerFactory createOverlay:layout];
  202. [_overlayManager showOverlay:overlayVC];
  203. [_eventEmitter sendOnNavigationCommandCompletion:showOverlay params:@{@"layout": layout}];
  204. completion();
  205. }
  206. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  207. [self assertReady];
  208. UIViewController* viewController = [_store findComponentForId:componentId];
  209. if (viewController) {
  210. [_overlayManager dismissOverlay:viewController];
  211. [_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay params:@{@"componentId": componentId}];
  212. completion();
  213. } else {
  214. [RNNErrorHandler reject:reject withErrorCode:1010 errorDescription:@"ComponentId not found"];
  215. }
  216. }
  217. #pragma mark - private
  218. - (void)removePopedViewControllers:(NSArray*)viewControllers {
  219. for (UIViewController *popedVC in viewControllers) {
  220. [_store removeComponentByViewControllerInstance:popedVC];
  221. }
  222. }
  223. - (void)assertReady {
  224. if (!_store.isReadyToReceiveCommands) {
  225. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  226. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  227. userInfo:nil]
  228. raise];
  229. }
  230. }
  231. #pragma mark - RNNModalManagerDelegate
  232. - (void)dismissedModal:(UIViewController *)viewController {
  233. [_eventEmitter sendModalsDismissedEvent:((RNNRootViewController *)viewController).componentId numberOfModalsDismissed:@(1)];
  234. }
  235. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  236. if (viewControllers && viewControllers.count) {
  237. [_eventEmitter sendModalsDismissedEvent:((RNNRootViewController *)viewControllers.lastObject).componentId numberOfModalsDismissed:@(viewControllers.count)];
  238. }
  239. }
  240. @end