react-native-navigation的迁移库

RNNCommandsHandler.m 12KB

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