react-native-navigation的迁移库

RNNCommandsHandler.m 5.0KB

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