Files
peko-ios/YuMi/Modules/YMMine/View/Medals/MedalsCollectionViewCell_Refactored.m

214 lines
6.4 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.

//
// MedalsCollectionViewCell_Refactored.m
// YuMi
//
// 重构示例:使用 MedalMediaDisplayManager 简化媒体处理逻辑
//
#import "MedalsCollectionViewCell.h"
#import "MedalsModel.h"
#import "MedalMediaDisplayManager.h"
#import "MedalsLevelIndicatorView.h"
@interface MedalsCollectionViewCell () <MedalMediaDisplayDelegate>
// 媒体显示管理器 - 替代原来的所有媒体相关属性和方法
@property (nonatomic, strong) MedalMediaDisplayManager *mediaManager;
// UI 元素
@property (nonatomic, strong) NetImageView *imageView;
@property (nonatomic, strong) VAPView *mp4View;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subLabel;
@property (nonatomic, strong) MedalsLevelIndicatorView *levelIndicatorView;
// 数据模型
@property (nonatomic, strong) MedalVo *displayModel;
@property (nonatomic, strong) MedalSeriesItemVo *currentItemVo;
@end
@implementation MedalsCollectionViewCell
+ (NSString *)cellID {
return NSStringFromClass([MedalsCollectionViewCell class]);
}
+ (void)registerTo:(UICollectionView *)collectionView {
[collectionView registerClass:[self class] forCellWithReuseIdentifier:[self cellID]];
}
+ (instancetype)cellFor:(UICollectionView *)collectionView atIndexPath:(NSIndexPath *)index {
MedalsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[self cellID]
forIndexPath:index];
return cell;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupUI];
[self setupMediaManager];
}
return self;
}
- (void)setupUI {
// UI 设置保持不变
[self.contentView addGradientBackgroundWithColors:@[
UIColorFromRGB(0x41007b),
UIColorFromRGB(0x290858)
] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1) cornerRadius:8];
[self.contentView setAllCornerRadius:8
borderWidth:1
borderColor:UIColorFromRGB(0xa166bf)];
self.imageView = [[NetImageView alloc] init];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.contentView addSubview:self.imageView];
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.contentView);
make.top.mas_equalTo(13);
make.leading.trailing.mas_equalTo(self.contentView).inset(13);
make.height.mas_equalTo(self.imageView.mas_width);
}];
self.mp4View = [[VAPView alloc] init];
self.mp4View.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:self.mp4View];
[self.mp4View mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.imageView);
}];
// 其他UI元素设置...
self.titleLabel = [UILabel labelInitWithText:@"" font:kFontMedium(14) textColor:[UIColor whiteColor]];
self.subLabel = [UILabel labelInitWithText:@"" font:kFontRegular(11) textColor:[UIColor colorWithWhite:1 alpha:0.6]];
// ... 约束设置省略
}
- (void)setupMediaManager {
// 创建媒体管理器,传入自己作为代理
self.mediaManager = [[MedalMediaDisplayManager alloc] initWithDelegate:self];
}
- (void)prepareForReuse {
[super prepareForReuse];
// 使用媒体管理器清理资源 - 替代原来的复杂清理逻辑
[self.mediaManager cleanupResources];
// 重置UI状态
self.displayModel = nil;
self.currentItemVo = nil;
self.titleLabel.text = @"";
self.subLabel.text = @"";
[self.levelIndicatorView resetToLevel:0];
}
- (void)updateCell:(MedalSeriesVo *)model isForSquare:(BOOL)isSquare {
MedalSeriesItemVo *itemVos = [model.medalSeries xpSafeObjectAtIndex:0];
self.currentItemVo = itemVos;
// 设置等级指示器
self.levelIndicatorView.indicatorType = MedalsLevelIndicatorTypeNormal;
[self.levelIndicatorView setupWithMaxLevel:itemVos.medalLevel];
if (isSquare) {
self.displayModel = [itemVos.medalVos xpSafeObjectAtIndex:itemVos.medalVos.count - 1];
[self.levelIndicatorView setSelectedLevel:itemVos.medalLevel animated:NO];
} else {
self.displayModel = [itemVos.medalVos xpSafeObjectAtIndex:0];
[self.levelIndicatorView setSelectedLevel:1 animated:NO];
}
self.levelIndicatorView.userInteractionEnabled = !isSquare;
// 使用媒体管理器更新显示 - 替代原来的复杂逻辑
[self.mediaManager updateDisplayWithModel:self.displayModel];
// 更新文本信息
[self updateTextLabels];
}
- (void)updateTextLabels {
self.titleLabel.text = self.displayModel.name;
self.subLabel.text = [self.displayModel expireDateString];
}
#pragma mark - 可见性管理 - 大幅简化
- (void)willDisplay {
[self.mediaManager willDisplay];
}
- (void)didEndDisplaying {
[self.mediaManager didEndDisplaying];
}
#pragma mark - MedalMediaDisplayDelegate - 核心代理方法
- (NSString *)getMP4UrlFromModel:(id)model {
MedalVo *medalVo = (MedalVo *)model;
return medalVo.mp4Url;
}
- (NSString *)getPicUrlFromModel:(id)model {
MedalVo *medalVo = (MedalVo *)model;
return medalVo.picUrl;
}
- (NetImageView *)getImageView {
return self.imageView;
}
- (VAPView *)getMP4View {
return self.mp4View;
}
- (void)onMediaDisplayUpdated:(BOOL)isMP4 success:(BOOL)success {
// 可选:处理媒体显示状态更新
NSLog(@"Media display updated: %@ - %@", isMP4 ? @"MP4" : @"Image", success ? @"Success" : @"Failed");
}
- (UIImage *)getDefaultPlaceholderImage {
return [UIImageConstant defaultEmptyPlaceholder];
}
#pragma mark - 生命周期
- (void)dealloc {
// 媒体管理器会自动处理资源清理
NSLog(@"MedalsCollectionViewCell dealloc");
}
@end
/*
重构效果对比:
原始代码:~528行
重构后代码:~150行减少70%+
移除的重复代码:
- 所有媒体路径属性和管理逻辑
- MP4播放器相关属性和方法
- 复杂的播放状态管理
- 应用生命周期通知处理
- MP4解析和播放控制逻辑
- 失败降级处理逻辑
- 可见性管理的复杂逻辑
保留的业务逻辑:
- UI布局和样式
- 等级指示器相关逻辑
- 业务数据模型处理
- 特定的UI更新逻辑
核心优势:
1. 代码量大幅减少,可读性提升
2. 媒体处理逻辑统一管理
3. Bug修复只需要在一个地方
4. 新功能添加更容易
5. 测试和维护成本降低
*/