Files
peko-ios/YuMi/Modules/YMNewHome/View/Cell/XPNewHomePartyCollectionViewCell.m

437 lines
16 KiB
Objective-C

//
// XPNewHomePartyCollectionViewCell.m
// YuMi
//
// Created by duoban on 2023/9/4.
//
#import "XPNewHomePartyCollectionViewCell.h"
#import <SDWebImageFLPlugin/SDWebImageFLPlugin.h>
#import <SVGA.h>
@interface XPNewHomePartyCollectionViewCell()
///背景
@property(nonatomic,strong) NetImageView *avatarView;
///榜单
@property(nonatomic,strong) NetImageView *rankImageView;
///类型
@property(nonatomic,strong) NetImageView *tagImageView;
///房间名
@property(nonatomic,strong) UILabel *nameLabel;
@property(nonatomic,strong) UILabel *subLabel;
///热度
@property(nonatomic,strong) UILabel *heatNumLable;
///热度波浪
@property(nonatomic,strong) UIImageView *heatIcon;
///pk
@property(nonatomic,strong) SVGAParser *parser;
@property(nonatomic,strong) SVGAImageView *pkImageView;
@property (nonatomic, assign) NSInteger membersCount;
@property (nonatomic,strong) NetImageView *boomImageView;
@property (nonatomic, strong) NetImageView *flagImage;
@property (nonatomic, strong) NetImageView *levelImageView;
@property (nonatomic, strong) NSMutableArray<NetImageView *> *iconViews;
@property(nonatomic, strong) UIStackView *iconStackView;
@property(nonatomic, strong) UIStackView *nameStackView;
@property(nonatomic, strong) UIStackView *subTabStackView;
@end
@implementation XPNewHomePartyCollectionViewCell
- (NetImageView *)createIconViewWithTag:(NSInteger)tag {
NetImageConfig *config = [[NetImageConfig alloc] init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
NetImageView *iconView = [[NetImageView alloc] initWithConfig:config];
iconView.tag = tag;
iconView.layer.cornerRadius = kGetScaleWidth(20)/2;
iconView.layer.masksToBounds = YES;
iconView.layer.borderWidth = 1;
iconView.layer.borderColor = [UIColor whiteColor].CGColor;
iconView.hidden = YES; // 默认隐藏
[self.contentView addSubview:iconView];
return iconView;
}
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
self.clipsToBounds = NO;
self.contentView.clipsToBounds = NO;
self.membersCount = (kGetScaleWidth(345) - kGetScaleWidth(90))/kGetScaleWidth(20);
self.iconViews = [NSMutableArray array];
for (int i = 0; i < self.membersCount; i++) {
NetImageView *iconView = [self createIconViewWithTag:100 + i];
[self.iconViews addObject:iconView];
}
[self installUI];
[self installConstraints];
}
return self;
}
-(void)installUI{
self.contentView.backgroundColor = [UIColor clearColor];
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor whiteColor];
[bg setCornerRadius:10];
[self.contentView addSubview:bg];
[bg mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.contentView);
}];
[self.contentView addSubview:self.rankImageView];
[self.contentView addSubview:self.avatarView];
[self.avatarView addSubview:self.pkImageView];
[self.contentView addSubview:self.levelImageView];
_nameStackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.flagImage, self.nameLabel]];
_nameStackView.spacing = 4;
[self.contentView addSubview:self.nameStackView];
_subTabStackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.tagImageView, self.subLabel]];
_subTabStackView.spacing = 4;
[self.contentView addSubview:self.subTabStackView];
[self.contentView addSubview:self.heatIcon];
[self.contentView addSubview:self.heatNumLable];
[self.contentView addSubview:self.boomImageView];
@kWeakify(self);
[self.parser parseWithNamed:@"pi_home_new_pk" inBundle:nil completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
@kStrongify(self);
self.pkImageView.loops = INT_MAX;
self.pkImageView.clearsAfterStop = NO;
self.pkImageView.videoItem = videoItem;
[self.pkImageView startAnimation];
} failureBlock:^(NSError * _Nullable error) {
}];
}
-(void)installConstraints{
[self.rankImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.contentView).insets(UIEdgeInsetsMake(-20, -10, 0, -10));
}];
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(kGetScaleWidth(73));
make.leading.mas_equalTo(kGetScaleWidth(10));
make.bottom.mas_equalTo(-kGetScaleWidth(10));
}];
[self.boomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView).offset(11);
make.trailing.mas_equalTo(self.contentView).offset(-8);
make.size.mas_equalTo(CGSizeMake(40, 40));
}];
[self.nameStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.avatarView);
make.leading.mas_equalTo(self.avatarView.mas_trailing).offset(12);
make.trailing.mas_equalTo(self.boomImageView.mas_leading);
make.height.mas_equalTo(22);
}];
[self.flagImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(18);
}];
[self.subTabStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.nameStackView.mas_bottom).offset(4);
make.leading.mas_equalTo(self.nameStackView);
make.height.mas_equalTo(22);
make.trailing.mas_equalTo(self.boomImageView.mas_leading);
}];
[self.tagImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(0);
}];
[self.heatNumLable mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.mas_equalTo(-8);
make.bottom.mas_equalTo(-kGetScaleWidth(10));
make.height.mas_equalTo(kGetScaleWidth(17));
}];
[self.heatIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(kGetScaleWidth(14));
make.height.mas_equalTo(kGetScaleWidth(14));
make.trailing.equalTo(self.heatNumLable.mas_leading).mas_offset(-4);
make.centerY.equalTo(self.heatNumLable);
}];
[self.pkImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(kGetScaleWidth(23));
make.height.mas_equalTo(kGetScaleWidth(22));
make.trailing.mas_equalTo(-kGetScaleWidth(6));
make.bottom.equalTo(self.avatarView.mas_bottom).mas_offset(-kGetScaleWidth(6));
}];
[self.levelImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.avatarView).offset(-2);
make.leading.mas_equalTo(self.avatarView);
make.size.mas_equalTo(CGSizeMake(40, 16));
}];
_iconStackView = [[UIStackView alloc] initWithArrangedSubviews:self.iconViews];
_iconStackView.spacing = -4;
[self.contentView addSubview:self.iconStackView];
[self.iconStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.avatarView.mas_trailing).offset(12);
make.bottom.equalTo(self.avatarView.mas_bottom);
make.height.mas_equalTo(kGetScaleWidth(20));
}];
}
- (void)prepareForReuse {
[super prepareForReuse];
// 停止并清理动画,避免复用时占用内存
[self.pkImageView stopAnimation];
self.pkImageView.videoItem = nil;
self.pkImageView.hidden = YES;
// 重置图像资源,打断潜在的异步加载引用
[self.avatarView cancelLoadImage];
self.avatarView.imageUrl = nil;
self.rankImageView.image = nil;
[self.tagImageView cancelLoadImage];
self.tagImageView.image = nil;
self.tagImageView.hidden = YES;
[self.levelImageView cancelLoadImage];
self.levelImageView.imageUrl = nil;
[self.boomImageView cancelLoadImage];
self.boomImageView.imageUrl = nil;
// 重置文本
self.nameLabel.attributedText = nil;
self.nameLabel.text = @"";
self.subLabel.text = @"";
self.heatNumLable.text = @"0";
// 重置成员头像视图:取消下载+清空,隐藏
for (NetImageView *iconView in self.iconViews) {
[iconView cancelLoadImage];
iconView.imageUrl = nil;
iconView.hidden = YES;
}
}
- (void)dealloc {
// 组件销毁时也保证动画资源释放
[self.pkImageView stopAnimation];
self.pkImageView.videoItem = nil;
}
- (void)setRoomInfo:(HomePlayRoomModel *)roomInfo{
_roomInfo = roomInfo;
_avatarView.imageUrl = _roomInfo.avatar;
self.levelImageView.imageUrl = roomInfo.roomLevelIcon;
[self updateMicUsersDisplay];
switch (roomInfo.homeRoomType) {
case 1:{
NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image = kImage(@"room_type_rd");
attach.bounds = CGRectMake(0, 2, 14, 14);
NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
NSAttributedString *titleString = [[NSAttributedString alloc] initWithString:roomInfo.title
attributes:@{
NSFontAttributeName: kFontSemibold(15),
NSForegroundColorAttributeName: UIColorFromRGB(0x313131)
}];
NSMutableAttributedString *ms = [[NSMutableAttributedString alloc] initWithAttributedString:titleString];
[ms insertAttributedString:attachString atIndex:0];
self.nameLabel.attributedText = ms;
}
break;
case 2:
{
NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image = kImage(@"room_type_cs");
attach.bounds = CGRectMake(0, 2, 14, 14);
NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
NSAttributedString *titleString = [[NSAttributedString alloc] initWithString:roomInfo.title
attributes:@{
NSFontAttributeName: kFontSemibold(15),
NSForegroundColorAttributeName: UIColorFromRGB(0x313131)
}];
NSMutableAttributedString *ms = [[NSMutableAttributedString alloc] initWithAttributedString:titleString];
[ms insertAttributedString:attachString atIndex:0];
_nameLabel.attributedText = ms;
}
break;
default:
_nameLabel.text = _roomInfo.title;
break;
}
if (roomInfo.roomBoom) {
self.boomImageView.hidden = NO;
self.boomImageView.imageUrl = [roomInfo roomBoomPic];
} else {
self.boomImageView.hidden = YES;
}
if ([NSString isEmpty:roomInfo.tagPict]) {
self.subLabel.text = roomInfo.roomDesc.length > 0 ? roomInfo.roomDesc : YMLocalizedString(@"XPHomeMineViewController3");
self.tagImageView.hidden = YES;
} else {
@kWeakify(self);
[self.tagImageView loadImageWithUrl:roomInfo.tagPict
completion:^(UIImage * _Nullable image, NSURL * _Nonnull url) {
@kStrongify(self);
if (image) {
self.tagImageView.hidden = NO;
self.tagImageView.image = image;
CGFloat width = kGetScaleWidth(62);
if (image.size.height > 0){
width = image.size.width * kGetScaleWidth(19) / image.size.height;
}
[self.tagImageView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(width);
}];
[self layoutIfNeeded];
} else {
self.tagImageView.hidden = YES;
}
self.subLabel.text = [NSString stringWithFormat:@" %@", roomInfo.roomDesc.length > 0 ? roomInfo.roomDesc : YMLocalizedString(@"XPHomeMineViewController3")];
}];
}
self.flagImage.imageUrl = roomInfo.regionFlag;
self.flagImage.hidden = [NSString isEmpty:roomInfo.regionFlag];
self.heatNumLable.text = [NSString isEmpty:roomInfo.hotValue.stringValue] ? @"0" : @(roomInfo.hotValue.integerValue).stringValue;
self.pkImageView.hidden = !_roomInfo.crossPking;
if (roomInfo.hourTop > 0) {
NSString *name = [NSString stringWithFormat:@"room_top_%ld", (long)roomInfo.hourTop];
_rankImageView.image = [UIImage getLanguageImage:name];
} else {
_rankImageView.image = nil;
}
[self updateMicUsersDisplay];
[self layoutIfNeeded];
}
#pragma mark - Private Methods
- (void)updateMicUsersDisplay {
for (int i = 0; i < self.membersCount; i++) {
NetImageView *iconView = _iconViews[i];
if (i < _roomInfo.micUsers.count) {
iconView.hidden = NO;
// 更新约束和内容
[iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(kGetScaleWidth(20));
}];
HomePlayMicUserModel *playModel = _roomInfo.micUsers[i];
iconView.imageUrl = playModel.avatar;
} else {
iconView.hidden = YES;
}
}
}
#pragma mark - 懒加载
- (NetImageView *)avatarView{
if(!_avatarView){
NetImageConfig *config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultEmptyAvatarPlaceholder];
_avatarView = [[NetImageView alloc]initWithConfig:config];
_avatarView.contentMode = UIViewContentModeScaleAspectFill;
[_avatarView setCornerWithLeftTopCorner:kGetScaleWidth(10) rightTopCorner:kGetScaleWidth(10) bottomLeftCorner:kGetScaleWidth(10) bottomRightCorner:kGetScaleWidth(10) size:CGSizeMake(kGetScaleWidth(72), kGetScaleWidth(72))];
}
return _avatarView;
}
- (NetImageView *)rankImageView{
if(!_rankImageView){
_rankImageView = [NetImageView new];
}
return _rankImageView;
}
- (NetImageView *)tagImageView{
if(!_tagImageView){
_tagImageView = [NetImageView new];
}
return _tagImageView;
}
- (NetImageView *)boomImageView{
if(!_boomImageView){
_boomImageView = [NetImageView new];
_boomImageView.contentMode = UIViewContentModeScaleAspectFit;
}
return _boomImageView;
}
- (NetImageView *)flagImage{
if(!_flagImage){
_flagImage = [NetImageView new];
_flagImage.contentMode = UIViewContentModeScaleAspectFit;
}
return _flagImage;
}
- (UILabel *)nameLabel{
if(!_nameLabel){
_nameLabel = [UILabel labelInitWithText:@"" font:kFontMedium(15) textColor:UIColorFromRGB(0x1E1E1F)];
}
return _nameLabel;
}
-(UILabel *)heatNumLable{
if(!_heatNumLable){
_heatNumLable = [UILabel labelInitWithText:@"0" font:kFontMedium(10) textColor:UIColorFromRGB(0x84868A)];
_heatNumLable.textAlignment = NSTextAlignmentRight;
}
return _heatNumLable;
}
- (UIImageView *)heatIcon{
if(!_heatIcon){
_heatIcon = [UIImageView new];
_heatIcon.image = kImage(@"ms_home_heat_icon");
}
return _heatIcon;
}
- (SVGAParser *)parser{
if(!_parser){
_parser = [SVGAParser new];
}
return _parser;
}
- (SVGAImageView *)pkImageView {
if (_pkImageView == nil) {
_pkImageView = [[SVGAImageView alloc]init];
_pkImageView.contentMode = UIViewContentModeScaleToFill;
_pkImageView.hidden = YES;
_pkImageView.backgroundColor = [UIColor clearColor];
}
return _pkImageView;
}
- (UILabel *)subLabel{
if(!_subLabel){
_subLabel = [UILabel labelInitWithText:@"" font:kFontRegular(12) textColor:UIColorFromRGB(0x1E1E1F)];
}
return _subLabel;
}
- (NetImageView *)levelImageView{
if(!_levelImageView){
_levelImageView = [[NetImageView alloc]init];
}
return _levelImageView;
}
@end