react-native-navigation的迁移库

RNNCommandsHandler.m 2.5KB

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