react-native-navigation的迁移库

RNNCommandsHandler.m 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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) showModal:(NSDictionary*)layout {
  36. [self assertReady];
  37. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  38. [_modalManager showModal:newVc];
  39. }
  40. -(void) dismissModal:(NSString*)containerId {
  41. [self assertReady];
  42. [_modalManager dismissModal:containerId];
  43. }
  44. -(void) dismissAllModals {
  45. [self assertReady];
  46. [_modalManager dismissAllModals];
  47. }
  48. #pragma mark - private
  49. -(void) assertReady {
  50. if (!_store.isReadyToReceiveCommands) {
  51. @throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
  52. }
  53. }
  54. @end