react-native-navigation的迁移库

RNNNavigationButtons.m 9.2KB

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