react-native-navigation的迁移库

RNNCommandsHandler.m 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  36. }
  37. -(void) push:(NSString*)containerId layout:(NSDictionary*)layout {
  38. [self assertReady];
  39. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  40. [_navigationStackManager push:newVc onTop:containerId];
  41. }
  42. -(void) pop:(NSString*)containerId {
  43. [self assertReady];
  44. [_navigationStackManager pop:containerId];
  45. }
  46. -(void) popTo:(NSString*)containerId {
  47. [self assertReady];
  48. [_navigationStackManager popTo:containerId];
  49. }
  50. -(void) popToRoot:(NSString*)containerId {
  51. [self assertReady];
  52. [_navigationStackManager popToRoot:containerId];
  53. }
  54. -(void) showModal:(NSDictionary*)layout {
  55. [self assertReady];
  56. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  57. [_modalManager showModal:newVc];
  58. }
  59. -(void) dismissModal:(NSString*)containerId {
  60. [self assertReady];
  61. [_modalManager dismissModal:containerId];
  62. }
  63. -(void) dismissAllModals {
  64. [self assertReady];
  65. [_modalManager dismissAllModals];
  66. }
  67. #pragma mark - private
  68. -(void) assertReady {
  69. if (!_store.isReadyToReceiveCommands) {
  70. [[NSException exceptionWithName:@"BridgeNotLoadedError"
  71. reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
  72. userInfo:nil]
  73. raise];
  74. }
  75. }
  76. @end