react-native-navigation的迁移库

RNNCommandsHandler.m 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #import "RNNCommandsHandler.h"
  2. #import "RNNModalManager.h"
  3. #import "RNNNavigationStackManager.h"
  4. #import "RNNNavigationOptions.h"
  5. #import "RNNRootViewController.h"
  6. #import "React/RCTUIManager.h"
  7. @implementation RNNCommandsHandler {
  8. RNNControllerFactory *_controllerFactory;
  9. RNNStore *_store;
  10. RNNNavigationStackManager* _navigationStackManager;
  11. RNNModalManager* _modalManager;
  12. }
  13. -(instancetype) initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory {
  14. self = [super init];
  15. _store = store;
  16. _controllerFactory = controllerFactory;
  17. _navigationStackManager = [[RNNNavigationStackManager alloc] initWithStore:_store];
  18. _modalManager = [[RNNModalManager alloc] initWithStore:_store];
  19. return self;
  20. }
  21. #pragma mark - public
  22. -(void) setRoot:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  23. [self assertReady];
  24. [_modalManager dismissAllModals];
  25. UIViewController *vc = [_controllerFactory createLayoutAndSaveToStore:layout];
  26. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  27. [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
  28. completion();
  29. }
  30. -(void) setOptions:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  31. [self assertReady];
  32. UIViewController* vc = [_store findComponentForId:componentId];
  33. if([vc isKindOfClass:[RNNRootViewController class]]) {
  34. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  35. [rootVc.navigationOptions mergeWith:options];
  36. [CATransaction begin];
  37. [CATransaction setCompletionBlock:completion];
  38. [rootVc.navigationOptions applyOn:vc];
  39. [CATransaction commit];
  40. }
  41. }
  42. -(void) push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  43. [self assertReady];
  44. UIViewController<RNNRootViewProtocol> *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  45. [_navigationStackManager push:newVc onTop:componentId completion:completion];
  46. }
  47. -(void)pop:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion {
  48. [self assertReady];
  49. [CATransaction begin];
  50. [CATransaction setCompletionBlock:completion];
  51. NSDictionary* animationData = options[@"customTransition"];
  52. if (animationData){
  53. if ([animationData objectForKey:@"animations"]) {
  54. [_navigationStackManager pop:componentId withAnimationData:animationData];
  55. } else {
  56. [[NSException exceptionWithName:NSInvalidArgumentException reason:@"unsupported transitionAnimation" userInfo:nil] raise];
  57. }
  58. } else {
  59. [_navigationStackManager pop:componentId withAnimationData:nil];
  60. }
  61. [CATransaction commit];
  62. }
  63. -(void) popTo:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  64. [self assertReady];
  65. [CATransaction begin];
  66. [CATransaction setCompletionBlock:completion];
  67. [_navigationStackManager popTo:componentId];
  68. [CATransaction commit];
  69. }
  70. -(void) popToRoot:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  71. [self assertReady];
  72. [CATransaction begin];
  73. [CATransaction setCompletionBlock:completion];
  74. [_navigationStackManager popToRoot:componentId];
  75. [CATransaction commit];
  76. }
  77. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  78. [self assertReady];
  79. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  80. [_modalManager showModal:newVc completion:completion];
  81. }
  82. -(void) dismissModal:(NSString*)componentId completion:(RNNTransitionCompletionBlock)completion {
  83. [self assertReady];
  84. [CATransaction begin];
  85. [CATransaction setCompletionBlock:completion];
  86. [_modalManager dismissModal:componentId];
  87. [CATransaction commit];
  88. }
  89. -(void) dismissAllModalsWithCompletion:(RNNTransitionCompletionBlock)completion {
  90. [self assertReady];
  91. [CATransaction begin];
  92. [CATransaction setCompletionBlock:completion];
  93. [_modalManager dismissAllModals];
  94. [CATransaction commit];
  95. }
  96. #pragma mark - private
  97. -(void) assertReady {
  98. if (!_store.isReadyToReceiveCommands) {
  99. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  100. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  101. userInfo:nil]
  102. raise];
  103. }
  104. }
  105. @end