Keine Beschreibung

FKProPriceDetailCell.m 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // FKProPriceDetailCell.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/8/12.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKProPriceDetailCell.h"
  9. #import "FKProDetailViewModel.h"
  10. #import "UICollectionViewLeftAlignedLayout.h"
  11. #import "FKProTagCollectionCell.h"
  12. #import "FKVipBuyGuideButton.h"
  13. #import "FLControllerHelper.h"
  14. #import "FKVipBuyController.h"
  15. static NSString * const kVipPriceTipString = @"VIP价";
  16. @interface FKProPriceDetailCell ()
  17. <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
  18. @property (nonatomic, strong) UILabel *priceSignLabel;
  19. @property (nonatomic, strong) UILabel *priceLabel;
  20. @property (nonatomic, strong) UILabel *referPriceLabel;
  21. @property (nonatomic, strong) UIView *referCutLine;
  22. @property (nonatomic, strong) UILabel *soldNumLabel;
  23. @property (nonatomic, strong) UIImageView *arrowImgView;
  24. @property (nonatomic, strong) UICollectionView *collectionView;
  25. @property (nonatomic, strong) NSArray *tagArray;
  26. @property (nonatomic, strong) UILabel *normalPriceLabel;
  27. @property (nonatomic, strong) UILabel *foreshshowActivityLabel;
  28. @property (nonatomic, strong) UILabel *inprogressActivityLabel;
  29. @property (nonatomic, strong) FKVipBuyGuideButton *vipBuyButton;
  30. @end
  31. @implementation FKProPriceDetailCell
  32. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  33. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  34. [self addAllSubviews];
  35. self.selectionStyle = UITableViewCellSelectionStyleNone;
  36. self.contentView.backgroundColor = [UIColor whiteColor];
  37. }
  38. return self;
  39. }
  40. - (void)addAllSubviews{
  41. [self.contentView addSubview:self.priceSignLabel];
  42. [self.contentView addSubview:self.priceLabel];
  43. [self.contentView addSubview:self.collectionView];
  44. [self.contentView addSubview:self.referPriceLabel];
  45. [self.contentView addSubview:self.referCutLine];
  46. [self.contentView addSubview:self.soldNumLabel];
  47. [self.contentView addSubview:self.arrowImgView];
  48. [self.contentView addSubview:self.normalPriceLabel];
  49. [self.contentView addSubview:self.vipBuyButton];
  50. [self.contentView addSubview:self.inprogressActivityLabel];
  51. [self.inprogressActivityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.left.equalTo(self.contentView).offset(13);
  53. make.bottom.equalTo(self.priceLabel.mas_baseline).offset(2);
  54. }];
  55. [self.priceSignLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.left.equalTo(self.inprogressActivityLabel.mas_right).offset(0);
  57. make.bottom.equalTo(self.priceLabel.mas_baseline).offset(2);
  58. }];
  59. [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.left.equalTo(self.priceSignLabel.mas_right);
  61. make.top.equalTo(self.contentView);
  62. }];
  63. [self.referPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.left.equalTo(self.priceLabel.mas_right).offset(7.5);
  65. make.bottom.equalTo(self.priceLabel.mas_baseline);
  66. }];
  67. [self.referCutLine mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.left.equalTo(self.referPriceLabel);
  69. make.right.equalTo(self.referPriceLabel);
  70. make.centerY.equalTo(self.referPriceLabel);
  71. make.height.mas_equalTo(0.5);
  72. }];
  73. [self.soldNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.right.equalTo(self.contentView).offset(-15);
  75. make.bottom.equalTo(self.contentView).offset(-14);
  76. }];
  77. [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  78. make.bottom.equalTo(self.contentView);
  79. make.centerX.equalTo(self.contentView.mas_left).offset(39);
  80. }];
  81. [self.contentView addSubview:self.foreshshowActivityLabel];
  82. [self.foreshshowActivityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.left.equalTo(self.contentView).offset(13);
  84. make.bottom.equalTo(self.contentView).offset(-14);
  85. }];
  86. [self.contentView addSubview:self.normalPriceLabel];
  87. [self.normalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.left.equalTo(self.contentView).offset(13);
  89. make.bottom.equalTo(self.contentView).offset(-14);
  90. }];
  91. [self.contentView addSubview:self.vipBuyButton];
  92. [self.vipBuyButton mas_makeConstraints:^(MASConstraintMaker *make) {
  93. make.left.equalTo(self.normalPriceLabel.mas_right).offset(12);
  94. make.centerY.equalTo(self.normalPriceLabel);
  95. make.size.mas_equalTo(CGSizeMake(62, 20));
  96. }];
  97. }
  98. - (void)fk_configWithViewModel:(id)viewModel indexPath:(NSIndexPath *)indexPath{
  99. if ([viewModel isKindOfClass:[FKProDetailViewModel class]]) {
  100. FKProDetailViewModel *detailModel = (FKProDetailViewModel *)viewModel;
  101. NSString *soldStr = detailModel.dataItem.productInfo.soldCount;
  102. if ([FLStringHelper isValidString:soldStr] && soldStr.integerValue > 0) {
  103. self.soldNumLabel.hidden = NO;
  104. self.soldNumLabel.text = [NSString stringWithFormat:@"已售:%@", detailModel.dataItem.productInfo.soldCount];
  105. } else {
  106. self.soldNumLabel.hidden = YES;
  107. }
  108. // 默认从选中规格中取非活动价格
  109. NSString *realPrice = detailModel.dataItem.productInfo.price;
  110. NSString *originPrice = detailModel.dataItem.productInfo.originalPrice;
  111. if (detailModel.selectBuyItem) {
  112. realPrice = detailModel.selectBuyItem.price;
  113. originPrice = detailModel.selectBuyItem.originPrice;
  114. }
  115. NSString *leftPrice = nil;
  116. NSString *rightPrice = nil;
  117. NSMutableAttributedString *bottomAttrPrice = nil;
  118. NSMutableArray *proTagArray = [NSMutableArray array];
  119. // 清除活动提示信息
  120. self.inprogressActivityLabel.text = nil;
  121. self.foreshshowActivityLabel.text = nil;
  122. self.normalPriceLabel.hidden = NO;
  123. self.vipBuyButton.hidden = NO;
  124. // 根据活动状态更新价格显示
  125. FKProPriceDetailCellType cellType = [detailModel priceDetailCellType];
  126. if (cellType == FKProPriceDetailCellTypeInProgress
  127. || cellType == FKProPriceDetailCellTypeForeshow) {
  128. self.normalPriceLabel.hidden = YES;
  129. self.vipBuyButton.hidden = YES;
  130. NSString *desc = detailModel.selectBuyItem.activityDesc;
  131. NSString *price = detailModel.selectBuyItem.activityPrice ? : realPrice;
  132. if (cellType == FKProPriceDetailCellTypeForeshow) {
  133. self.foreshshowActivityLabel.text = [NSString stringWithFormat:@"%@¥%@", desc, [FLStringHelper convertFenToYuan:price]];
  134. leftPrice = realPrice;
  135. rightPrice = originPrice;
  136. } else if (cellType == FKProPriceDetailCellTypeInProgress) {
  137. self.inprogressActivityLabel.text = desc;
  138. leftPrice = price;
  139. rightPrice = originPrice;
  140. }
  141. } else {
  142. // 显示UI
  143. NSRange bottomPriceRange;
  144. if ([detailModel.discountItem isVipUserWithDiscount]) {
  145. NSString *tempPrice = [NSString stringWithFormat:@"%@", [FLStringHelper convertFenToYuan:realPrice]];
  146. bottomAttrPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"普通用户价:¥%@", tempPrice]];
  147. bottomPriceRange = NSMakeRange(bottomAttrPrice.length - tempPrice.length, tempPrice.length);
  148. CGFloat discount = [detailModel.discountItem.discount floatValue];
  149. realPrice = [NSString stringWithFormat:@"%.2lf", [realPrice floatValue] * discount/100.0];
  150. leftPrice = realPrice;
  151. rightPrice = originPrice;
  152. [proTagArray addObject:kVipPriceTipString];
  153. self.vipBuyButton.hidden = YES;
  154. } else {
  155. CGFloat discount = [detailModel.discountItem.discount floatValue];
  156. NSString *tempPrice = [NSString stringWithFormat:@"%.2lf", [realPrice floatValue]/100.0*discount/100.0];
  157. bottomAttrPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"VIP用户价:¥%@", tempPrice]];
  158. bottomPriceRange = NSMakeRange(bottomAttrPrice.length - tempPrice.length, tempPrice.length);
  159. leftPrice = realPrice;
  160. rightPrice = originPrice;
  161. self.vipBuyButton.hidden = NO;
  162. }
  163. [bottomAttrPrice addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x666666) range:bottomPriceRange];
  164. [bottomAttrPrice addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:bottomPriceRange];
  165. }
  166. self.priceLabel.text = [NSString stringWithFormat:@"%@", [FLStringHelper convertFenToYuan:leftPrice]];
  167. self.referPriceLabel.hidden = YES;
  168. self.referCutLine.hidden = YES;
  169. if ([FLStringHelper isValidString:rightPrice]){
  170. self.referPriceLabel.hidden = NO;
  171. self.referCutLine.hidden = NO;
  172. self.referPriceLabel.text = [NSString stringWithFormat:@"¥%@", [FLStringHelper convertFenToYuan:rightPrice]];
  173. }
  174. self.normalPriceLabel.attributedText = bottomAttrPrice;
  175. CGFloat discount = [self getPriceDiscount:realPrice originPrice:originPrice];
  176. if (discount >= 0.005 && discount <= 0.8) {
  177. NSString *discountTip = [NSString stringWithFormat:@"%.1f折", discount * 10.0f];
  178. [proTagArray addObject:discountTip];
  179. }
  180. [proTagArray addObjectsFromArray:detailModel.tagArray];
  181. self.tagArray = proTagArray;
  182. // 重新布局CollectionView
  183. WeakSelf(weakSelf);
  184. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  185. UIView *preView = nil;
  186. if (weakSelf.referPriceLabel.hidden) {
  187. preView = weakSelf.priceLabel;
  188. } else {
  189. preView = weakSelf.referPriceLabel;
  190. }
  191. [weakSelf.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
  192. make.left.equalTo(preView.mas_right).offset(7.5);
  193. make.right.equalTo(weakSelf.contentView);
  194. make.centerY.equalTo(weakSelf.priceLabel);
  195. make.height.mas_equalTo(20);
  196. }];
  197. [weakSelf.collectionView reloadData];
  198. });
  199. }
  200. }
  201. - (CGFloat)getPriceDiscount:(NSString *)priceString originPrice:(NSString *)originPriceString {
  202. if ([FLStringHelper isValidString:priceString] && [FLStringHelper isValidString:originPriceString]) {
  203. CGFloat price = [FLStringHelper convertFenStringToYuanValue:priceString];
  204. CGFloat orginPrice = [FLStringHelper convertFenStringToYuanValue:originPriceString];
  205. return price / orginPrice;
  206. }
  207. return 0;
  208. }
  209. + (CGFloat)cellHeight:(FKProPriceDetailCellType)cellType {
  210. CGFloat offset = 26.0;
  211. if (cellType == FKProPriceDetailCellTypeInProgress) {
  212. offset = 0;
  213. }
  214. return (36.0f + offset);
  215. }
  216. #pragma mark - UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
  217. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  218. return self.tagArray.count;
  219. }
  220. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  221. return CGSizeMake([FKProTagCollectionCell widthForKeyword:self.tagArray[indexPath.row]], 16);
  222. }
  223. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  224. FKProTagCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FKProTagCollectionCell class])
  225. forIndexPath:indexPath];
  226. NSString *text = self.tagArray[indexPath.row];
  227. cell.titleLabel.text = text;
  228. if ([text rangeOfString:kVipPriceTipString].length > 0) {
  229. cell.titleLabel.textColor = UIColorFromRGB(0xff5656);
  230. cell.titleLabel.layer.borderColor = UIColorFromRGB(0xff5656).CGColor;
  231. } else {
  232. cell.titleLabel.textColor = UIColorFromRGB(0xe77800);
  233. cell.titleLabel.layer.borderColor = UIColorFromRGB(0xe77800).CGColor;
  234. }
  235. return cell;
  236. }
  237. #pragma mark - Action
  238. - (IBAction)clickVipBuyButton:(id)sender {
  239. FKVipBuyController *controller = [FKVipBuyController new];
  240. controller.hidesBottomBarWhenPushed = YES;
  241. [[FLControllerHelper currentNavigationController] pushViewController:controller animated:YES];
  242. }
  243. #pragma mark - property
  244. - (UILabel *)priceLabel{
  245. if (_priceLabel == nil) {
  246. _priceLabel = [[UILabel alloc]init];
  247. _priceLabel.textColor = UIColorFromRGB(0xff6362);
  248. _priceLabel.font = [UIFont boldSystemFontOfSize:23];
  249. }
  250. return _priceLabel;
  251. }
  252. - (UILabel *)priceSignLabel{
  253. if (_priceSignLabel == nil) {
  254. _priceSignLabel = [[UILabel alloc]init];
  255. _priceSignLabel.textColor = UIColorFromRGB(0xff6362);
  256. _priceSignLabel.font = [UIFont systemFontOfSize:11];
  257. _priceSignLabel.text = @"¥";
  258. }
  259. return _priceSignLabel;
  260. }
  261. - (UILabel *)referPriceLabel{
  262. if (_referPriceLabel == nil) {
  263. _referPriceLabel = [[UILabel alloc]init];
  264. _referPriceLabel.textColor = UIColorFromRGB(0x9999999);
  265. _referPriceLabel.font = [UIFont systemFontOfSize:10];
  266. }
  267. return _referPriceLabel;
  268. }
  269. - (UIView *)referCutLine{
  270. if (_referCutLine == nil) {
  271. _referCutLine = [[UIView alloc]init];
  272. _referCutLine.backgroundColor = UIColorFromRGB(0x9999999);
  273. }
  274. return _referCutLine;
  275. }
  276. - (UILabel *)soldNumLabel{
  277. if (_soldNumLabel == nil) {
  278. _soldNumLabel = [[UILabel alloc]init];
  279. _soldNumLabel.textColor = UIColorFromRGB(0x666666);
  280. _soldNumLabel.font = [UIFont systemFontOfSize:12];
  281. _soldNumLabel.text = @"已售";
  282. }
  283. return _soldNumLabel;
  284. }
  285. - (UIImageView *)arrowImgView{
  286. if (_arrowImgView == nil) {
  287. _arrowImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Wight_arrow"]];
  288. }
  289. return _arrowImgView;
  290. }
  291. - (UILabel *)foreshshowActivityLabel {
  292. if (_foreshshowActivityLabel == nil) {
  293. _foreshshowActivityLabel = [[UILabel alloc]init];
  294. _foreshshowActivityLabel.textColor = UIColorFromRGB(0xff2c6b);
  295. _foreshshowActivityLabel.font = [UIFont systemFontOfSize:12];
  296. }
  297. return _foreshshowActivityLabel;
  298. }
  299. - (UILabel *)inprogressActivityLabel {
  300. if (_inprogressActivityLabel == nil) {
  301. _inprogressActivityLabel = [[UILabel alloc]init];
  302. _inprogressActivityLabel.textColor = UIColorFromRGB(0x666666);
  303. _inprogressActivityLabel.font = [UIFont systemFontOfSize:11];
  304. }
  305. return _inprogressActivityLabel;
  306. }
  307. - (UILabel *)normalPriceLabel {
  308. if (_normalPriceLabel == nil) {
  309. _normalPriceLabel = [UILabel new];
  310. _normalPriceLabel.textColor = UIColorFromRGB(0x666666);
  311. _normalPriceLabel.font = [UIFont systemFontOfSize:8];
  312. // _normalPriceLabel.backgroundColor = [UIColor yellowColor];
  313. }
  314. return _normalPriceLabel;
  315. }
  316. - (UICollectionView *)collectionView {
  317. if (!_collectionView) {
  318. UICollectionViewLeftAlignedLayout *layout = [UICollectionViewLeftAlignedLayout new];
  319. layout.minimumInteritemSpacing = 6;
  320. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  321. _collectionView.showsVerticalScrollIndicator = NO;
  322. _collectionView.scrollEnabled = NO;
  323. _collectionView.dataSource = self;
  324. _collectionView.delegate = self;
  325. _collectionView.backgroundColor = UIColorFromRGB(0xffffff);
  326. _collectionView.contentInset = UIEdgeInsetsZero;
  327. [_collectionView registerClass:[FKProTagCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([FKProTagCollectionCell class])];
  328. }
  329. return _collectionView;
  330. }
  331. - (FKVipBuyGuideButton *)vipBuyButton {
  332. if (!_vipBuyButton) {
  333. _vipBuyButton = [FKVipBuyGuideButton buttonWithType:UIButtonTypeCustom];
  334. _vipBuyButton.hidden = YES;
  335. [_vipBuyButton addTarget:self action:@selector(clickVipBuyButton:) forControlEvents:UIControlEventTouchUpInside];
  336. }
  337. return _vipBuyButton;
  338. }
  339. @end