react-native-navigation的迁移库

RNNNavigationButtons.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #import "RNNNavigationButtons.h"
  2. #import "RNNUIBarButtonItem.h"
  3. #import <React/RCTConvert.h>
  4. #import "RCTHelpers.h"
  5. @interface RNNNavigationButtons()
  6. @property (weak, nonatomic) RNNRootViewController* viewController;
  7. @property (strong, nonatomic) NSArray* rightButtons;
  8. @property (strong, nonatomic) NSArray* leftButtons;
  9. @property (strong, nonatomic) RNNButtonOptions* defaultLeftButtonStyle;
  10. @property (strong, nonatomic) RNNButtonOptions* defaultRightButtonStyle;
  11. @end
  12. @implementation RNNNavigationButtons
  13. -(instancetype)initWithViewController:(RNNRootViewController*)viewController {
  14. self = [super init];
  15. self.viewController = viewController;
  16. return self;
  17. }
  18. - (void)applyLeftButtons:(NSArray *)leftButtons rightButtons:(NSArray *)rightButtons defaultLeftButtonStyle:(RNNButtonOptions *)defaultLeftButtonStyle defaultRightButtonStyle:(RNNButtonOptions *)defaultRightButtonStyle {
  19. _defaultLeftButtonStyle = defaultLeftButtonStyle;
  20. _defaultRightButtonStyle = defaultRightButtonStyle;
  21. if (leftButtons) {
  22. [self setButtons:leftButtons side:@"left" animated:NO defaultStyle:_defaultLeftButtonStyle];
  23. }
  24. if (rightButtons) {
  25. [self setButtons:rightButtons side:@"right" animated:NO defaultStyle:_defaultRightButtonStyle];
  26. }
  27. }
  28. -(void)setButtons:(NSArray*)buttons side:(NSString*)side animated:(BOOL)animated defaultStyle:(RNNButtonOptions *)defaultStyle {
  29. NSMutableArray *barButtonItems = [NSMutableArray new];
  30. NSArray* resolvedButtons = [self resolveButtons:buttons];
  31. for (NSDictionary *button in resolvedButtons) {
  32. RNNUIBarButtonItem* barButtonItem = [self buildButton:button defaultStyle:defaultStyle];
  33. if(barButtonItem) {
  34. [barButtonItems addObject:barButtonItem];
  35. }
  36. }
  37. if ([side isEqualToString:@"left"]) {
  38. self.leftButtons = barButtonItems;
  39. [self.viewController.navigationItem setLeftBarButtonItems:self.leftButtons animated:animated];
  40. }
  41. if ([side isEqualToString:@"right"]) {
  42. self.rightButtons = barButtonItems;
  43. [self.viewController.navigationItem setRightBarButtonItems:self.rightButtons animated:animated];
  44. }
  45. }
  46. - (NSArray *)resolveButtons:(id)buttons {
  47. if ([buttons isKindOfClass:[NSArray class]]) {
  48. return buttons;
  49. } else {
  50. return @[buttons];
  51. }
  52. }
  53. -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNButtonOptions *)defaultStyle {
  54. NSString* buttonId = dictionary[@"id"];
  55. NSString* title = [self getValue:dictionary[@"text"] withDefault:defaultStyle.text];
  56. NSDictionary* component = dictionary[@"component"];
  57. if (!buttonId) {
  58. @throw [NSException exceptionWithName:@"NSInvalidArgumentException" reason:[@"button id is not specified " stringByAppendingString:title] userInfo:nil];
  59. }
  60. UIImage* iconImage = nil;
  61. id icon = [self getValue:dictionary[@"icon"] withDefault:defaultStyle.icon];
  62. if (icon) {
  63. iconImage = [RCTConvert UIImage:icon];
  64. }
  65. RNNUIBarButtonItem *barButtonItem;
  66. if (component) {
  67. RCTRootView *view = (RCTRootView*)[self.viewController.creator createRootView:component[@"name"] rootViewId:component[@"componentId"]];
  68. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withCustomView:view];
  69. } else if (iconImage) {
  70. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withIcon:iconImage];
  71. } else if (title) {
  72. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withTitle:title];
  73. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:dictionary withPrefix:@"button"];
  74. if (buttonTextAttributes.allKeys.count > 0) {
  75. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  76. }
  77. } else {
  78. return nil;
  79. }
  80. barButtonItem.target = self;
  81. barButtonItem.action = @selector(onButtonPress:);
  82. NSNumber *enabled = [self getValue:dictionary[@"enabled"] withDefault:defaultStyle.enabled];
  83. BOOL enabledBool = enabled ? [enabled boolValue] : YES;
  84. [barButtonItem setEnabled:enabledBool];
  85. NSMutableDictionary* textAttributes = [[NSMutableDictionary alloc] init];
  86. NSMutableDictionary* disabledTextAttributes = [[NSMutableDictionary alloc] init];
  87. UIColor* color = [self color:dictionary[@"color"] defaultColor:defaultStyle.color];
  88. if (color) {
  89. [textAttributes setObject:color forKey:NSForegroundColorAttributeName];
  90. [barButtonItem setImage:iconImage];
  91. [barButtonItem setTintColor:color];
  92. }
  93. UIColor* disabledColor = [self color:dictionary[@"disabledColor"] defaultColor:defaultStyle.disabledColor];;
  94. if (disabledColor) {
  95. UIColor *color = disabledColor;
  96. [disabledTextAttributes setObject:color forKey:NSForegroundColorAttributeName];
  97. }
  98. NSNumber* fontSize = [self fontSize:dictionary[@"fontSize"] defaultFontSize:defaultStyle.fontSize];
  99. NSString* fontFamily = [self fontFamily:dictionary[@"fontFamily"] defaultFontFamily:defaultStyle.fontFamily];
  100. UIFont *font = nil;
  101. if (fontFamily) {
  102. font = [UIFont fontWithName:fontFamily size:[fontSize floatValue]];
  103. } else {
  104. font = [UIFont systemFontOfSize:[fontSize floatValue]];
  105. }
  106. [textAttributes setObject:font forKey:NSFontAttributeName];
  107. [disabledTextAttributes setObject:font forKey:NSFontAttributeName];
  108. [barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
  109. [barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted];
  110. [barButtonItem setTitleTextAttributes:disabledTextAttributes forState:UIControlStateDisabled];
  111. NSString *testID = dictionary[@"testID"];
  112. if (testID)
  113. {
  114. barButtonItem.accessibilityIdentifier = testID;
  115. }
  116. return barButtonItem;
  117. }
  118. - (UIColor *)color:(NSNumber *)color defaultColor:(NSNumber *)defaultColor {
  119. if (color) {
  120. return [RCTConvert UIColor:color];
  121. } else if (defaultColor) {
  122. return [RCTConvert UIColor:defaultColor];
  123. }
  124. return nil;
  125. }
  126. - (NSNumber *)fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize {
  127. if (fontSize) {
  128. return fontSize;
  129. } else if (defaultFontSize) {
  130. return defaultFontSize;
  131. }
  132. return @(17);
  133. }
  134. - (NSString *)fontFamily:(NSString *)fontFamily defaultFontFamily:(NSString *)defaultFontFamily {
  135. if (fontFamily) {
  136. return fontFamily;
  137. } else {
  138. return defaultFontFamily;
  139. }
  140. }
  141. - (id)getValue:(id)value withDefault:(id)defaultValue {
  142. return value ? value : defaultValue;
  143. }
  144. -(void)onButtonPress:(RNNUIBarButtonItem*)barButtonItem {
  145. [self.viewController.eventEmitter sendOnNavigationButtonPressed:self.viewController.componentId buttonId:barButtonItem.buttonId];
  146. }
  147. @end