react-native-navigation的迁移库

RNNNavigationButtons.m 8.1KB

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