// // ScanModelListCell.m // HMLG Scan Order // // Created by Rui Zhang on 3/17/22. // Copyright © 2022 United Software Applications, Inc. All rights reserved. // #import "ScanModelListCell.h" #import "RAConvertor.h" #import "RAUtils.h" #import "RASingleton.h" #import "AppDelegate.h" #import "RADataProvider.h" static const CGFloat kTableRowHeight = 24.0; static const CGFloat kCellHPadding = 20.0; static const CGFloat kMinColumnWidth = 125; @implementation ScanModelListCell - (void)awakeFromNib { [super awakeFromNib]; self.selectionStyle = UITableViewCellSelectionStyleNone; self.priceScrollView.showsVerticalScrollIndicator = NO; self.priceScrollView.showsHorizontalScrollIndicator = YES; self.priceScrollView.alwaysBounceVertical = NO; self.priceScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; } // MARK: - setModelJson - (void)setModelJson:(NSMutableDictionary *)modelJson { _modelJson = modelJson; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; NSDictionary *price_group = [RADataProvider get_price_group:appDelegate.price_group]; // ── 基础字段 ──────────────────────────────────────────────────────────── NSString *port = _modelJson[@"port"]; if (port.length == 0) port = @"N/A"; NSString *origin = _modelJson[@"origin"]; if (origin.length == 0) origin = @"N/A"; NSString *available = _modelJson[@"available"]; if (available.length == 0) available = @"N/A"; NSString *unit_cuft = _modelJson[@"unit_cuft"]; if (unit_cuft.length == 0) { unit_cuft = @"N/A"; } else { unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]]; } // ── 静态 label ────────────────────────────────────────────────────────── _labelModel.text = _modelJson[@"model"]; _labelDescription.text = _modelJson[@"description"]; _labelDimension.text = _modelJson[@"dimension"]; // ── 数量 / stepper ────────────────────────────────────────────────────── _editQTY.text = [_modelJson[@"count"] stringValue]; int c = [_modelJson[@"count"] intValue]; int s = [_modelJson[@"stockUom"] intValue]; [self init_Stepper:s max:9999 min:0 value:c]; // ── 价格 flag 处理(与 ScanListCell 保持一致)─────────────────────────── NSString *price1 = _modelJson[price_group[@"price_1"][@"name"]]; NSString *price2 = _modelJson[price_group[@"price_2"][@"name"]]; NSString *price3 = _modelJson[price_group[@"price_3"][@"name"]]; if (price1 && ![price1 isEqualToString:@"N/A"]) price1 = [NSString stringWithFormat:@"%.2f", [price1 doubleValue]]; if (price2 && ![price2 isEqualToString:@"N/A"]) { price2 = [NSString stringWithFormat:@"%.2f", [price2 doubleValue]]; _modelJson[@"special_price"] = @YES; } if (price3 && ![price3 isEqualToString:@"N/A"]) price3 = [NSString stringWithFormat:@"%.2f", [price3 doubleValue]]; if ((price1 && ![price1 isEqualToString:@"N/A"] && [price1 isEqualToString:price3]) || (price2 && ![price2 isEqualToString:@"N/A"] && [price2 isEqualToString:price3])) _modelJson[@"net_price"] = @NO; // ── 构建动态表格列 ─────────────────────────────────────────────────────── NSMutableArray *columns = [NSMutableArray array]; [columns addObject:@{ @"header": @"CuFT", @"value": unit_cuft }]; [columns addObject:@{ @"header": @"Origin", @"value": origin }]; [columns addObject:@{ @"header": @"Port", @"value": port }]; [columns addObject:@{ @"header": @"Available", @"value": available }]; int priceCount = [price_group[@"price_count"] intValue]; for (int i = 0; i < priceCount; i++) { NSString *key = [NSString stringWithFormat:@"price_%d", i]; NSDictionary *pg = price_group[key]; if (!pg || !pg[@"display"]) continue; NSString *rawValue = _modelJson[pg[@"name"]]; NSString *displayValue = @""; if (rawValue && ![rawValue isEqualToString:@"N/A"]) { double dval = [rawValue doubleValue]; if (dval > 0) displayValue = [RAConvertor currencyNumber:(float)dval]; } // price_2 固定为 Show Special if (i == 2 && ![_modelJson[@"special_price"] boolValue]) displayValue = @""; [columns addObject:@{ @"header": pg[@"display"], @"value": displayValue }]; } [self buildPriceTableWithColumns:columns]; } // MARK: - Stepper - (void)init_Stepper:(int)step max:(int)max min:(int)min value:(int)value { if (self.steper != nil) { if (step <= 0) step = 1; self.steper.minimumValue = min; self.steper.stepValue = step; self.steper.value = value; } } // MARK: - 动态表格构建 - (void)buildPriceTableWithColumns:(NSArray *)columns { for (UIView *v in self.priceScrollView.subviews) [v removeFromSuperview]; if (columns.count == 0) { self.priceScrollView.contentSize = CGSizeZero; return; } UIFont *font = [UIFont systemFontOfSize:15.0]; UIColor *borderColor = UIColor.labelColor; CGFloat totalWidth = 0; NSMutableArray *colWidths = [NSMutableArray arrayWithCapacity:columns.count]; for (NSDictionary *col in columns) { NSString *header = col[@"header"] ?: @""; NSString *value = col[@"value"] ?: @""; NSDictionary *attrs = @{ NSFontAttributeName: font }; CGFloat hw = [header sizeWithAttributes:attrs].width + kCellHPadding * 2; CGFloat vw = [value sizeWithAttributes:attrs].width + kCellHPadding * 2; CGFloat w = MAX(MAX(hw, vw), kMinColumnWidth); [colWidths addObject:@(w)]; } BOOL isLast = NO; CGFloat x = 0; for (NSUInteger i = 0; i < columns.count; i++) { CGFloat w = [colWidths[i] floatValue]; NSDictionary *col = columns[i]; isLast = (i == columns.count - 1); UILabel *hLabel = [self makeCellLabel:col[@"header"] font:font borderColor:borderColor frame:CGRectMake(x, 0, w, kTableRowHeight) isLast:isLast]; hLabel.backgroundColor = [UIColor secondarySystemBackgroundColor]; [self.priceScrollView addSubview:hLabel]; UILabel *vLabel = [self makeCellLabel:col[@"value"] font:font borderColor:borderColor frame:CGRectMake(x, kTableRowHeight, w, kTableRowHeight) isLast:isLast]; [self.priceScrollView addSubview:vLabel]; x += w; totalWidth = x; } self.priceScrollView.contentSize = CGSizeMake(totalWidth, kTableRowHeight * 2); } // MARK: - 辅助 - (UILabel *)makeCellLabel:(NSString *)text font:(UIFont *)font borderColor:(UIColor *)borderColor frame:(CGRect)frame isLast:(BOOL)isLast { UILabel *label = [[UILabel alloc] initWithFrame:frame]; label.text = text ?: @""; label.font = font; label.textAlignment = NSTextAlignmentCenter; label.clipsToBounds = YES; CGFloat w = frame.size.width; CGFloat h = frame.size.height; CGFloat lw = 1.0; CAShapeLayer *border = [CAShapeLayer layer]; UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(0, 0)]; [path addLineToPoint:CGPointMake(w, 0)]; [path moveToPoint:CGPointMake(0, h)]; [path addLineToPoint:CGPointMake(w, h)]; [path moveToPoint:CGPointMake(0, 0)]; [path addLineToPoint:CGPointMake(0, h)]; if (isLast) { [path moveToPoint:CGPointMake(w, 0)]; [path addLineToPoint:CGPointMake(w, h)]; } border.path = path.CGPath; border.strokeColor = borderColor.CGColor; border.lineWidth = lw; border.fillColor = [UIColor clearColor].CGColor; [label.layer addSublayer:border]; return label; } @end