react-native-navigation的迁移库

RNNCommandsHandler.m 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. [_controllerFactory setDefaultOptionsDict:optionsDict];
  48. }
  49. -(void)push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  50. [self assertReady];
  51. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  52. [_navigationStackManager push:newVc onTop:componentId completion:^{
  53. completion();
  54. }];
  55. }
  56. -(void)setStackRoot:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  57. [self assertReady];
  58. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  59. [_navigationStackManager setRoot:newVc fromComponent:componentId completion:^{
  60. completion();
  61. }];
  62. }
  63. -(void)pop:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  64. [self assertReady];
  65. [CATransaction begin];
  66. [CATransaction setCompletionBlock:^{
  67. completion();
  68. }];
  69. NSDictionary* animationData = options[@"customTransition"];
  70. RNNAnimationOptions* transitionOptions = [[RNNAnimationOptions alloc] initWithDict:animationData];
  71. if (transitionOptions.animations){
  72. [_navigationStackManager pop:componentId withTransitionOptions:transitionOptions];
  73. } else {
  74. [_navigationStackManager pop:componentId withTransitionOptions:nil];
  75. }
  76. [CATransaction commit];
  77. }
  78. -(void) popTo:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  79. [self assertReady];
  80. [CATransaction begin];
  81. [CATransaction setCompletionBlock:^{
  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. completion();
  92. }];
  93. [_navigationStackManager popToRoot:componentId];
  94. [CATransaction commit];
  95. }
  96. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  97. [self assertReady];
  98. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  99. [_modalManager showModal:newVc completion:^{
  100. completion();
  101. }];
  102. }
  103. -(void) dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  104. [self assertReady];
  105. [CATransaction begin];
  106. [CATransaction setCompletionBlock:^{
  107. completion();
  108. }];
  109. [_modalManager dismissModal:componentId];
  110. [CATransaction commit];
  111. }
  112. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  113. [self assertReady];
  114. [CATransaction begin];
  115. [CATransaction setCompletionBlock:^{
  116. completion();
  117. }];
  118. [_modalManager dismissAllModals];
  119. [CATransaction commit];
  120. }
  121. -(void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  122. [self assertReady];
  123. UIViewController<RNNRootViewProtocol>* overlayVC = [_controllerFactory createOverlay:layout];
  124. [_overlayManager showOverlay:overlayVC completion:^{
  125. completion();
  126. }];
  127. }
  128. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  129. [self assertReady];
  130. [_overlayManager dismissOverlay:componentId completion:^{
  131. completion();
  132. }];
  133. }
  134. #pragma mark - private
  135. -(void) assertReady {
  136. if (!_store.isReadyToReceiveCommands) {
  137. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  138. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  139. userInfo:nil]
  140. raise];
  141. }
  142. }
  143. @end