react-native-navigation的迁移库

RNNCommandsHandler.m 2.2KB

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