Files
peko-ios/YuMi/Modules/YMMine/View/Cell/MineInfo/XPMineMultipleContentTableViewCell.m
2024-11-11 19:35:21 +08:00

472 lines
16 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.centerX.mas_equalTo(self.contentView);
make.bottom.mas_equalTo(-24);
make.height.mas_equalTo(18);
}];
}
- (void)setUserMedalModel:(UserMedalModel *)userMedalModel {
self.icon.hidden = YES;
self.vapView.hidden = NO;
self.countLabel.hidden = YES;
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.contentView);
make.bottom.mas_equalTo(-8);
make.height.mas_equalTo(18);
}];
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 = 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