react-native-navigation的迁移库

RNNCommandsHandler.m 10KB

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