Files
peko-ios/YuMi/Modules/YMMine/View/Cell/MineInfo/XPMineMultipleContentTableViewCell.m

554 lines
18 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
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
- (void)setupBGColor;
- (void)setupCellBGColor;
@end
@implementation RoundCornerBorderBackground
- (instancetype)init {
if (self = [super init]) {
self.layer.borderWidth = 1;
self.layer.cornerRadius = 10;
self.layer.masksToBounds = YES;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
// 在 layoutSubviews 中设置渐变图层的 frame
self.gradientLayer.frame = self.bounds;
}
- (void)setupBGColor {
// UIView *bg = [[UIView alloc] init];
self.layer.borderColor = UIColorFromRGB(0x1C4B5D).CGColor;
// 创建 CAGradientLayer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
// 设置渐变颜色数组(从上到下)
gradientLayer.colors = @[
(id)UIColorFromRGB(0x071A21).CGColor,
(id)UIColorFromRGB(0x0B222B).CGColor
];
// 设置渐变的起止位置0.0 是顶部1.0 是底部)
gradientLayer.startPoint = CGPointMake(0.5, 0.0); // 顶部中央
gradientLayer.endPoint = CGPointMake(0.5, 1.0); // 底部中央
// 将渐变图层添加到 view 的图层中
[self.layer insertSublayer:gradientLayer atIndex:0];
self.gradientLayer = gradientLayer;
}
- (void)setupCellBGColor {
self.layer.borderColor = UIColorFromRGB(0x1A4655).CGColor;
// 创建 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); // 底部中央
// 将渐变图层添加到 view 的图层中
[self.layer insertSublayer:gradientLayer atIndex:0];
self.gradientLayer = gradientLayer;
}
@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];
[view setupCellBGColor];
[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);
make.width.height.mas_equalTo(kGetScaleWidth(63));
}];
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.countLabel];
[self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.contentView);
make.bottom.mas_equalTo(-2);
make.height.mas_equalTo(20);
}];
[self.contentView addSubview:self.vapView];
[self.vapView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.contentView);
make.top.mas_equalTo(self.contentView).offset(7);
make.width.height.mas_equalTo(kGetScaleWidth(63));
}];
}
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];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.mas_equalTo(self.contentView).inset(2);
make.bottom.mas_equalTo(-24);
make.height.mas_equalTo(18);
}];
}
- (void)setUserMedalModel:(UserMedalModel *)userMedalModel {
_userMedalModel = userMedalModel;
self.icon.hidden = YES;
self.vapView.hidden = NO;
self.countLabel.hidden = YES;
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.mas_equalTo(self.contentView).inset(2);
make.bottom.mas_equalTo(-8);
make.height.mas_equalTo(18);
}];
self.nameLabel.text = userMedalModel.medalName;
// 停止之前的播放
[self.vapView stopHWDMP4];
// 按照新的优先级逻辑处理显示
NSString *mp4Url = userMedalModel.mp4Url;
NSString *picUrl = userMedalModel.picUrl;
// 1. 优先使用 mp4Url 展示 vapView 内容(判断是否有效 mp4
if (![NSString isEmpty:mp4Url] && [self isValidMP4URL:mp4Url]) {
[self playMP4WithUrl:mp4Url];
return;
}
// 2. mp4Url 无效,检查 picUrl 是否为有效图片
if (![NSString isEmpty:picUrl] && [NSString isValidImageURL:picUrl]) {
// 显示图片内容
[self showImageWithUrl:picUrl];
return;
}
// 3. picUrl 不是有效图片,尝试使用 picUrl 展示 vapView 内容
if (![NSString isEmpty:picUrl]) {
[self playMP4WithUrl:picUrl];
return;
}
// 所有条件都不满足,显示默认状态
[self showDefaultState];
}
#pragma mark - 私有方法
/// 验证是否为有效的 MP4 URL
- (BOOL)isValidMP4URL:(NSString *)url {
if ([NSString isEmpty:url]) {
return NO;
}
NSString *lowercaseUrl = [url lowercaseString];
return [lowercaseUrl hasSuffix:@".mp4"] ||
[lowercaseUrl containsString:@"mp4"] ||
[lowercaseUrl containsString:@"video"];
}
/// 验证是否为有效的图片 URL
- (BOOL)isValidImageURL:(NSString *)url {
if ([NSString isEmpty:url]) {
return NO;
}
NSString *lowercaseUrl = [url lowercaseString];
return [lowercaseUrl hasSuffix:@".jpg"] ||
[lowercaseUrl hasSuffix:@".jpeg"] ||
[lowercaseUrl hasSuffix:@".png"] ||
[lowercaseUrl hasSuffix:@".gif"] ||
[lowercaseUrl hasSuffix:@".webp"] ||
[lowercaseUrl containsString:@"image"];
}
/// 播放 MP4 内容
- (void)playMP4WithUrl:(NSString *)url {
self.vapView.hidden = NO;
self.icon.hidden = YES;
NSString *resourcePath = [url pureURLString];
if (resourcePath.length > 0) {
@kWeakify(self);
[self.vapParser parseWithURL:resourcePath completionBlock:^(NSString * _Nullable videoUrl) {
@kStrongify(self);
if (videoUrl.length) {
[self.vapView playHWDMP4:videoUrl repeatCount:-1 delegate:nil];
}
} failureBlock:^(NSError * _Nullable error) {
@kStrongify(self);
// MP4 播放失败,显示默认状态
[self showDefaultState];
}];
}
}
/// 显示图片内容
- (void)showImageWithUrl:(NSString *)url {
self.vapView.hidden = YES;
self.icon.hidden = NO;
self.icon.imageUrl = url;
}
/// 显示默认状态
- (void)showDefaultState {
self.vapView.hidden = YES;
self.icon.hidden = NO;
self.icon.imageUrl = @""; // 显示默认占位图
}
- (NetImageView *)icon {
if (!_icon) {
_icon = [[NetImageView alloc] init];
}
return _icon;
}
- (UILabel *)nameLabel {
if (!_nameLabel) {
_nameLabel = [UILabel labelInitWithText:@"" font:kFontMedium(12) textColor:[UIColor whiteColor]];
_nameLabel.textAlignment = NSTextAlignmentCenter;
_nameLabel.numberOfLines = 2;
_nameLabel.minimumScaleFactor = 0.7f;
_nameLabel.adjustsFontSizeToFitWidth = YES;
}
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 = UIViewContentModeScaleAspectFit;
}
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 50 + lines * (kGetScaleWidth(94) + 8);
}
+ (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 58 + lines * (kGetScaleWidth(107) + 7);
}
- (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.edges.mas_equalTo(self.contentView);
}];
[view setupBGColor];
[self.contentView addSubview:self.medalButton];
[self.medalButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView).offset(8);
make.leading.mas_equalTo(8);
make.height.mas_equalTo(30);
make.width.mas_greaterThanOrEqualTo(60);
}];
[self.contentView addSubview:self.giftButton];
[self.giftButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView).offset(8);
// make.leading.mas_equalTo(8 + 60 + 8);
make.leading.mas_equalTo(self.medalButton.mas_trailing).offset(8);
make.height.mas_equalTo(30);
make.width.mas_greaterThanOrEqualTo(50);
}];
[self.contentView addSubview:self.indicatorImageView];
[self.indicatorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(8 + 30 - 11);
make.top.mas_equalTo(self.contentView).offset(34);
make.size.mas_equalTo(CGSizeMake(21, 6));
}];
[self.contentView addSubview:self.contentCollectionView];
[self.contentCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView).offset(50);
make.bottom.mas_equalTo(self.contentView).offset(-8);
make.leading.mas_equalTo(self.contentView).offset(8);
make.trailing.mas_equalTo(self.contentView).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.leading.mas_equalTo(self.isMedal ? (8 + 30 - 11) : (8+90-11));
make.top.mas_equalTo(self.contentView).offset(34);
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 {
if (self.isMedal) {
XPMultipleContentCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Medal Cell" forIndexPath:indexPath];
cell.userMedalModel = [self.medalModel.userMedals xpSafeObjectAtIndex:indexPath.row];
return cell;
} else {
XPMultipleContentCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Gift Cell" forIndexPath:indexPath];
cell.giftModel = [self.allGifts xpSafeObjectAtIndex:indexPath.row];
return cell;
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat width = (KScreenWidth - 14*2 - 16 - 7 * 3)/4.0;
return self.isMedal ? CGSizeMake(width, kGetScaleWidth(94)) : CGSizeMake(width, 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:@"Medal Cell"];
[_contentCollectionView registerClass:[XPMultipleContentCollectionCell class]
forCellWithReuseIdentifier:@"Gift Cell"];
}
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