react-native-navigation的迁移库

RNNCommandsHandler.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. [_eventEmitter sendOnNavigationCommand:setRoot params:@{@"layout": layout}];
  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. [_eventEmitter sendOnNavigationCommand:mergeOptions params:@{@"componentId": componentId, @"options": options}];
  56. UIViewController* vc = [_store findComponentForId:componentId];
  57. if([vc isKindOfClass:[RNNRootViewController class]]) {
  58. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  59. [rootVc.options mergeWith:options];
  60. [CATransaction begin];
  61. [CATransaction setCompletionBlock:completion];
  62. [rootVc.options applyOn:vc];
  63. [CATransaction commit];
  64. }
  65. if ([vc isKindOfClass:[RNNSplitViewController class]]) {
  66. RNNSplitViewController* splitVc = (RNNSplitViewController*)vc;
  67. [splitVc.options mergeWith:options];
  68. [CATransaction begin];
  69. [CATransaction setCompletionBlock:completion];
  70. [splitVc.options applyOn:vc];
  71. [CATransaction commit];
  72. }
  73. }
  74. -(void) setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
  75. [self assertReady];
  76. [_eventEmitter sendOnNavigationCommand:setDefaultOptions params:@{@"options": optionsDict}];
  77. [_controllerFactory setDefaultOptionsDict:optionsDict];
  78. }
  79. -(void)push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  80. [self assertReady];
  81. RNNRootViewController *newVc = (RNNRootViewController *)[_controllerFactory createLayoutAndSaveToStore:layout];
  82. UIViewController *fromVC = [_store findComponentForId:componentId];
  83. if (newVc.options.preview.elementId) {
  84. UIViewController* vc = [_store findComponentForId:componentId];
  85. if([vc isKindOfClass:[RNNRootViewController class]]) {
  86. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  87. rootVc.previewController = newVc;
  88. RNNElementFinder* elementFinder = [[RNNElementFinder alloc] initWithFromVC:vc];
  89. RNNElementView* elementView = [elementFinder findElementForId:newVc.options.preview.elementId];
  90. CGSize size = CGSizeMake(rootVc.view.frame.size.width, rootVc.view.frame.size.height);
  91. if (newVc.options.preview.width) {
  92. size.width = [newVc.options.preview.width floatValue];
  93. }
  94. if (newVc.options.preview.height) {
  95. size.height = [newVc.options.preview.height floatValue];
  96. }
  97. if (newVc.options.preview.width || newVc.options.preview.height) {
  98. newVc.preferredContentSize = size;
  99. }
  100. [rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:elementView];
  101. }
  102. } else {
  103. [newVc onReactViewReady:^{
  104. id animationDelegate = (newVc.options.animations.push.hasCustomAnimation || newVc.isCustomTransitioned) ? newVc : nil;
  105. [_stackManager push:newVc onTop:fromVC animated:newVc.options.animations.push.enable animationDelegate:animationDelegate completion:^{
  106. [_eventEmitter sendOnNavigationCommandCompletion:push params:@{@"componentId": componentId}];
  107. completion();
  108. } rejection:rejection];
  109. }];
  110. }
  111. }
  112. -(void)setStackRoot:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  113. [self assertReady];
  114. UIViewController<RNNRootViewProtocol> *newVC = [_controllerFactory createLayoutAndSaveToStore:layout];
  115. UIViewController *fromVC = [_store findComponentForId:componentId];
  116. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  117. [_stackManager setStackRoot:newVC fromViewController:fromVC animated:newVC.options.animations.push.enable completion:^{
  118. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot params:@{@"componentId": componentId}];
  119. completion();
  120. } rejection:rejection];
  121. }
  122. -(void)pop:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  123. [self assertReady];
  124. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  125. UINavigationController *nvc = vc.navigationController;
  126. if ([nvc topViewController] == vc) {
  127. if (vc.options.animations.pop) {
  128. nvc.delegate = vc;
  129. } else {
  130. nvc.delegate = nil;
  131. }
  132. } else {
  133. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  134. [vcs removeObject:vc];
  135. [nvc setViewControllers:vcs animated:vc.options.animations.pop.enable];
  136. }
  137. [_stackManager pop:vc animated:vc.options.animations.pop.enable completion:^{
  138. [_store removeComponent:componentId];
  139. [_eventEmitter sendOnNavigationCommandCompletion:pop params:@{@"componentId": componentId}];
  140. completion();
  141. } rejection:^(NSString *code, NSString *message, NSError *error) {
  142. }];
  143. }
  144. -(void) popTo:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  145. [self assertReady];
  146. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  147. [_stackManager popTo:vc animated:vc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  148. [_eventEmitter sendOnNavigationCommandCompletion:popTo params:@{@"componentId": componentId}];
  149. [self removePopedViewControllers:poppedViewControllers];
  150. completion();
  151. } rejection:^(NSString *code, NSString *message, NSError *error) {
  152. }];
  153. }
  154. -(void) popToRoot:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  155. [self assertReady];
  156. RNNRootViewController *newVc = (RNNRootViewController*)[_store findComponentForId:componentId];
  157. [CATransaction begin];
  158. [CATransaction setCompletionBlock:^{
  159. [_eventEmitter sendOnNavigationCommandCompletion:popToRoot params:@{@"componentId": componentId}];
  160. completion();
  161. }];
  162. [_stackManager popToRoot:newVc animated:newVc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  163. [self removePopedViewControllers:poppedViewControllers];
  164. } rejection:^(NSString *code, NSString *message, NSError *error) {
  165. }];
  166. [CATransaction commit];
  167. }
  168. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  169. [self assertReady];
  170. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  171. [_modalManager showModal:newVc completion:^{
  172. [_eventEmitter sendOnNavigationCommandCompletion:showModal params:@{@"layout": layout}];
  173. completion();
  174. }];
  175. }
  176. -(void) dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  177. [self assertReady];
  178. [CATransaction begin];
  179. [CATransaction setCompletionBlock:^{
  180. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  181. completion();
  182. }];
  183. [_modalManager dismissModal:componentId];
  184. [CATransaction commit];
  185. }
  186. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  187. [self assertReady];
  188. [CATransaction begin];
  189. [CATransaction setCompletionBlock:^{
  190. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals params:@{}];
  191. completion();
  192. }];
  193. [_modalManager dismissAllModals];
  194. [CATransaction commit];
  195. }
  196. -(void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  197. [self assertReady];
  198. UIViewController<RNNRootViewProtocol>* overlayVC = [_controllerFactory createOverlay:layout];
  199. [_overlayManager showOverlay:overlayVC completion:^{
  200. [_eventEmitter sendOnNavigationCommandCompletion:showOverlay params:@{@"layout": layout}];
  201. completion();
  202. }];
  203. }
  204. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  205. [self assertReady];
  206. [_overlayManager dismissOverlay:componentId completion:^{
  207. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  208. completion();
  209. } rejection:reject];
  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