react-native-navigation的迁移库

RNNCommandsHandler.m 2.1KB

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