react-native-navigation的迁移库

RNNNavigationButtons.m 8.1KB

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