react-native-navigation的迁移库

RNNCommandsHandler.m 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. RCTBridge* _bridge;
  11. RNNNavigationStackManager* _navigationStackManager;
  12. RNNModalManager* _modalManager;
  13. }
  14. -(instancetype) initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory andBridge:(RCTBridge*)bridge {
  15. self = [super init];
  16. _bridge = bridge;
  17. _store = store;
  18. _controllerFactory = controllerFactory;
  19. _navigationStackManager = [[RNNNavigationStackManager alloc] initWithStore:_store];
  20. _modalManager = [[RNNModalManager alloc] initWithStore:_store];
  21. return self;
  22. }
  23. #pragma mark - public
  24. -(void) setRoot:(NSDictionary*)layout {
  25. [self assertReady];
  26. [_modalManager dismissAllModals];
  27. UIViewController *vc = [_controllerFactory createLayoutAndSaveToStore:layout];
  28. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  29. [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
  30. }
  31. -(void) setOptions:(NSString*)containerId options:(NSDictionary*)options {
  32. [self assertReady];
  33. UIViewController* vc = [_store findContainerForId:containerId];
  34. if([vc isKindOfClass:[RNNRootViewController class]]) {
  35. RNNRootViewController* rootVc = (RNNRootViewController*)vc;
  36. [rootVc.navigationOptions mergeWith:options];
  37. [rootVc.navigationOptions applyOn:vc];
  38. [rootVc applyNavigationButtons];
  39. }
  40. }
  41. -(void) push:(NSString*)containerId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  42. [self assertReady];
  43. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  44. UIViewController *fromVc = [_store findContainerForId:containerId];
  45. [_bridge.uiManager setAvailableSize:fromVc.view.bounds.size forRootView:newVc.view];
  46. [_navigationStackManager push:newVc onTop:containerId completion:completion];
  47. }
  48. -(void)pop:(NSString*)containerId options:(NSDictionary*)options{
  49. [self assertReady];
  50. NSDictionary* animationData = options[@"customTransition"];
  51. if (animationData){
  52. if ([animationData objectForKey:@"animations"]) {
  53. [_navigationStackManager pop:containerId withAnimationData:animationData];
  54. } else {
  55. [[NSException exceptionWithName:NSInvalidArgumentException reason:@"unsupported transitionAnimation" userInfo:nil] raise];
  56. }
  57. } else {
  58. [_navigationStackManager pop:containerId withAnimationData:nil];
  59. }
  60. }
  61. -(void) popTo:(NSString*)containerId {
  62. [self assertReady];
  63. [_navigationStackManager popTo:containerId];
  64. }
  65. -(void) popToRoot:(NSString*)containerId {
  66. [self assertReady];
  67. [_navigationStackManager popToRoot:containerId];
  68. }
  69. -(void) showModal:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
  70. [self assertReady];
  71. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  72. [_modalManager showModal:newVc completion:completion];
  73. }
  74. -(void) dismissModal:(NSString*)containerId {
  75. [self assertReady];
  76. [_modalManager dismissModal:containerId];
  77. }
  78. -(void) dismissAllModals {
  79. [self assertReady];
  80. [_modalManager dismissAllModals];
  81. }
  82. #pragma mark - private
  83. -(void) assertReady {
  84. if (!_store.isReadyToReceiveCommands) {
  85. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  86. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  87. userInfo:nil]
  88. raise];
  89. }
  90. }
  91. @end