Files
peko-ios/YuMi/Modules/YMRoom/View/LittleGame/View/XPLittleGameRoomListView.m
eggmanQQQ 2077815bac 处理需求 #64
1. /miniGame/record/miniGameList 补充参数 roomUid
2024-06-07 17:10:06 +08:00

175 lines
5.4 KiB
Objective-C

//
// XPRoomLittleGameListView.m
// xplan-ios
//
// Created by 冯硕 on 2022/1/22.
//
#import "XPLittleGameRoomListView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "Api+LittleGame.h"
///Model
#import "LittleGameInfoModel.h"
#import "XPLittleGameTableViewCell.h"
@interface XPLittleGameRoomListView ()<UITableViewDelegate, UITableViewDataSource>
///容器
@property (nonatomic,strong) UIStackView *stackView;
///列表
@property (nonatomic,strong) UITableView *tableView;
///原始数据源
@property (nonatomic,strong) NSArray *gameList;
///显示的数据源
@property (nonatomic,strong) NSArray *datasource;
///游戏信息
@property (nonatomic,strong) LittleGameInfoModel *gameInfo;
@end
@implementation XPLittleGameRoomListView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.layer.cornerRadius = 12.5;
self.layer.masksToBounds = YES;
self.layer.borderWidth = 0.5;
self.layer.borderColor = UIColorRGBAlpha(0xffffff, 0.6).CGColor;
self.backgroundColor = UIColorRGBAlpha(0x000000, 0.2);
[self addSubview:self.stackView];
[self.stackView addArrangedSubview:self.tableView];
}
- (void)initSubViewConstraints {
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(120);
make.bottom.mas_equalTo(self.stackView.mas_bottom);
}];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.top.mas_equalTo(self);
}];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(25);
}];
}
- (NSArray *)handleGameListData:(NSArray *)array {
NSMutableArray * dataArray= [NSMutableArray array];
for (int i = 0; i<array.count; i++) {
LittleGameInfoModel *info = [array safeObjectAtIndex1:i];
if ([info.mgId isEqualToString:self.mgId]) {
self.gameInfo = info;
} else {
if (info.mgId.length < 2 && self.mgId.length < 1) {
[dataArray insertObject:info atIndex:0];
} else {
[dataArray addObject:info];
}
}
}
return [dataArray copy];
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XPLittleGameTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPLittleGameTableViewCell class])];
if (cell == nil) {
cell = [[XPLittleGameTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPLittleGameTableViewCell class])];
}
cell.info = [self.datasource safeObjectAtIndex1:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 25;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.datasource.count > 0) {
self.tableView.hidden = YES;
LittleGameInfoModel *info = [self.datasource safeObjectAtIndex1:indexPath.row];
if (info.mgId.integerValue != self.mgId.integerValue) {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPRoomLittleGameListView:didSelectItem:)]) {
[self.delegate xPRoomLittleGameListView:self didSelectItem:info];
}
}
}
}
#pragma mark - Getters And Setters
- (void)setMgId:(NSString *)mgId {
_mgId = mgId;
if (self.gameList.count > 0) {
self.datasource = [self handleGameListData:self.gameList];
} else {
[Api getLittleGameList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
NSArray * array = [LittleGameInfoModel modelsWithArray:data.data];
LittleGameInfoModel * gameInfo = [[LittleGameInfoModel alloc] init];
gameInfo.mgId = @"0";
gameInfo.name = YMLocalizedString(@"XPLittleGameRoomListView0");
NSMutableArray * dataArray = [NSMutableArray array];
[dataArray addObjectsFromArray:array];
[dataArray addObject:gameInfo];
self.gameList = [dataArray copy];
self.datasource = [self handleGameListData:self.gameList];
}
}
roomUid:@""];
}
}
- (void)setDatasource:(NSArray *)datasource {
_datasource = datasource;
[self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(25 * self.datasource.count);
}];
[self.tableView reloadData];
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisVertical;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentFill;
_stackView.spacing = 0;
}
return _stackView;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [UIView new];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.hidden = YES;
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[_tableView registerClass:[XPLittleGameTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPLittleGameTableViewCell class])];
}
return _tableView;
}
@end