react-native-navigation的迁移库

RNNCommandsHandler.m 2.9KB

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