react-native-navigation的迁移库

RNNCommandsHandler.m 11KB

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