react-native-navigation的迁移库

RNNCommandsHandler.m 3.6KB

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