react-native-navigation的迁移库

RNNNavigationButtons.m 6.3KB

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