react-native-navigation的迁移库

RNNCommandsHandler.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. id animationDelegate = (newVc.options.animations.push.hasCustomAnimation || newVc.isCustomTransitioned) ? newVc : nil;
  104. [newVc waitForReactViewRender:newVc.options.animations.push.waitForRender perform:^{
  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. RNNNavigationOptions* options = [newVC getLeafViewController].options;
  116. UIViewController *fromVC = [_store findComponentForId:componentId];
  117. __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
  118. [_stackManager setStackRoot:newVC fromViewController:fromVC animated:options.animations.push.enable completion:^{
  119. [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot params:@{@"componentId": componentId}];
  120. completion();
  121. } rejection:rejection];
  122. }
  123. -(void)pop:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  124. [self assertReady];
  125. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  126. UINavigationController *nvc = vc.navigationController;
  127. if ([nvc topViewController] == vc) {
  128. if (vc.options.animations.pop) {
  129. nvc.delegate = vc;
  130. } else {
  131. nvc.delegate = nil;
  132. }
  133. } else {
  134. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  135. [vcs removeObject:vc];
  136. [nvc setViewControllers:vcs animated:vc.options.animations.pop.enable];
  137. }
  138. [_stackManager pop:vc animated:vc.options.animations.pop.enable completion:^{
  139. [_store removeComponent:componentId];
  140. [_eventEmitter sendOnNavigationCommandCompletion:pop params:@{@"componentId": componentId}];
  141. completion();
  142. } rejection:^(NSString *code, NSString *message, NSError *error) {
  143. }];
  144. }
  145. -(void) popTo:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  146. [self assertReady];
  147. RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
  148. [_stackManager popTo:vc animated:vc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  149. [_eventEmitter sendOnNavigationCommandCompletion:popTo params:@{@"componentId": componentId}];
  150. [self removePopedViewControllers:poppedViewControllers];
  151. completion();
  152. } rejection:^(NSString *code, NSString *message, NSError *error) {
  153. }];
  154. }
  155. -(void) popToRoot:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  156. [self assertReady];
  157. RNNRootViewController *newVc = (RNNRootViewController*)[_store findComponentForId:componentId];
  158. [CATransaction begin];
  159. [CATransaction setCompletionBlock:^{
  160. [_eventEmitter sendOnNavigationCommandCompletion:popToRoot params:@{@"componentId": componentId}];
  161. completion();
  162. }];
  163. [_stackManager popToRoot:newVc animated:newVc.options.animations.pop.enable completion:^(NSArray *poppedViewControllers) {
  164. [self removePopedViewControllers:poppedViewControllers];
  165. } rejection:^(NSString *code, NSString *message, NSError *error) {
  166. }];
  167. [CATransaction commit];
  168. }
  169. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  170. [self assertReady];
  171. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  172. [_modalManager showModal:newVc completion:^{
  173. [_eventEmitter sendOnNavigationCommandCompletion:showModal params:@{@"layout": layout}];
  174. completion();
  175. }];
  176. }
  177. -(void) dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  178. [self assertReady];
  179. [CATransaction begin];
  180. [CATransaction setCompletionBlock:^{
  181. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  182. completion();
  183. }];
  184. [_modalManager dismissModal:componentId];
  185. [CATransaction commit];
  186. }
  187. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  188. [self assertReady];
  189. [CATransaction begin];
  190. [CATransaction setCompletionBlock:^{
  191. [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals params:@{}];
  192. completion();
  193. }];
  194. [_modalManager dismissAllModals];
  195. [CATransaction commit];
  196. }
  197. -(void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  198. [self assertReady];
  199. UIViewController<RNNRootViewProtocol>* overlayVC = [_controllerFactory createOverlay:layout];
  200. [_overlayManager showOverlay:overlayVC completion:^{
  201. [_eventEmitter sendOnNavigationCommandCompletion:showOverlay params:@{@"layout": layout}];
  202. completion();
  203. }];
  204. }
  205. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
  206. [self assertReady];
  207. [_overlayManager dismissOverlay:componentId completion:^{
  208. [_eventEmitter sendOnNavigationCommandCompletion:dismissModal params:@{@"componentId": componentId}];
  209. completion();
  210. } rejection:reject];
  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. @end