Files
peko-ios/YuMi/Modules/YMMine/View/Cell/MineInfo/XPMineMultipleContentTableViewCell.m
2024-09-23 20:35:29 +08:00

425 lines
15 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// XPMineMultipleContentTableViewCell.m
// YuMi
//
// Created by P on 2024/9/20.
//
#import "XPMineMultipleContentTableViewCell.h"
#import <QGVAPWrapView.h>
#import "MedalModel.h"
#import "UserGiftWallInfoModel.h"
#import "XPRoomGiftAnimationParser.h"
@interface RoundCornerBorderBackground : UIView
@end
@implementation RoundCornerBorderBackground
- (instancetype)init {
if (self = [super init]) {
[self setupBGColor];
self.layer.borderColor = UIColorFromRGB(0x1A4655).CGColor;
self.layer.borderWidth = 1;
self.layer.cornerRadius = 8;
self.layer.masksToBounds = YES;
}
return self;
}
- (void)setupBGColor {
// 创建 CAGradientLayer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
// 设置渐变颜色数组(从上到下)
gradientLayer.colors = @[
(id)UIColorFromRGB(0x041921).CGColor,
(id)UIColorFromRGB(0x072834).CGColor
];
// 设置渐变的起止位置0.0 是顶部1.0 是底部)
gradientLayer.startPoint = CGPointMake(0.5, 0.0); // 顶部中央
gradientLayer.endPoint = CGPointMake(0.5, 1.0); // 底部中央
// 设置渐变图层的大小为视图的大小
gradientLayer.frame = self.bounds;
// 将渐变图层添加到 view 的图层中
[self.layer insertSublayer:gradientLayer atIndex:0];
}
@end
@interface XPMultipleContentCollectionCell : UICollectionViewCell
@property (nonatomic, copy) NSString *videoUrl;
@property (nonatomic, strong) VAPView *vapView;
@property (nonatomic, strong) XPRoomGiftAnimationParser *vapParser;
@property (nonatomic, strong) NetImageView *icon;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UserMedalModel *userMedalModel;
@property (nonatomic, strong) UserGiftWallInfoModel *giftModel;
@end
@implementation XPMultipleContentCollectionCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
RoundCornerBorderBackground *view = [[RoundCornerBorderBackground alloc] init];
[self.contentView addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.contentView);
}];
[self.contentView addSubview:self.icon];
[self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.contentView);
make.top.mas_equalTo(self.contentView).offset(8);
make.width.height.mas_equalTo(kGetScaleWidth(63));
}];
[self.contentView addSubview:self.nameLabel];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.contentView);
make.top.mas_equalTo(self.icon.mas_bottom).offset(2);
make.height.mas_equalTo(18);
}];
[self.contentView addSubview:self.countLabel];
[self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.contentView);
make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(2);
make.height.mas_equalTo(20);
}];
[self.contentView addSubview:self.vapView];
[self.vapView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.icon);
}];
}
return self;
}
- (void)setGiftModel:(UserGiftWallInfoModel *)giftModel {
_giftModel = giftModel;
self.vapView.hidden = YES;
[self.vapView stopHWDMP4];
self.icon.hidden = NO;
self.countLabel.hidden = NO;
self.icon.imageUrl = giftModel.picUrl;
self.nameLabel.text = giftModel.giftName;
self.countLabel.text = [NSString stringWithFormat:@"x%ld", (long)giftModel.reciveCount];
}
- (void)setUserMedalModel:(UserMedalModel *)userMedalModel {
self.icon.hidden = YES;
self.vapView.hidden = NO;
self.countLabel.hidden = YES;
self.nameLabel.text = userMedalModel.medalName;
if (self.videoUrl.length > 0) {
[self.vapView playHWDMP4:self.videoUrl repeatCount:-1 delegate:nil];
} else {
NSString *resourcePath = [userMedalModel.picUrl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (resourcePath.length > 0) {
NSString *encodingUrl = [resourcePath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
@kWeakify(self);
[self.vapParser parseWithURL:encodingUrl completionBlock:^(NSString * _Nullable videoUrl) {
@kStrongify(self);
if (videoUrl.length) {
[self.vapView playHWDMP4:videoUrl repeatCount:-1 delegate:nil];
}
} failureBlock:^(NSError * _Nullable error) {
}];
}
}
}
- (NetImageView *)icon {
if (!_icon) {
_icon = [[NetImageView alloc] init];
}
return _icon;
}
- (UILabel *)nameLabel {
if (!_nameLabel) {
_nameLabel = [UILabel labelInitWithText:@"" font:kFontMedium(12) textColor:[UIColor whiteColor]];
}
return _nameLabel;
}
- (UILabel *)countLabel {
if (!_countLabel) {
_countLabel = [UILabel labelInitWithText:@"" font:kFontMedium(14) textColor:[UIColor whiteColor]];
}
return _countLabel;
}
- (VAPView *)vapView {
if (!_vapView) {
_vapView = [[VAPView alloc] init];
[_vapView setMute:YES];
_vapView.contentMode = UIViewContentModeScaleAspectFill;
}
return _vapView;
}
- (XPRoomGiftAnimationParser *)vapParser {
if (!_vapParser) {
_vapParser = [[XPRoomGiftAnimationParser alloc] init];
}
return _vapParser;
}
@end
@interface XPMineMultipleContentTableViewCell () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property (nonatomic, assign) BOOL isMedal;
@property (nonatomic, strong) UIButton *giftButton;
@property (nonatomic, strong) UIButton *medalButton;
@property (nonatomic, strong) UIImageView *indicatorImageView;
@property (nonatomic, strong) UICollectionView *contentCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *contentLayout;
@property (nonatomic, strong) NSMutableArray *allGifts;
@property (nonatomic, strong) UILabel *emptyLabel;
@end
@implementation XPMineMultipleContentTableViewCell
+ (CGFloat)cellHeightFro:(MedalModel *)medalModel
{
if (!medalModel || medalModel.userMedals.count == 0) {
return kGetScaleWidth(116);
}
NSInteger lines = ceil(medalModel.userMedals.count/4.0);
return 42 + lines * (kGetScaleWidth(94) + 16);
}
+ (CGFloat)cellHeightFro:(NSArray<UserGiftWallInfoModel *> *)giftWall with:(NSArray<UserGiftWallInfoModel *> *)luckyGiftWall
{
if (giftWall.count == 0 && luckyGiftWall.count == 0) {
return kGetScaleWidth(116);
}
NSInteger lines = ceil((giftWall.count + luckyGiftWall.count)/4.0);
return lines * (kGetScaleWidth(107) + 16);
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.isMedal = YES;
[self setupUI];
}
return self;
}
- (void)setupUI {
RoundCornerBorderBackground *view = [[RoundCornerBorderBackground alloc] init];
[self.contentView addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo(self.contentView);
make.bottom.mas_equalTo(self.contentView);//.offset(-8);
}];
[self.contentView addSubview:self.medalButton];
[self.medalButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(8);
make.leading.mas_equalTo(8);
make.size.mas_equalTo(CGSizeMake(60, 22));
}];
[self.contentView addSubview:self.giftButton];
[self.giftButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.medalButton);
make.leading.mas_equalTo(8 + 60 + 8);
make.size.mas_equalTo(CGSizeMake(50, 22));
}];
[self.contentView addSubview:self.indicatorImageView];
[self.indicatorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.medalButton);
make.top.mas_equalTo(self.medalButton.mas_bottom);
make.size.mas_equalTo(CGSizeMake(21, 6));
}];
[self.contentView addSubview:self.contentCollectionView];
[self.contentCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.indicatorImageView.mas_bottom).offset(8);
make.bottom.mas_equalTo(view).offset(-8);
make.leading.mas_equalTo(view).offset(8);
make.trailing.mas_equalTo(view).offset(-8);
}];
[self.contentView addSubview:self.emptyLabel];
[self.emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.indicatorImageView.mas_bottom).offset(8);
make.bottom.mas_equalTo(view).offset(-8);
make.leading.mas_equalTo(view).offset(8);
make.trailing.mas_equalTo(view).offset(-8);
}];
}
- (void)didTapButton:(UIButton *)sender {
self.isMedal = sender.tag == 1;
self.medalButton.selected = self.isMedal ? YES : NO;
self.giftButton.selected = self.isMedal ? NO : YES;
self.medalButton.titleLabel.font = self.isMedal ? kFontSemibold(16) : kFontRegular(14);
self.giftButton.titleLabel.font = self.isMedal ? kFontRegular(14) : kFontSemibold(16);
[self.indicatorImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.isMedal ? self.medalButton : self.giftButton);
make.top.mas_equalTo(self.medalButton.mas_bottom);
make.size.mas_equalTo(CGSizeMake(21, 6));
}];
if (self.didChangeCatalog) {
self.didChangeCatalog(self.isMedal);
}
}
- (void)setGiftWall:(NSArray<UserGiftWallInfoModel *> *)giftWall {
self.allGifts = @[].mutableCopy;
[self.allGifts addObjectsFromArray:giftWall];
}
- (void)setLuckyGiftWall:(NSArray<UserGiftWallInfoModel *> *)luckyGiftWall {
[self.allGifts addObjectsFromArray:luckyGiftWall];
}
- (void)setMedalModel:(MedalModel *)medalModel {
_medalModel = medalModel;
}
- (void)updateCell {
[self.contentCollectionView reloadData];
if (self.isMedal) {
self.emptyLabel.hidden = self.medalModel.medalCount != 0;
} else {
self.emptyLabel.hidden = self.allGifts.count != 0;
}
}
#pragma mark -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.isMedal) {
return self.medalModel.userMedals.count;
} else {
return self.allGifts.count;
}
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPMultipleContentCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPMultipleContentCollectionCell class]) forIndexPath:indexPath];
if (self.isMedal) {
cell.userMedalModel = [self.medalModel.userMedals xpSafeObjectAtIndex:indexPath.row];
} else {
cell.giftModel = [self.allGifts xpSafeObjectAtIndex:indexPath.row];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return self.isMedal ? CGSizeMake(kGetScaleWidth(77), kGetScaleWidth(94)) : CGSizeMake(kGetScaleWidth(77), kGetScaleWidth(107));
}
#pragma mark -
- (UIButton *)medalButton {
if (!_medalButton) {
_medalButton = [UIButton buttonWithType:UIButtonTypeCustom];
_medalButton.tag = 1;
_medalButton.selected = YES;
[_medalButton setTitle:YMLocalizedString(@"XPMineDataGiftTableViewCell2") forState:UIControlStateNormal];
[_medalButton.titleLabel setFont:kFontSemibold(16)];
[_medalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[_medalButton setTitleColor:[UIColor colorWithWhite:1 alpha:0.6] forState:UIControlStateNormal];
[_medalButton addTarget:self
action:@selector(didTapButton:)
forControlEvents:UIControlEventTouchUpInside];
}
return _medalButton;
}
- (UIButton *)giftButton {
if (!_giftButton) {
_giftButton = [UIButton buttonWithType:UIButtonTypeCustom];
_giftButton.tag = 2;
[_giftButton setTitle:YMLocalizedString(@"XPMineUserInfoGiftWallViewController0") forState:UIControlStateNormal];
[_giftButton.titleLabel setFont:kFontRegular(14)];
[_giftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[_giftButton setTitleColor:[UIColor colorWithWhite:1 alpha:0.6] forState:UIControlStateNormal];
[_giftButton addTarget:self
action:@selector(didTapButton:)
forControlEvents:UIControlEventTouchUpInside];
}
return _giftButton;
}
- (UIImageView *)indicatorImageView {
if (!_indicatorImageView) {
_indicatorImageView = [[UIImageView alloc] initWithImage:kImage(@"user_page_Indicator")];
}
return _indicatorImageView;
}
- (UICollectionViewFlowLayout *)contentLayout {
if (!_contentLayout) {
_contentLayout = [[UICollectionViewFlowLayout alloc] init];
_contentLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
_contentLayout.minimumInteritemSpacing = 7;
_contentLayout.minimumLineSpacing = 7;
}
return _contentLayout;
}
- (UICollectionView *)contentCollectionView {
if (!_contentCollectionView) {
_contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero
collectionViewLayout:self.contentLayout];
_contentCollectionView.delegate = self;
_contentCollectionView.dataSource = self;
_contentCollectionView.backgroundColor = [UIColor clearColor];
_contentCollectionView.scrollEnabled = NO;
[_contentCollectionView registerClass:[XPMultipleContentCollectionCell class]
forCellWithReuseIdentifier:NSStringFromClass([XPMultipleContentCollectionCell class])];
}
return _contentCollectionView;
}
- (UILabel *)emptyLabel {
if (!_emptyLabel) {
_emptyLabel = [UILabel labelInitWithText:@"No data" font:kFontRegular(14) textColor:[UIColor colorWithWhite:1 alpha:0.6]];
_emptyLabel.textAlignment = NSTextAlignmentCenter;
_emptyLabel.hidden = YES;
}
return _emptyLabel;
}
@end