react-native-navigation的迁移库

RNNNavigationButtons.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. [self notifyButtonsDidAppear:barButtonItems];
  56. }
  57. - (NSArray *)currentButtons {
  58. NSArray* currentButtons = [self.viewController.navigationItem.leftBarButtonItems arrayByAddingObjectsFromArray:self.viewController.navigationItem.rightBarButtonItems];
  59. return currentButtons;
  60. }
  61. - (void)componentDidAppear {
  62. for (RNNUIBarButtonItem* barButtonItem in [self currentButtons]) {
  63. [barButtonItem notifyDidAppear];
  64. }
  65. }
  66. - (void)componentDidDisappear {
  67. for (RNNUIBarButtonItem* barButtonItem in [self currentButtons]) {
  68. [barButtonItem notifyDidDisappear];
  69. }
  70. }
  71. - (void)notifyButtonsDidAppear:(NSArray *)barButtonItems {
  72. for (RNNUIBarButtonItem* barButtonItem in barButtonItems) {
  73. [barButtonItem notifyDidAppear];
  74. }
  75. }
  76. - (void)clearPreviousButtonViews:(NSArray<UIBarButtonItem *> *)newButtons oldButtons:(NSArray<UIBarButtonItem *> *)oldButtons {
  77. NSArray<UIBarButtonItem *>* removedButtons = [oldButtons difference:newButtons withPropertyName:@"customView"];
  78. for (UIBarButtonItem* buttonItem in removedButtons) {
  79. RNNReactView* reactView = buttonItem.customView;
  80. if ([reactView isKindOfClass:[RNNReactView class]]) {
  81. [reactView componentDidDisappear];
  82. [_componentRegistry removeChildComponent:reactView.componentId];
  83. }
  84. }
  85. }
  86. - (NSArray *)resolveButtons:(id)buttons {
  87. if ([buttons isKindOfClass:[NSArray class]]) {
  88. return buttons;
  89. } else {
  90. return @[buttons];
  91. }
  92. }
  93. -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNButtonOptions *)defaultStyle insets:(UIEdgeInsets)insets {
  94. NSString* buttonId = dictionary[@"id"];
  95. NSString* title = [self getValue:dictionary[@"text"] withDefault:[defaultStyle.text getWithDefaultValue:nil]];
  96. NSDictionary* component = dictionary[@"component"];
  97. NSString* systemItemName = dictionary[@"systemItem"];
  98. UIColor* color = [self color:[RCTConvert UIColor:dictionary[@"color"]] defaultColor:[defaultStyle.color getWithDefaultValue:nil]];
  99. UIColor* disabledColor = [self color:[RCTConvert UIColor:dictionary[@"disabledColor"]] defaultColor:[defaultStyle.disabledColor getWithDefaultValue:nil]];
  100. if (!buttonId) {
  101. @throw [NSException exceptionWithName:@"NSInvalidArgumentException" reason:[@"button id is not specified " stringByAppendingString:title] userInfo:nil];
  102. }
  103. UIImage* defaultIcon = [defaultStyle.icon getWithDefaultValue:nil];
  104. UIImage* iconImage = [self getValue:dictionary[@"icon"] withDefault:defaultIcon];
  105. if (![iconImage isKindOfClass:[UIImage class]]) {
  106. iconImage = [RCTConvert UIImage:iconImage];
  107. }
  108. if (iconImage) {
  109. iconImage = [iconImage imageWithInsets:insets];
  110. if (color) {
  111. iconImage = [iconImage withTintColor:color];
  112. }
  113. }
  114. RNNUIBarButtonItem *barButtonItem;
  115. if (component) {
  116. RNNComponentOptions* componentOptions = [RNNComponentOptions new];
  117. componentOptions.componentId = [[Text alloc] initWithValue:component[@"componentId"]];
  118. componentOptions.name = [[Text alloc] initWithValue:component[@"name"]];
  119. RNNReactButtonView *view = [_componentRegistry createComponentIfNotExists:componentOptions parentComponentId:self.viewController.layoutInfo.componentId componentType:RNNComponentTypeTopBarButton reactViewReadyBlock:nil];
  120. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withCustomView:view];
  121. } else if (iconImage) {
  122. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withIcon:iconImage];
  123. } else if (title) {
  124. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withTitle:title];
  125. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:dictionary withPrefix:@"button"];
  126. if (buttonTextAttributes.allKeys.count > 0) {
  127. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  128. }
  129. } else if (systemItemName) {
  130. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withSystemItem:systemItemName];
  131. } else {
  132. return nil;
  133. }
  134. barButtonItem.target = self.viewController;
  135. barButtonItem.action = @selector(onButtonPress:);
  136. NSNumber *enabled = [self getValue:dictionary[@"enabled"] withDefault:defaultStyle.enabled.getValue];
  137. BOOL enabledBool = enabled ? [enabled boolValue] : YES;
  138. [barButtonItem setEnabled:enabledBool];
  139. 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]];
  140. 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]];
  141. if (!enabledBool && disabledColor) {
  142. color = disabledColor;
  143. [disabledTextAttributes setObject:disabledColor forKey:NSForegroundColorAttributeName];
  144. }
  145. if (color) {
  146. [textAttributes setObject:color forKey:NSForegroundColorAttributeName];
  147. [barButtonItem setImage:[[iconImage withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  148. barButtonItem.tintColor = color;
  149. }
  150. [barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
  151. [barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted];
  152. [barButtonItem setTitleTextAttributes:disabledTextAttributes forState:UIControlStateDisabled];
  153. NSString *testID = dictionary[@"testID"];
  154. if (testID)
  155. {
  156. barButtonItem.accessibilityIdentifier = testID;
  157. }
  158. return barButtonItem;
  159. }
  160. - (UIColor *)color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
  161. if (color) {
  162. return color;
  163. } else if (defaultColor) {
  164. return defaultColor;
  165. }
  166. return nil;
  167. }
  168. - (id)getValue:(id)value withDefault:(id)defaultValue {
  169. return value ? value : defaultValue;
  170. }
  171. - (UIEdgeInsets)leftButtonInsets:(RNNInsetsOptions *)defaultInsets {
  172. return UIEdgeInsetsMake([defaultInsets.top getWithDefaultValue:0],
  173. [defaultInsets.left getWithDefaultValue:0],
  174. [defaultInsets.bottom getWithDefaultValue:0],
  175. [defaultInsets.right getWithDefaultValue:15]);
  176. }
  177. - (UIEdgeInsets)rightButtonInsets:(RNNInsetsOptions *)defaultInsets {
  178. return UIEdgeInsetsMake([defaultInsets.top getWithDefaultValue:0],
  179. [defaultInsets.left getWithDefaultValue:15],
  180. [defaultInsets.bottom getWithDefaultValue:0],
  181. [defaultInsets.right getWithDefaultValue:0]);
  182. }
  183. @end