react-native-navigation的迁移库

RNNNavigationButtons.m 7.1KB

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