react-native-navigation的迁移库

RNNCommandsHandler.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. UIViewController* vc = [_store findContainerForId:containerId];
  29. NSString* title = options[@"title"];
  30. NSString* topBarTextColor = options[@"topBarTextColor"];
  31. [vc setTitle:title];
  32. int rgb = [[topBarTextColor substringFromIndex:1] intValue];
  33. UIColor* color = [UIColor colorWithRed:((float)((rgb & 0xFF0000) >> 16))/255.0 \
  34. green:((float)((rgb & 0x00FF00) >> 8))/255.0 \
  35. blue:((float)((rgb & 0x0000FF) >> 0))/255.0 \
  36. alpha:1.0];
  37. vc.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: color};
  38. }
  39. -(void) push:(NSString*)containerId layout:(NSDictionary*)layout {
  40. [self assertReady];
  41. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  42. [_navigationStackManager push:newVc onTop:containerId];
  43. }
  44. -(void) pop:(NSString*)containerId {
  45. [self assertReady];
  46. [_navigationStackManager pop:containerId];
  47. }
  48. -(void) popTo:(NSString*)containerId {
  49. [self assertReady];
  50. [_navigationStackManager popTo:containerId];
  51. }
  52. -(void) popToRoot:(NSString*)containerId {
  53. [self assertReady];
  54. [_navigationStackManager popToRoot:containerId];
  55. }
  56. -(void) showModal:(NSDictionary*)layout {
  57. [self assertReady];
  58. UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
  59. [_modalManager showModal:newVc];
  60. }
  61. -(void) dismissModal:(NSString*)containerId {
  62. [self assertReady];
  63. [_modalManager dismissModal:containerId];
  64. }
  65. -(void) dismissAllModals {
  66. [self assertReady];
  67. [_modalManager dismissAllModals];
  68. }
  69. #pragma mark - private
  70. -(void) assertReady {
  71. if (!_store.isReadyToReceiveCommands) {
  72. @throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
  73. }
  74. }
  75. @end