Files
peko-ios/YuMi/Modules/YMRoom/View/RoomMode/RoomModeViewController.m
2025-01-07 20:07:54 +08:00

1166 lines
43 KiB
Objective-C

//
// RoomModeViewController.m
// YuMi
//
// Created by P on 2024/12/23.
//
#import "RoomModeViewController.h"
#import <SVGA.h>
#import "RoomModePresenter.h"
#import "RoomResourceManager.h"
#import "XPWebViewController.h"
@interface RoomModeCollectionCell : UICollectionViewCell
@property(nonatomic, strong) RoomMicInfoModel *micInfoModel;
@property(nonatomic, strong) UIView *statusView;
@property(nonatomic, strong) NetImageView *imageView;
@property(nonatomic, strong) SVGAImageView *svgaImageView;
@property(nonatomic, strong) SVGAParser *parser;
@property(nonatomic, strong) UILabel *nameLabel;
@property(nonatomic, assign) BOOL isForRoomType;
+ (void)registerTo:(UICollectionView *)collectionView;
+ (RoomModeCollectionCell *)cellFro:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath displayName:(BOOL)displayName;
// 阿语布局环境下可能会有重复的内容显示,尝试每个 cell 都独立一个 id
+ (void)registerTo:(UICollectionView *)collectionView forIndex:(NSInteger)index;
+ (RoomModeCollectionCell *)cellFroIndex:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath displayName:(BOOL)displayName;
@end
@implementation RoomModeCollectionCell
+ (void)registerTo:(UICollectionView *)collectionView {
[collectionView registerClass:[self class] forCellWithReuseIdentifier:@"RoomModeCollectionCell"];
}
+ (void)registerTo:(UICollectionView *)collectionView forIndex:(NSInteger)index {
[collectionView registerClass:[self class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"RoomModeCollectionCell_%@", @(index)]];
}
+ (RoomModeCollectionCell *)cellFro:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath displayName:(BOOL)displayName {
RoomModeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RoomModeCollectionCell" forIndexPath:indexPath];
cell.nameLabel.hidden = !displayName;
return cell;
}
+ (RoomModeCollectionCell *)cellFroIndex:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath displayName:(BOOL)displayName {
RoomModeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"RoomModeCollectionCell_%@", @(indexPath.row)]
forIndexPath:indexPath];
cell.nameLabel.hidden = !displayName;
return cell;
}
- (void)dealloc {
if (_svgaImageView) {
[self.svgaImageView stopAnimation];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
}
- (void)prepareForReuse {
[super prepareForReuse];
self.imageView.image = nil;
if (_svgaImageView) {
[self.svgaImageView stopAnimation];
}
}
- (void)setMicInfoModel:(RoomMicInfoModel *)micInfoModel {
_micInfoModel = micInfoModel;
if ([micInfoModel.id isEqualToString:@"-1"]) {
if (micInfoModel.dressType == MicResourceType_Skin) {
self.imageView.image = kImage(@"room_mode_default_skin");
}else {
self.imageView.image = kImage(@"room_mic_normal");
}
} else {
if (micInfoModel.dressType == MicResourceType_Skin) {
self.imageView.imageUrl = micInfoModel.normalMicUrl;
} else {
[self playSVGA:micInfoModel.normalMicUrl];
}
}
}
- (void)setIsForRoomType:(BOOL)isForRoomType {
_isForRoomType = isForRoomType;
if (isForRoomType) {
[self.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(self.statusView.mas_height).multipliedBy(0.8);
}];
}
}
- (void)playSVGA:(NSString *)path{
if (!_svgaImageView) {
[self.contentView addSubview:self.svgaImageView];
[self.svgaImageView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.mas_equalTo(self.imageView);
make.center.mas_equalTo(self.statusView);
make.width.height.mas_equalTo(self.statusView.mas_height).multipliedBy(0.85);
}];
}
@kWeakify(self);
[self.parser parseWithURL:[NSURL URLWithString:path] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
@kStrongify(self);
self.svgaImageView.loops = INT_MAX;
self.svgaImageView.clearsAfterStop = YES;
self.svgaImageView.videoItem = videoItem;
[self.svgaImageView startAnimation];
} failureBlock:^(NSError * _Nullable error) { }];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self.contentView addSubview:self.statusView];
[self.statusView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo(self.contentView);
make.height.mas_equalTo(87);
}];
[self.contentView addSubview:self.nameLabel];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.statusView.mas_bottom).offset(4);
make.leading.trailing.mas_equalTo(self.statusView);
make.height.mas_equalTo(18);
}];
[self.contentView addSubview:self.imageView];
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self.statusView);
make.width.height.mas_equalTo(self.statusView.mas_height).multipliedBy(0.65);
}];
}
return self;
}
- (void)setSelected:(BOOL)selected {
self.statusView.layer.borderWidth = selected ? 1 : 0;
self.statusView.backgroundColor = selected ? UIColorRGBAlpha(0xFF8C03, 0.1) : UIColorFromRGB(0x1b1b1d);
self.nameLabel.textColor = [UIColor colorWithWhite:1 alpha:selected ? 1 : 0.5];
}
- (UIView *)statusView {
if (!_statusView) {
_statusView = [[UIView alloc] init];
_statusView.backgroundColor = UIColorFromRGB(0x1b1b1d);
_statusView.layer.borderColor = UIColorFromRGB(0xFF8C03).CGColor;
_statusView.layer.borderWidth = 0;
[_statusView setCornerRadius:12];
}
return _statusView;
}
- (UILabel *)nameLabel {
if (!_nameLabel) {
_nameLabel = [UILabel labelInitWithText:@"?" font:kFontRegular(13) textColor:[UIColor whiteColor]];
_nameLabel.textAlignment = NSTextAlignmentCenter;
}
return _nameLabel;
}
- (NetImageView *)imageView {
if (!_imageView) {
_imageView = [[NetImageView alloc] init];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
}
return _imageView;
}
- (SVGAImageView *)svgaImageView {
if (!_svgaImageView) {
_svgaImageView = [[SVGAImageView alloc] init];
_svgaImageView.contentMode = UIViewContentModeScaleAspectFit;
}
return _svgaImageView;
}
- (SVGAParser *)parser{
if(!_parser){
_parser = [SVGAParser new];
}
return _parser;
}
@end
@interface RoomModeOwnerCard : UITableViewCell
@property(nonatomic, strong) RoomInfoModel *roomInfo;
@property(nonatomic, strong) RoomLevelInfoModel *LevelInfo;
@property(nonatomic, strong) UIImageView *backgroundImageView;
@property(nonatomic, strong) NetImageView *avatarImage;
@property(nonatomic, strong) UILabel *nameLabel;
@property(nonatomic, strong) NetImageView *levelImage;
@property(nonatomic, strong) UILabel *valueLabel;
@property(nonatomic, strong) UILabel *adminLabel;
@property(nonatomic, strong) UILabel *currentLevelLabel;
@property(nonatomic, strong) UILabel *nextLevelLabel;
@property(nonatomic, strong) UIProgressView *expProgressView;
@property(nonatomic, strong) UILabel *expToNextLabel;
+ (void)registerTo:(UITableView *)tableView;
+ (RoomModeOwnerCard *)cellFro:(UITableView *)tableView
indexPath:(NSIndexPath *)indexPath
roomInfo:(RoomInfoModel *)roomInfo
levelInfo:(RoomLevelInfoModel *)levelInfo;
@end
@implementation RoomModeOwnerCard
+ (CGFloat)cellHeight {
return 7 + 176 + 0;
}
+ (void)registerTo:(UITableView *)tableView {
[tableView registerClass:[self class] forCellReuseIdentifier:@"RoomModeOwnerCard"];
}
+ (RoomModeOwnerCard *)cellFro:(UITableView *)tableView
indexPath:(NSIndexPath *)indexPath
roomInfo:(RoomInfoModel *)roomInfo
levelInfo:(RoomLevelInfoModel *)levelInfo {
RoomModeOwnerCard *card = [tableView dequeueReusableCellWithIdentifier:@"RoomModeOwnerCard" forIndexPath:indexPath];
card.selectionStyle = UITableViewCellSelectionStyleNone;
card.backgroundColor = [UIColor clearColor];
card.contentView.backgroundColor = [UIColor clearColor];
card.roomInfo = roomInfo;
card.LevelInfo = levelInfo;
return card;
}
- (void)setRoomInfo:(RoomInfoModel *)roomInfo {
_roomInfo = roomInfo;
self.avatarImage.imageUrl = roomInfo.avatar;
self.nameLabel.text = roomInfo.nick;
}
- (void)setLevelInfo:(RoomLevelInfoModel *)LevelInfo {
_LevelInfo = LevelInfo;
self.levelImage.imageUrl = LevelInfo.currentLevelIcon;
self.expProgressView.progress = (LevelInfo.roomVal - LevelInfo.currentLevelExp) * 100.0 / (LevelInfo.nextLevelExp - LevelInfo.currentLevel) / 100.0;
self.valueLabel.text = [NSString stringWithFormat:@"%@%@", YMLocalizedString(@"1.0.33_text_6"), @(LevelInfo.roomVal)];
self.nextLevelLabel.text = [NSString stringWithFormat:@"%@%@", YMLocalizedString(@"1.0.33_text_9"), @(LevelInfo.nextLevel)];
self.currentLevelLabel.text = [NSString stringWithFormat:@"%@%@", YMLocalizedString(@"1.0.33_text_9"), @(LevelInfo.currentLevel)];
self.expToNextLabel.text = [NSString stringWithFormat:@"%@%@", YMLocalizedString(@"1.0.33_text_8"), @(LevelInfo.nextLevelExp - LevelInfo.roomVal)];
self.adminLabel.text = [NSString stringWithFormat:YMLocalizedString(@"1.0.33_text_7"), @(LevelInfo.currentManagerNum), @(LevelInfo.managerLimitNum)];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self.contentView addSubview:self.backgroundImageView];
[self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView).offset(7);
make.leading.trailing.mas_equalTo(self.contentView).inset(15);
make.height.mas_equalTo(176);
}];
[self.contentView addSubview:self.avatarImage];
[self.avatarImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.top.mas_equalTo(self.backgroundImageView).offset(18);
make.size.mas_equalTo(CGSizeMake(79, 79));
}];
[self.contentView addSubview:self.nameLabel];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.avatarImage);
make.leading.mas_equalTo(self.avatarImage.mas_trailing).offset(12);
make.trailing.mas_equalTo(self.backgroundImageView);
make.height.mas_equalTo(22);
}];
[self.contentView addSubview:self.levelImage];
[self.levelImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(1);
make.leading.mas_equalTo(self.nameLabel);
make.size.mas_equalTo(CGSizeMake(52, 20));
}];
[self.contentView addSubview:self.valueLabel];
[self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.levelImage.mas_bottom).offset(1);
make.leading.mas_equalTo(self.nameLabel);
make.height.mas_equalTo(18);
}];
[self.contentView addSubview:self.adminLabel];
[self.adminLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.valueLabel.mas_bottom).offset(1);
make.leading.mas_equalTo(self.nameLabel);
make.height.mas_equalTo(18);
}];
[self.contentView addSubview:self.currentLevelLabel];
[self.currentLevelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.avatarImage);
make.top.mas_equalTo(self.avatarImage.mas_bottom).offset(12);
make.height.mas_equalTo(18);
}];
[self.contentView addSubview:self.nextLevelLabel];
[self.nextLevelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.mas_equalTo(self.backgroundImageView).offset(-16);
make.top.mas_equalTo(self.avatarImage.mas_bottom).offset(12);
make.height.mas_equalTo(18);
}];
[self.contentView addSubview:self.expProgressView];
[self.expProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.currentLevelLabel);
make.trailing.mas_equalTo(self.nextLevelLabel);
make.top.mas_equalTo(self.currentLevelLabel.mas_bottom).offset(4);
make.height.mas_equalTo(6);
}];
[self.contentView addSubview:self.expToNextLabel];
[self.expToNextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.backgroundImageView);
make.bottom.mas_equalTo(self.backgroundImageView).offset(-16);
make.height.mas_equalTo(18);
}];
}
return self;
}
- (UIImageView *)backgroundImageView {
if (!_backgroundImageView) {
_backgroundImageView = [[UIImageView alloc] initWithImage:kImage(@"room_mode_card_bg")];
}
return _backgroundImageView;
}
- (NetImageView *)avatarImage {
if (!_avatarImage) {
_avatarImage = [[NetImageView alloc] init];
[_avatarImage setCornerRadius:8
corners:kCALayerMaxXMaxYCorner | kCALayerMaxXMinYCorner | kCALayerMinXMaxYCorner | kCALayerMinXMinYCorner
borderWidth:1 borderColor:[UIColor whiteColor]];
}
return _avatarImage;
}
- (UILabel *)nameLabel {
if (!_nameLabel) {
_nameLabel = [UILabel labelInitWithText:@"" font:kFontMedium(16) textColor:[UIColor whiteColor]];
}
return _nameLabel;
}
- (NetImageView *)levelImage {
if (!_levelImage) {
_levelImage = [[NetImageView alloc] init];
}
return _levelImage;
}
- (UILabel *)valueLabel {
if (!_valueLabel) {
_valueLabel = [UILabel labelInitWithText:YMLocalizedString(@"1.0.33_text_6") font:kFontRegular(12) textColor:[UIColor colorWithWhite:1 alpha:0.8]];
}
return _valueLabel;
}
- (UILabel *)adminLabel {
if (!_adminLabel) {
_adminLabel = [UILabel labelInitWithText:YMLocalizedString(@"1.0.33_text_7") font:kFontRegular(12) textColor:[UIColor colorWithWhite:1 alpha:0.8]];
}
return _adminLabel;
}
- (UILabel *)currentLevelLabel {
if (!_currentLevelLabel) {
_currentLevelLabel = [UILabel labelInitWithText:YMLocalizedString(@"1.0.33_text_9") font:kFontMedium(12) textColor:[UIColor whiteColor]];
}
return _currentLevelLabel;
}
- (UILabel *)nextLevelLabel {
if (!_nextLevelLabel) {
_nextLevelLabel = [UILabel labelInitWithText:YMLocalizedString(@"1.0.33_text_9") font:kFontMedium(12) textColor:[UIColor whiteColor]];
_nextLevelLabel.textAlignment = NSTextAlignmentRight;
}
return _nextLevelLabel;
}
- (UIProgressView *)expProgressView {
if (!_expProgressView) {
_expProgressView = [[UIProgressView alloc] init];
_expProgressView.progressTintColor = [UIColor whiteColor];
_expProgressView.trackTintColor = [UIColor colorWithWhite:1 alpha:0.2];
_expProgressView.progress = 0.0;
}
return _expProgressView;
}
- (UILabel *)expToNextLabel {
if (!_expToNextLabel) {
_expToNextLabel = [UILabel labelInitWithText:YMLocalizedString(@"1.0.33_text_8") font:kFontRegular(12) textColor:[UIColor colorWithWhite:1 alpha:0.8]];
}
return _expToNextLabel;
}
@end
@interface RoomModeTypeCard : UITableViewCell <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, assign) RoomType type;
@property(nonatomic, copy)NSArray *dataSource;
@property(nonatomic, copy) void(^updateSelectedType)(RoomType type);
+ (void)registerTo:(UITableView *)tableView;
+ (RoomModeTypeCard *)cellFro:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath dataSource:(NSArray *)dataSource currentType:(RoomType)type;
@end
@implementation RoomModeTypeCard
+ (CGFloat)cellHeight:(NSInteger)cellCount {
NSInteger line = cellCount/3 + cellCount%3;
return MAX(0, line) * (90 + 4 + 18) + MAX(0, line-1) * 10;
}
+ (void)registerTo:(UITableView *)tableView {
[tableView registerClass:[self class] forCellReuseIdentifier:@"RoomModeTypeCard"];
}
+ (RoomModeTypeCard *)cellFro:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath dataSource:(NSArray *)dataSource currentType:(RoomType)type {
RoomModeTypeCard *card = [tableView dequeueReusableCellWithIdentifier:@"RoomModeTypeCard" forIndexPath:indexPath];
card.selectionStyle = UITableViewCellSelectionStyleNone;
card.backgroundColor = [UIColor clearColor];
card.contentView.backgroundColor = [UIColor clearColor];
card.dataSource = dataSource;
card.type = type;
[card.collectionView reloadData];
NSInteger row = 0;
switch (type) {
case RoomType_Game:
row = 0;
break;
case RoomType_10Mic:
row = 1;
break;
case RoomType_15Mic:
row = 2;
break;
case RoomType_19Mic:
row = 4;
break;
case RoomType_20Mic:
row = 3;
break;
default:
break;
}
[card.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
return card;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self.contentView addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.mas_equalTo(self.contentView).inset(15);
make.top.bottom.mas_equalTo(self.contentView);
}];
}
return self;
}
- (void)setDataSource:(NSArray *)dataSource {
_dataSource = dataSource;
[self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RoomModeCollectionCell *cell = [RoomModeCollectionCell cellFro:collectionView indexPath:indexPath displayName:YES];
cell.isForRoomType = YES;
NSDictionary *dic = [self.dataSource xpSafeObjectAtIndex:indexPath.row];
if (dic) {
cell.selected = [[dic objectForKey:@"isUses"] boolValue];
NSString *imageName = [NSString stringWithFormat:@"room_mode_mic_%@_%@", [dic objectForKey:@"type"], cell.isSelected ? @"on" : @"off"];
cell.imageView.image = kImage(imageName);
cell.nameLabel.text = [dic objectForKey:@"name"];
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (_updateSelectedType) {
RoomType selectedType = RoomType_Game;
switch (indexPath.row) {
case 0:
selectedType = RoomType_Game;
break;
case 1:
selectedType = RoomType_10Mic;
break;
case 2:
selectedType = RoomType_15Mic;
break;
case 3:
selectedType = RoomType_20Mic;
break;
case 4:
selectedType = RoomType_19Mic;
break;
default:
break;
}
self.updateSelectedType(selectedType);
}
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake((KScreenWidth - 31 - 14)/3, 87 + 4 + 18);
layout.minimumLineSpacing = 10;
layout.minimumInteritemSpacing = 4;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.scrollEnabled = NO;
[RoomModeCollectionCell registerTo:_collectionView];
}
return _collectionView;
}
@end
@interface RoomModeMicSkinCard : UITableViewCell <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property(nonatomic, assign) NSInteger usedID;
@property(nonatomic, assign) NSInteger roomLevel;
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) NSMutableArray <RoomMicInfoModel *>*skins;
@property(nonatomic, copy) void(^updateSelectedSkinID)(NSString *skinID);
+ (void)registerTo:(UITableView *)tableView;
+ (RoomModeMicSkinCard *)cellFro:(UITableView *)tableView
indexPath:(NSIndexPath *)indexPath
skins:(NSArray <RoomMicInfoModel *> *)skins
usedID:(NSInteger)usedID
roomLevel:(NSInteger)roomLevel;
@end
@implementation RoomModeMicSkinCard
+ (CGFloat)cellHeight:(NSInteger)cellCount {
NSInteger line = cellCount/3 + cellCount%3;
return MAX(0, line) * (87) + MAX(0, line-1) * 10;
}
+ (void)registerTo:(UITableView *)tableView {
[tableView registerClass:[self class] forCellReuseIdentifier:@"RoomModeMicSkinCard"];
}
+ (RoomModeMicSkinCard *)cellFro:(UITableView *)tableView
indexPath:(NSIndexPath *)indexPath
skins:(NSArray <RoomMicInfoModel *> *)skins
usedID:(NSInteger)usedID
roomLevel:(NSInteger)roomLevel {
RoomModeMicSkinCard *card = [tableView dequeueReusableCellWithIdentifier:@"RoomModeMicSkinCard" forIndexPath:indexPath];
card.selectionStyle = UITableViewCellSelectionStyleNone;
card.backgroundColor = [UIColor clearColor];
card.contentView.backgroundColor = [UIColor clearColor];
card.usedID = usedID;
card.roomLevel = roomLevel;
card.skins = skins.mutableCopy;
if (skins.count > 0) {
[card.collectionView reloadData];
[card manualSelectedCell];
}
return card;
}
- (void)manualSelectedCell {
__block NSInteger row = 0;
[self.skins enumerateObjectsUsingBlock:^(RoomMicInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.id isEqualToString:@(self.usedID).stringValue]) {
row = idx;
*stop = YES;
}
}];
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]
animated:NO
scrollPosition:UICollectionViewScrollPositionNone];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self.contentView addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.mas_equalTo(self.contentView).inset(15);
make.top.bottom.mas_equalTo(self.contentView);
}];
[self.collectionView reloadData];
}
return self;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.skins.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RoomModeCollectionCell *cell = [RoomModeCollectionCell cellFro:collectionView indexPath:indexPath displayName:NO];
cell.micInfoModel = [self.skins xpSafeObjectAtIndex:indexPath.row];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
RoomMicInfoModel *model = [self.skins xpSafeObjectAtIndex:indexPath.row];
if ([model.id isEqualToString:@(self.usedID).stringValue] || (model.id.integerValue == -1 && self.usedID == 0)) {
return;
}
if (model.reachLevel <= self.roomLevel) {
if (self.updateSelectedSkinID) {
self.updateSelectedSkinID(model.id);
}
} else {
[self manualSelectedCell];
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"1.0.33_text_15")];
}
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake((KScreenWidth - 31 - 14)/3, 87);
layout.minimumLineSpacing = 10;
layout.minimumInteritemSpacing = 4;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.scrollEnabled = NO;
[RoomModeCollectionCell registerTo:_collectionView];
}
return _collectionView;
}
@end
@interface RoomModeMicEffectCard : UITableViewCell <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property(nonatomic, assign) NSInteger usedID;
@property(nonatomic, assign) NSInteger roomLevel;
@property(nonatomic, strong) NSMutableArray *effects;
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, copy) void(^updateSelectedEffectID)(NSString *effectID);
+ (void)registerTo:(UITableView *)tableView;
+ (RoomModeMicEffectCard *)cellFro:(UITableView *)tableView
indexPath:(NSIndexPath *)indexPath
effects:(NSArray <RoomMicInfoModel *> *)effects
usedID:(NSInteger)usedID
roomLevel:(NSInteger)roomLevel ;
@end
@implementation RoomModeMicEffectCard
+ (CGFloat)cellHeight:(NSInteger)cellCount {
NSInteger line = cellCount/3 + cellCount%3;
return MAX(0, line) * (87) + MAX(0, line-1) * 10;
}
+ (void)registerTo:(UITableView *)tableView {
[tableView registerClass:[self class] forCellReuseIdentifier:@"RoomModeMicEffectCard"];
}
+ (RoomModeMicEffectCard *)cellFro:(UITableView *)tableView
indexPath:(NSIndexPath *)indexPath
effects:(NSArray <RoomMicInfoModel *> *)effects
usedID:(NSInteger)usedID
roomLevel:(NSInteger)roomLevel {
RoomModeMicEffectCard *card = [tableView dequeueReusableCellWithIdentifier:@"RoomModeMicEffectCard" forIndexPath:indexPath];
card.selectionStyle = UITableViewCellSelectionStyleNone;
card.backgroundColor = [UIColor clearColor];
card.contentView.backgroundColor = [UIColor clearColor];
card.usedID = usedID;
card.roomLevel = roomLevel;
card.effects = effects.mutableCopy;
if (effects.count > 0) {
[card.collectionView reloadData];
[card manualSelectedCell];
}
return card;
}
- (void)manualSelectedCell {
__block NSInteger row = 0;
[self.effects enumerateObjectsUsingBlock:^(RoomMicInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.id isEqualToString:@(self.usedID).stringValue]) {
row = idx;
*stop = YES;
}
}];
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self.contentView addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.mas_equalTo(self.contentView).inset(15);
make.top.bottom.mas_equalTo(self.contentView);
}];
[self.collectionView reloadData];
}
return self;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.effects.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RoomModeCollectionCell *cell = [RoomModeCollectionCell cellFro:collectionView indexPath:indexPath displayName:NO];
cell.micInfoModel = [self.effects xpSafeObjectAtIndex:indexPath.row];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
RoomMicInfoModel *model = [self.effects xpSafeObjectAtIndex:indexPath.row];
if ([model.id isEqualToString:@(self.usedID).stringValue] || (model.id.integerValue == -1 && self.usedID == 0)) {
return;
}
if (model.reachLevel <= self.roomLevel) {
if (self.updateSelectedEffectID) {
self.updateSelectedEffectID(model.id);
}
} else {
[self manualSelectedCell];
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"1.0.33_text_15")];
}
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake((KScreenWidth - 31 - 14)/3, 87);
layout.minimumLineSpacing = 10;
layout.minimumInteritemSpacing = 4;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.scrollEnabled = NO;
[RoomModeCollectionCell registerTo:_collectionView];
}
return _collectionView;
}
@end
@interface RoomModeViewController () <UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) RoomLevelInfoModel *levelInfo;
@property(nonatomic, assign) RoomType originalRoomType;
@property(nonatomic, assign) RoomType updatedRoomType;
@property(nonatomic, copy) NSString *updatedMicSkinID;
@property(nonatomic, copy) NSString *updatedMicEffectID;
@property(nonatomic, strong) UIButton *useButton;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *typeDataSource;
@property(nonatomic, strong) NSMutableArray *micSkinDataSource;
@property(nonatomic, strong) NSMutableArray *micEffectDataSource;
@end
// TODO: 补充缓存的使用 / 补充跳转到 web
@implementation RoomModeViewController
- (RoomModePresenter *)createPresenter {
return [[RoomModePresenter alloc] init];
}
- (BOOL)isHiddenNavBar {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.typeDataSource = @[
@{@"type":@(9), @"name":YMLocalizedString(@"1.0.33_text_10"), @"isUsed":@(YES)},
@{@"type":@(10), @"name":YMLocalizedString(@"1.0.33_text_11"), @"isUsed":@(NO)},
@{@"type":@(15), @"name":YMLocalizedString(@"1.0.33_text_12"), @"isUsed":@(NO)},
@{@"type":@(20), @"name":YMLocalizedString(@"1.0.33_text_13"), @"isUsed":@(NO)},
@{@"type":@(19), @"name":YMLocalizedString(@"1.0.33_text_14"), @"isUsed":@(NO)},
].mutableCopy;
self.micSkinDataSource = @[].mutableCopy;
self.micEffectDataSource = @[].mutableCopy;
[self setupUI];
[self loadRoomLevelInfo];
}
#pragma mark -
- (void)setupUI {
self.view.backgroundColor = [UIColor blackColor];
[self setupNavigationBar];
[self setupTableView];
[self setupUseButton];
}
- (void)setupNavigationBar {
UILabel *titleLabel = [self titleLabel];
UIButton *backButton = [self backButton];
UIButton *helpButton = [self helpButton];
[self.view addSubview:titleLabel];
[self.view addSubview:backButton];
[self.view addSubview:helpButton];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.view);
make.top.mas_equalTo(self.view).offset(kStatusBarHeight);
make.height.mas_equalTo(22);
}];
[backButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.view).offset(16);
make.centerY.mas_equalTo(titleLabel);
make.size.mas_equalTo(CGSizeMake(22, 22));
}];
[helpButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.mas_equalTo(self.view).offset(-16);
make.centerY.mas_equalTo(titleLabel);
make.width.height.mas_equalTo(44);
}];
}
- (void)setupTableView {
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view).offset(kNavigationHeight);
make.leading.trailing.mas_equalTo(self.view);
make.bottom.mas_equalTo(self.view).offset(-(kSafeAreaBottomHeight + 40));
}];
}
- (void)setupUseButton {
[self.view addSubview:self.useButton];
[self.useButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.view).offset(-kSafeAreaBottomHeight);
make.leading.trailing.mas_equalTo(self.view).inset(15);
make.height.mas_equalTo(40);
}];
}
#pragma mark -
- (void)didTapBack {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didTapHelp {
XPWebViewController *vc = [[XPWebViewController alloc] initWithRoomUID:@(self.roomInfo.uid).stringValue];
vc.url = URLWithType(KRoomLevelRule);
[self.navigationController pushViewController:vc animated:YES];
}
- (void)didTapUse {
[self updateRoomMode];
}
#pragma mark -
- (void)loadRoomLevelInfo {
self.updatedRoomType = self.roomInfo.type;
self.originalRoomType = self.roomInfo.type;
self.updatedMicSkinID = @(self.roomInfo.usedMicSkinId).stringValue;
self.updatedMicEffectID = @(self.roomInfo.usedMicEffectId).stringValue;
@kWeakify(self);
[self.presenter loadRoomLevelInfo:self.roomInfo.uid
success:^(RoomLevelInfoModel * _Nonnull model) {
@kStrongify(self);
self.levelInfo = model;
[self.tableView reloadData];
} failure:^(NSError * _Nonnull error) {
}];
}
- (void)updateRoomMode {
if (self.updatedRoomType < self.originalRoomType) {
@kWeakify(self);
[TTPopup alertWithMessage:YMLocalizedString(@"XPRoomTypeSelectionViewController3")
confirmHandler:^{
@kStrongify(self);
[self requestRoomUpdate];
} cancelHandler:^{}];
} else {
[self requestRoomUpdate];
}
}
- (void)requestRoomUpdate {
@kWeakify(self);
[self.presenter updateRoomMode:self.updatedRoomType
micSkinID:self.updatedMicSkinID.integerValue
micEffectID:self.updatedMicEffectID.integerValue
forRoom:self.roomInfo
success:^{
@kStrongify(self);
[self.navigationController popViewControllerAnimated:YES];
} failure:^(NSError * _Nonnull error) {
}];
}
#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return 0;
break;
case 1:
case 2:
case 3:
return 46;
break;
default:
return 0;
break;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *content = @"";
switch (section) {
case 1:
content = YMLocalizedString(@"1.0.33_text_2");
break;
case 2:
content = YMLocalizedString(@"1.0.33_text_3");
break;
case 3:
content = YMLocalizedString(@"1.0.33_text_4");
break;
case 0:
default:
content = @"";
break;
}
UILabel *label = [UILabel labelInitWithText:content
font:kFontMedium(16)
textColor:[UIColor whiteColor]];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(headerView);
make.leading.mas_equalTo(15);
make.height.mas_equalTo(22);
}];
return headerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return 1;
break;
case 1:
case 2:
case 3:
return 1;
break;
default:
return 0;
break;
}
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0:
return [RoomModeOwnerCard cellHeight];
break;
case 1:
return [RoomModeTypeCard cellHeight:4];
break;
case 2: {
if (self.levelInfo) {
return [RoomModeMicSkinCard cellHeight:self.levelInfo.micSkins.count];
} else {
return 10;
}
}
break;
case 3:
if (self.levelInfo) {
return [RoomModeMicEffectCard cellHeight:self.levelInfo.micEffects.count];
} else {
return 10;
}
break;
default:
return 0;
break;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0: {
RoomModeOwnerCard *cell = [RoomModeOwnerCard cellFro:tableView
indexPath:indexPath
roomInfo:self.roomInfo
levelInfo:self.levelInfo];
return cell;
}
break;
case 1: {
RoomModeTypeCard *cell = [RoomModeTypeCard cellFro:tableView
indexPath:indexPath
dataSource:self.typeDataSource.copy
currentType:self.roomInfo.type];
@kWeakify(self);
[cell setUpdateSelectedType:^(RoomType type) {
@kStrongify(self);
self.updatedRoomType = type;
}];
return cell;
}
break;
case 2: {
RoomModeMicSkinCard *cell = [RoomModeMicSkinCard cellFro:tableView
indexPath:indexPath
skins:self.levelInfo.micSkins
usedID:self.updatedMicSkinID.integerValue
roomLevel:self.levelInfo.currentLevel];
@kWeakify(self);
[cell setUpdateSelectedSkinID:^(NSString *skinID) {
@kStrongify(self);
self.updatedMicSkinID = skinID;
}];
return cell;
}
break;
case 3: {
RoomModeMicEffectCard *cell = [RoomModeMicEffectCard cellFro:tableView
indexPath:indexPath
effects:self.levelInfo.micEffects
usedID:self.updatedMicEffectID.integerValue
roomLevel:self.levelInfo.currentLevel];
@kWeakify(self);
[cell setUpdateSelectedEffectID:^(NSString *effectID) {
@kStrongify(self);
self.updatedMicEffectID = effectID;
}];
return cell;
}
break;
default:
return [UITableViewCell new];
break;
}
}
#pragma mark -
- (UIButton *)backButton {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setImage:[kImage(@"common_nav_back_white") ms_SetImageForRTL]
forState:UIControlStateNormal];
[b addTarget:self
action:@selector(didTapBack)
forControlEvents:UIControlEventTouchUpInside];
return b;
}
- (UILabel *)titleLabel {
UILabel *label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentCenter;
label.font = kFontMedium(17);
label.text = YMLocalizedString(@"1.0.33_text_1");
label.textColor = UIColorFromRGB(0xD9E7F7);
return label;
}
- (UIButton *)helpButton {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setImage:kImage(@"room_mode_help") forState:UIControlStateNormal];
[b addTarget:self action:@selector(didTapHelp) forControlEvents:UIControlEventTouchUpInside];
return b;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavigationHeight, KScreenWidth, KScreenHeight - kNavigationHeight) style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[RoomModeOwnerCard registerTo:_tableView];
[RoomModeTypeCard registerTo:_tableView];
[RoomModeMicSkinCard registerTo:_tableView];
[RoomModeMicEffectCard registerTo:_tableView];
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];
_tableView.tableFooterView = footer;
}
return _tableView;
}
- (UIButton *)useButton {
if (!_useButton) {
_useButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_useButton addGradientBackgroundWithColors:@[UIColorFromRGB(0xE29030), UIColorFromRGB(0xfcc074)]
startPoint:CGPointMake(0, 0.5)
endPoint:CGPointMake(1, 0.5)
cornerRadius:20];
[_useButton setTitle:YMLocalizedString(@"1.0.33_text_5") forState:UIControlStateNormal];
[_useButton addTarget:self action:@selector(didTapUse) forControlEvents:UIControlEventTouchUpInside];
}
return _useButton;
}
@end