react-native-navigation的迁移库

RNNCommandsHandler.m 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "React/RCTUIManager.h"
  8. static NSString* const setRoot = @"setRoot";
  9. static NSString* const setStackRoot = @"setStackRoot";
  10. static NSString* const push = @"push";
  11. static NSString* const pop = @"pop";
  12. static NSString* const popTo = @"popTo";
  13. static NSString* const popToRoot = @"popToRoot";
  14. static NSString* const showModal = @"showModal";
  15. static NSString* const dismissModal = @"dismissModal";
  16. static NSString* const dismissAllModals = @"dismissAllModals";
  17. static NSString* const showOverlay = @"showOverlay";
  18. static NSString* const dismissOverlay = @"dismissOverlay";
  19. static NSString* const mergeOptions = @"mergeOptions";
  20. static NSString* const setDefaultOptions = @"setDefaultOptions";
  21. @implementation RNNCommandsHandler {
  22. RNNControllerFactory *_controllerFactory;
  23. RNNStore *_store;
  24. RNNNavigationStackManager* _navigationStackManager;
  25. RNNModalManager* _modalManager;
  26. RNNOverlayManager* _overlayManager;
  27. RNNEventEmitter* _eventEmitter;
  28. }
  29. -(instancetype) initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter {
  30. self = [super init];
  31. _store = store;
  32. _controllerFactory = controllerFactory;
  33. _eventEmitter = eventEmitter;
  34. _navigationStackManager = [[RNNNavigationStackManager alloc] initWithStore:_store];
  35. _modalManager = [[RNNModalManager alloc] initWithStore:_store];
  36. _overlayManager = [[RNNOverlayManager alloc] initWithStore:_store];
  37. return self;
  38. }
  39. #pragma mark - public
  40. -(void) setRoot:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  41. [self assertReady];
  42. [_modalManager dismissAllModals];
  43. [_eventEmitter sendOnNavigationComment:setRoot params:@{@"layout": layout}];
  44. UIViewController *vc = [_controllerFactory createLayoutAndSaveToStore:layout];
  45. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  46. [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
  47. completion();
  48. }
  49. -(void) mergeOptions:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  50. [self assertReady];
  51. [_eventEmitter sendOnNavigationComment:mergeOptions params:@{@"componentId": componentId, @"options": options}];
  52. UIViewController* vc = [_store findComponentForId:componentId];
  53. if([vc isKindOfClass:[RNNRootViewController class]]) {
  54. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  55. [rootVc.options mergeWith:options];
  56. [CATransaction begin];
  57. [CATransaction setCompletionBlock:completion];
  58. [rootVc.options applyOn:vc];
  59. [CATransaction commit];
  60. }
  61. }
  62. -(void) setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
  63. [self assertReady];
  64. [_eventEmitter sendOnNavigationComment:setDefaultOptions params:@{@"options": optionsDict}];
  65. [_controllerFactory setDefaultOptionsDict:optionsDict];
  66. }
  67. -(void)push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  68. [self assertReady];
  69. [_eventEmitter sendOnNavigationComment:push params:@{@"componentId": componentId}];
  70. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  71. [_navigationStackManager push:newVc onTop:componentId completion:^{
  72. completion();
  73. }];
  74. }
  75. -(void)setStackRoot:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  76. [self assertReady];
  77. [_eventEmitter sendOnNavigationComment:setStackRoot params:@{@"componentId": componentId}];
  78. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  79. [_navigationStackManager setRoot:newVc fromComponent:componentId completion:^{
  80. completion();
  81. }];
  82. }
  83. -(void)pop:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  84. [self assertReady];
  85. [_eventEmitter sendOnNavigationComment:pop params:@{@"componentId": componentId}];
  86. [CATransaction begin];
  87. [CATransaction setCompletionBlock:^{
  88. completion();
  89. }];
  90. NSDictionary* animationData = options[@"customTransition"];
  91. RNNAnimationOptions* transitionOptions = [[RNNAnimationOptions alloc] initWithDict:animationData];
  92. if (transitionOptions.animations){
  93. [_navigationStackManager pop:componentId withTransitionOptions:transitionOptions];
  94. } else {
  95. [_navigationStackManager pop:componentId withTransitionOptions:nil];
  96. }
  97. [CATransaction commit];
  98. }
  99. -(void) popTo:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  100. [self assertReady];
  101. [_eventEmitter sendOnNavigationComment:popTo params:@{@"componentId": componentId}];
  102. [CATransaction begin];
  103. [CATransaction setCompletionBlock:^{
  104. completion();
  105. }];
  106. [_navigationStackManager popTo:componentId];
  107. [CATransaction commit];
  108. }
  109. -(void) popToRoot:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  110. [self assertReady];
  111. [_eventEmitter sendOnNavigationComment:popToRoot params:@{@"componentId": componentId}];
  112. [CATransaction begin];
  113. [CATransaction setCompletionBlock:^{
  114. completion();
  115. }];
  116. [_navigationStackManager popToRoot:componentId];
  117. [CATransaction commit];
  118. }
  119. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  120. [self assertReady];
  121. [_eventEmitter sendOnNavigationComment:showModal params:@{@"layout": layout}];
  122. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  123. [_modalManager showModal:newVc completion:^{
  124. completion();
  125. }];
  126. }
  127. -(void) dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  128. [self assertReady];
  129. [_eventEmitter sendOnNavigationComment:dismissModal params:@{@"componentId": componentId}];
  130. [CATransaction begin];
  131. [CATransaction setCompletionBlock:^{
  132. completion();
  133. }];
  134. [_modalManager dismissModal:componentId];
  135. [CATransaction commit];
  136. }
  137. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  138. [self assertReady];
  139. [_eventEmitter sendOnNavigationComment:dismissAllModals params:nil];
  140. [CATransaction begin];
  141. [CATransaction setCompletionBlock:^{
  142. completion();
  143. }];
  144. [_modalManager dismissAllModals];
  145. [CATransaction commit];
  146. }
  147. -(void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  148. [self assertReady];
  149. [_eventEmitter sendOnNavigationComment:showOverlay params:@{@"layout": layout}];
  150. UIViewController<RNNRootViewProtocol>* overlayVC = [_controllerFactory createOverlay:layout];
  151. [_overlayManager showOverlay:overlayVC completion:^{
  152. completion();
  153. }];
  154. }
  155. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  156. [self assertReady];
  157. [_eventEmitter sendOnNavigationComment:dismissModal params:@{@"componentId": componentId}];
  158. [_overlayManager dismissOverlay:componentId completion:^{
  159. completion();
  160. }];
  161. }
  162. #pragma mark - private
  163. -(void) assertReady {
  164. if (!_store.isReadyToReceiveCommands) {
  165. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  166. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  167. userInfo:nil]
  168. raise];
  169. }
  170. }
  171. @end