// // FKProPriceDetailCell.m // FirstLink // // Created by jack on 16/8/12. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKProPriceDetailCell.h" #import "FKProDetailViewModel.h" #import "UICollectionViewLeftAlignedLayout.h" #import "FKProTagCollectionCell.h" #import "FKVipBuyGuideButton.h" #import "FLControllerHelper.h" #import "FKVipBuyController.h" static NSString * const kVipPriceTipString = @"VIP价"; @interface FKProPriceDetailCell () @property (nonatomic, strong) UILabel *priceSignLabel; @property (nonatomic, strong) UILabel *priceLabel; @property (nonatomic, strong) UILabel *referPriceLabel; @property (nonatomic, strong) UIView *referCutLine; @property (nonatomic, strong) UILabel *soldNumLabel; @property (nonatomic, strong) UIImageView *arrowImgView; @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSArray *tagArray; @property (nonatomic, strong) UILabel *normalPriceLabel; @property (nonatomic, strong) UILabel *foreshshowActivityLabel; @property (nonatomic, strong) UILabel *inprogressActivityLabel; @property (nonatomic, strong) FKVipBuyGuideButton *vipBuyButton; @end @implementation FKProPriceDetailCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self addAllSubviews]; self.selectionStyle = UITableViewCellSelectionStyleNone; self.contentView.backgroundColor = [UIColor whiteColor]; } return self; } - (void)addAllSubviews{ [self.contentView addSubview:self.priceSignLabel]; [self.contentView addSubview:self.priceLabel]; [self.contentView addSubview:self.collectionView]; [self.contentView addSubview:self.referPriceLabel]; [self.contentView addSubview:self.referCutLine]; [self.contentView addSubview:self.soldNumLabel]; [self.contentView addSubview:self.arrowImgView]; [self.contentView addSubview:self.normalPriceLabel]; [self.contentView addSubview:self.vipBuyButton]; [self.contentView addSubview:self.inprogressActivityLabel]; [self.inprogressActivityLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(13); make.bottom.equalTo(self.priceLabel.mas_baseline).offset(2); }]; [self.priceSignLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.inprogressActivityLabel.mas_right).offset(0); make.bottom.equalTo(self.priceLabel.mas_baseline).offset(2); }]; [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.priceSignLabel.mas_right); make.top.equalTo(self.contentView); }]; [self.referPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.priceLabel.mas_right).offset(7.5); make.bottom.equalTo(self.priceLabel.mas_baseline); }]; [self.referCutLine mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.referPriceLabel); make.right.equalTo(self.referPriceLabel); make.centerY.equalTo(self.referPriceLabel); make.height.mas_equalTo(0.5); }]; [self.soldNumLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-15); make.bottom.equalTo(self.contentView).offset(-14); }]; [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.contentView); make.centerX.equalTo(self.contentView.mas_left).offset(39); }]; [self.contentView addSubview:self.foreshshowActivityLabel]; [self.foreshshowActivityLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(13); make.bottom.equalTo(self.contentView).offset(-14); }]; [self.contentView addSubview:self.normalPriceLabel]; [self.normalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(13); make.bottom.equalTo(self.contentView).offset(-14); }]; [self.contentView addSubview:self.vipBuyButton]; [self.vipBuyButton mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.normalPriceLabel.mas_right).offset(12); make.centerY.equalTo(self.normalPriceLabel); make.size.mas_equalTo(CGSizeMake(62, 20)); }]; } - (void)fk_configWithViewModel:(id)viewModel indexPath:(NSIndexPath *)indexPath{ if ([viewModel isKindOfClass:[FKProDetailViewModel class]]) { FKProDetailViewModel *detailModel = (FKProDetailViewModel *)viewModel; NSString *soldStr = detailModel.dataItem.productInfo.soldCount; if ([FLStringHelper isValidString:soldStr] && soldStr.integerValue > 0) { self.soldNumLabel.hidden = NO; self.soldNumLabel.text = [NSString stringWithFormat:@"已售:%@", detailModel.dataItem.productInfo.soldCount]; } else { self.soldNumLabel.hidden = YES; } // 默认从选中规格中取非活动价格 NSString *realPrice = detailModel.dataItem.productInfo.price; NSString *originPrice = detailModel.dataItem.productInfo.originalPrice; if (detailModel.selectBuyItem) { realPrice = detailModel.selectBuyItem.price; originPrice = detailModel.selectBuyItem.originPrice; } NSString *leftPrice = nil; NSString *rightPrice = nil; NSMutableAttributedString *bottomAttrPrice = nil; NSMutableArray *proTagArray = [NSMutableArray array]; // 清除活动提示信息 self.inprogressActivityLabel.text = nil; self.foreshshowActivityLabel.text = nil; self.normalPriceLabel.hidden = NO; self.vipBuyButton.hidden = NO; // 根据活动状态更新价格显示 FKProPriceDetailCellType cellType = [detailModel priceDetailCellType]; if (cellType == FKProPriceDetailCellTypeInProgress || cellType == FKProPriceDetailCellTypeForeshow) { self.normalPriceLabel.hidden = YES; self.vipBuyButton.hidden = YES; NSString *desc = detailModel.selectBuyItem.activityDesc; NSString *price = detailModel.selectBuyItem.activityPrice ? : realPrice; if (cellType == FKProPriceDetailCellTypeForeshow) { self.foreshshowActivityLabel.text = [NSString stringWithFormat:@"%@¥%@", desc, [FLStringHelper convertFenToYuan:price]]; leftPrice = realPrice; rightPrice = originPrice; } else if (cellType == FKProPriceDetailCellTypeInProgress) { self.inprogressActivityLabel.text = desc; leftPrice = price; rightPrice = originPrice; } } else { // 显示UI NSRange bottomPriceRange; if ([detailModel.discountItem isVipUserWithDiscount]) { NSString *tempPrice = [NSString stringWithFormat:@"%@", [FLStringHelper convertFenToYuan:realPrice]]; bottomAttrPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"普通用户价:¥%@", tempPrice]]; bottomPriceRange = NSMakeRange(bottomAttrPrice.length - tempPrice.length, tempPrice.length); CGFloat discount = [detailModel.discountItem.discount floatValue]; realPrice = [NSString stringWithFormat:@"%.2lf", [realPrice floatValue] * discount/100.0]; leftPrice = realPrice; rightPrice = originPrice; [proTagArray addObject:kVipPriceTipString]; self.vipBuyButton.hidden = YES; } else { CGFloat discount = [detailModel.discountItem.discount floatValue]; NSString *tempPrice = [NSString stringWithFormat:@"%.2lf", [realPrice floatValue]/100.0*discount/100.0]; bottomAttrPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"VIP用户价:¥%@", tempPrice]]; bottomPriceRange = NSMakeRange(bottomAttrPrice.length - tempPrice.length, tempPrice.length); leftPrice = realPrice; rightPrice = originPrice; self.vipBuyButton.hidden = NO; } [bottomAttrPrice addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x666666) range:bottomPriceRange]; [bottomAttrPrice addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:bottomPriceRange]; } self.priceLabel.text = [NSString stringWithFormat:@"%@", [FLStringHelper convertFenToYuan:leftPrice]]; self.referPriceLabel.hidden = YES; self.referCutLine.hidden = YES; if ([FLStringHelper isValidString:rightPrice]){ self.referPriceLabel.hidden = NO; self.referCutLine.hidden = NO; self.referPriceLabel.text = [NSString stringWithFormat:@"¥%@", [FLStringHelper convertFenToYuan:rightPrice]]; } self.normalPriceLabel.attributedText = bottomAttrPrice; CGFloat discount = [self getPriceDiscount:realPrice originPrice:originPrice]; if (discount >= 0.005 && discount <= 0.8) { NSString *discountTip = [NSString stringWithFormat:@"%.1f折", discount * 10.0f]; [proTagArray addObject:discountTip]; } [proTagArray addObjectsFromArray:detailModel.tagArray]; self.tagArray = proTagArray; // 重新布局CollectionView WeakSelf(weakSelf); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ UIView *preView = nil; if (weakSelf.referPriceLabel.hidden) { preView = weakSelf.priceLabel; } else { preView = weakSelf.referPriceLabel; } [weakSelf.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(preView.mas_right).offset(7.5); make.right.equalTo(weakSelf.contentView); make.centerY.equalTo(weakSelf.priceLabel); make.height.mas_equalTo(20); }]; [weakSelf.collectionView reloadData]; }); } } - (CGFloat)getPriceDiscount:(NSString *)priceString originPrice:(NSString *)originPriceString { if ([FLStringHelper isValidString:priceString] && [FLStringHelper isValidString:originPriceString]) { CGFloat price = [FLStringHelper convertFenStringToYuanValue:priceString]; CGFloat orginPrice = [FLStringHelper convertFenStringToYuanValue:originPriceString]; return price / orginPrice; } return 0; } + (CGFloat)cellHeight:(FKProPriceDetailCellType)cellType { CGFloat offset = 26.0; if (cellType == FKProPriceDetailCellTypeInProgress) { offset = 0; } return (36.0f + offset); } #pragma mark - UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.tagArray.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { return CGSizeMake([FKProTagCollectionCell widthForKeyword:self.tagArray[indexPath.row]], 16); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { FKProTagCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FKProTagCollectionCell class]) forIndexPath:indexPath]; NSString *text = self.tagArray[indexPath.row]; cell.titleLabel.text = text; if ([text rangeOfString:kVipPriceTipString].length > 0) { cell.titleLabel.textColor = UIColorFromRGB(0xff5656); cell.titleLabel.layer.borderColor = UIColorFromRGB(0xff5656).CGColor; } else { cell.titleLabel.textColor = UIColorFromRGB(0xe77800); cell.titleLabel.layer.borderColor = UIColorFromRGB(0xe77800).CGColor; } return cell; } #pragma mark - Action - (IBAction)clickVipBuyButton:(id)sender { FKVipBuyController *controller = [FKVipBuyController new]; controller.hidesBottomBarWhenPushed = YES; [[FLControllerHelper currentNavigationController] pushViewController:controller animated:YES]; } #pragma mark - property - (UILabel *)priceLabel{ if (_priceLabel == nil) { _priceLabel = [[UILabel alloc]init]; _priceLabel.textColor = UIColorFromRGB(0xff6362); _priceLabel.font = [UIFont boldSystemFontOfSize:23]; } return _priceLabel; } - (UILabel *)priceSignLabel{ if (_priceSignLabel == nil) { _priceSignLabel = [[UILabel alloc]init]; _priceSignLabel.textColor = UIColorFromRGB(0xff6362); _priceSignLabel.font = [UIFont systemFontOfSize:11]; _priceSignLabel.text = @"¥"; } return _priceSignLabel; } - (UILabel *)referPriceLabel{ if (_referPriceLabel == nil) { _referPriceLabel = [[UILabel alloc]init]; _referPriceLabel.textColor = UIColorFromRGB(0x9999999); _referPriceLabel.font = [UIFont systemFontOfSize:10]; } return _referPriceLabel; } - (UIView *)referCutLine{ if (_referCutLine == nil) { _referCutLine = [[UIView alloc]init]; _referCutLine.backgroundColor = UIColorFromRGB(0x9999999); } return _referCutLine; } - (UILabel *)soldNumLabel{ if (_soldNumLabel == nil) { _soldNumLabel = [[UILabel alloc]init]; _soldNumLabel.textColor = UIColorFromRGB(0x666666); _soldNumLabel.font = [UIFont systemFontOfSize:12]; _soldNumLabel.text = @"已售"; } return _soldNumLabel; } - (UIImageView *)arrowImgView{ if (_arrowImgView == nil) { _arrowImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Wight_arrow"]]; } return _arrowImgView; } - (UILabel *)foreshshowActivityLabel { if (_foreshshowActivityLabel == nil) { _foreshshowActivityLabel = [[UILabel alloc]init]; _foreshshowActivityLabel.textColor = UIColorFromRGB(0xff2c6b); _foreshshowActivityLabel.font = [UIFont systemFontOfSize:12]; } return _foreshshowActivityLabel; } - (UILabel *)inprogressActivityLabel { if (_inprogressActivityLabel == nil) { _inprogressActivityLabel = [[UILabel alloc]init]; _inprogressActivityLabel.textColor = UIColorFromRGB(0x666666); _inprogressActivityLabel.font = [UIFont systemFontOfSize:11]; } return _inprogressActivityLabel; } - (UILabel *)normalPriceLabel { if (_normalPriceLabel == nil) { _normalPriceLabel = [UILabel new]; _normalPriceLabel.textColor = UIColorFromRGB(0x666666); _normalPriceLabel.font = [UIFont systemFontOfSize:8]; // _normalPriceLabel.backgroundColor = [UIColor yellowColor]; } return _normalPriceLabel; } - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewLeftAlignedLayout *layout = [UICollectionViewLeftAlignedLayout new]; layout.minimumInteritemSpacing = 6; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.scrollEnabled = NO; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = UIColorFromRGB(0xffffff); _collectionView.contentInset = UIEdgeInsetsZero; [_collectionView registerClass:[FKProTagCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([FKProTagCollectionCell class])]; } return _collectionView; } - (FKVipBuyGuideButton *)vipBuyButton { if (!_vipBuyButton) { _vipBuyButton = [FKVipBuyGuideButton buttonWithType:UIButtonTypeCustom]; _vipBuyButton.hidden = YES; [_vipBuyButton addTarget:self action:@selector(clickVipBuyButton:) forControlEvents:UIControlEventTouchUpInside]; } return _vipBuyButton; } @end