react-native-navigation的迁移库

RNNCommandsHandler.m 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #import "RNNCommandsHandler.h"
  2. #import "RNNModalManager.h"
  3. #import "RNNNavigationStackManager.h"
  4. @implementation RNNCommandsHandler {
  5. RNNControllerFactory *_controllerFactory;
  6. RNNStore *_store;
  7. RNNNavigationStackManager* _navigationStackManager;
  8. RNNModalManager* _modalManager;
  9. }
  10. -(instancetype) initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory {
  11. self = [super init];
  12. _store = store;
  13. _controllerFactory = controllerFactory;
  14. _navigationStackManager = [[RNNNavigationStackManager alloc] initWithStore:_store];
  15. _modalManager = [[RNNModalManager alloc] initWithStore:_store];
  16. return self;
  17. }
  18. #pragma mark - public
  19. -(void) setRoot:(NSDictionary*)layout {
  20. [self assertReady];
  21. [_modalManager dismissAllModals];
  22. UIViewController *vc = [_controllerFactory createLayoutAndSaveToStore:layout];
  23. UIApplication.sharedApplication.delegate.window.rootViewController = vc;
  24. [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
  25. }
  26. -(void) setOptions:(NSString*)containerId options:(NSDictionary*)options {
  27. [self assertReady];
  28. [[_store findContainerForId:containerId] setTitle:options[@"title"]];
  29. }
  30. -(void) push:(NSString*)containerId layout:(NSDictionary*)layout {
  31. [self assertReady];
  32. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  33. [_navigationStackManager push:newVc onTop:containerId];
  34. }
  35. -(void) pop:(NSString*)containerId {
  36. [self assertReady];
  37. [_navigationStackManager pop:containerId];
  38. }
  39. -(void) popTo:(NSString*)containerId {
  40. [self assertReady];
  41. [_navigationStackManager popTo:containerId];
  42. }
  43. -(void) popToRoot:(NSString*)containerId {
  44. [self assertReady];
  45. [_navigationStackManager popToRoot:containerId];
  46. }
  47. -(void) showModal:(NSDictionary*)layout {
  48. [self assertReady];
  49. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  50. [_modalManager showModal:newVc];
  51. }
  52. -(void) dismissModal:(NSString*)containerId {
  53. [self assertReady];
  54. [_modalManager dismissModal:containerId];
  55. }
  56. -(void) dismissAllModals {
  57. [self assertReady];
  58. [_modalManager dismissAllModals];
  59. }
  60. #pragma mark - private
  61. -(void) assertReady {
  62. if (!_store.isReadyToReceiveCommands) {
  63. @throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
  64. }
  65. }
  66. @end