react-native-navigation的迁移库

RNNNavigationButtons.m 7.8KB

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