react-native-navigation的迁移库

RNNCommandsHandler.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. RNNTransitionOptions* transitionOptions = [[RNNTransitionOptions alloc] initWithDict:animationData];
  67. if (transitionOptions){
  68. if (transitionOptions.animations) {
  69. [_navigationStackManager pop:componentId withTransitionOptions:transitionOptions];
  70. } else {
  71. [[NSException exceptionWithName:NSInvalidArgumentException reason:@"unsupported transitionAnimation" userInfo:nil] raise];
  72. }
  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. [_controllerFactory.eventEmitter sendNavigationEvent:[RNNNavigationEvent create:kPopTo fromComponent:nil toComponent:componentId]];
  83. completion();
  84. }];
  85. [_navigationStackManager popTo:componentId];
  86. [CATransaction commit];
  87. }
  88. -(void) popToRoot:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  89. [self assertReady];
  90. [CATransaction begin];
  91. [CATransaction setCompletionBlock:^{
  92. [_controllerFactory.eventEmitter sendNavigationEvent:[RNNNavigationEvent create:kPopToRoot fromComponent:componentId toComponent:nil]];
  93. completion();
  94. }];
  95. [_navigationStackManager popToRoot:componentId];
  96. [CATransaction commit];
  97. }
  98. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  99. [self assertReady];
  100. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  101. [_modalManager showModal:newVc completion:^{
  102. [_controllerFactory.eventEmitter sendNavigationEvent:[RNNNavigationEvent create:kShowModal fromComponent:nil toComponent:newVc.componentId]];
  103. completion();
  104. }];
  105. }
  106. -(void) dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  107. [self assertReady];
  108. [CATransaction begin];
  109. [CATransaction setCompletionBlock:^{
  110. [_controllerFactory.eventEmitter sendNavigationEvent:[RNNNavigationEvent create:kDismissModal fromComponent:componentId toComponent:nil]];
  111. completion();
  112. }];
  113. [_modalManager dismissModal:componentId];
  114. [CATransaction commit];
  115. }
  116. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  117. [self assertReady];
  118. [CATransaction begin];
  119. [CATransaction setCompletionBlock:^{
  120. [_controllerFactory.eventEmitter sendNavigationEvent:[RNNNavigationEvent create:kDismissAllModals fromComponent:nil toComponent:nil]];
  121. completion();
  122. }];
  123. [_modalManager dismissAllModals];
  124. [CATransaction commit];
  125. }
  126. -(void)showOverlay:(NSDictionary *)layout completion:(RNNTransitionCompletionBlock)completion {
  127. [self assertReady];
  128. UIViewController<RNNRootViewProtocol>* overlayVC = [_controllerFactory createOverlay:layout];
  129. [_overlayManager showOverlay:overlayVC completion:^{
  130. [_controllerFactory.eventEmitter sendNavigationEvent:[RNNNavigationEvent create:kShowOverlay fromComponent:nil toComponent:overlayVC.componentId]];
  131. completion();
  132. }];
  133. }
  134. - (void)dismissOverlay:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  135. [self assertReady];
  136. [_overlayManager dismissOverlay:componentId completion:^{
  137. [_controllerFactory.eventEmitter sendNavigationEvent:[RNNNavigationEvent create:kDismissOverlay fromComponent:componentId toComponent:nil]];
  138. completion();
  139. }];
  140. }
  141. #pragma mark - private
  142. -(void) assertReady {
  143. if (!_store.isReadyToReceiveCommands) {
  144. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  145. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  146. userInfo:nil]
  147. raise];
  148. }
  149. }
  150. @end