react-native-navigation的迁移库

RNNCommandsHandler.m 5.2KB

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