react-native-navigation的迁移库

RNNCommandsHandler.m 6.4KB

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