Files
peko-ios/YuMi/Modules/YMNewHome/View/XPHomeRecommendViewController.m
2023-07-18 18:26:10 +08:00

445 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.

//
// XPHomeRecommendViewController.m
// YuMi
//
// Created by YuMi on 2022/2/21.
//
#import "XPHomeRecommendViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <JXCategoryView/JXCategoryView.h>
#import <MJRefresh/MJRefresh.h>
#import <ReactiveObjC/ReactiveObjC.h>
///Tool
#import "YUMIMacroUitls.h"
#import "DJDKMIMOMColor.h"
#import "XPWeakTimer.h"
#import "ClientConfig.h"
#import "UIImage+Utils.h"
#import "Api+Room.h"
#import "Api+RoomSetting.h"
#import "AccountInfoStorage.h"
#import "TTPopup.h"
#import "NSArray+Safe.h"
///View
#import "XPNewHomePlayTableViewCell.h"
#import "XPNewHomeRecommendTableViewCell.h"
#import "XPNewHomePlayEmptyTableViewCell.h"
#import "XPHomeBannerTableViewCell.h"
#import "XPNewHomePartyTableViewCell.h"
#import "XPRoomViewController.h"
#import "XPWebViewController.h"
///Model
#import "HomePlayRoomModel.h"
#import "HomeBannerInfoModel.h"
#import "HomeRecommendRoomModel.h"
#import "ClanDetailInfoModel.h"
///P
#import "XPNewHomeRecommendPresenter.h"
#import "XPNewHomeRecommendProtocol.h"
UIKIT_EXTERN NSString * const kShieldingNotification;
@interface XPHomeRecommendViewController ()<UITableViewDelegate, UITableViewDataSource, XPNewHomePlayTableViewCellDelegate, XPHomeBannerTableViewCellDelegate, XPNewHomePlayEmptyTableViewCellDelegate,XPNewHomeRecommendTableViewCellDelegate,XPNewHomeRecommendProtocol>
{
NSTimer * timer;
}
///列表
@property (nonatomic,strong) UITableView *tableView;
///轮播图
@property (nonatomic,strong) NSArray<HomeBannerInfoModel *> *bannerList;
///推荐房
@property (nonatomic,strong) NSArray<HomeRecommendRoomModel *> *recommendList;
///组队开黑
@property (nonatomic,strong) NSArray<HomePlayRoomModel *> *playTeamList;
///个人房列表数据
@property (nonatomic,strong) NSArray<HomePlayRoomModel *> *personalRoomList;
///房间信息
@property (nonatomic,strong) RoomInfoModel * roomInfo;
///是否正在请示数据,防止过多请求
@property (nonatomic,assign) BOOL isRequestData;
@end
@implementation XPHomeRecommendViewController
- (XPNewHomeRecommendPresenter *)createPresenter {
return [[XPNewHomeRecommendPresenter alloc] init];
}
- (BOOL)isHiddenNavBar {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initHeaderAndFooterRrfresh];
[self addTimer];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (timer) {
[timer setFireDate:[NSDate distantPast]]; //很远的过去
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (timer) {
//关闭定时器
[timer setFireDate:[NSDate distantFuture]]; //很远的将来
}
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark - InitHttp
- (void)addTimer {
timer = [XPWeakTimer scheduledTimerWithTimeInterval:15 block:^(id userInfo) {
[self requestData];
} userInfo:nil repeats:YES];
}
-(void)requestData{
///加锁防止测试发现偶尔会出现闪退bug
@synchronized (self.presenter) {
if(self.isRequestData == YES)return;
self.isRequestData = YES;
///网络请求异步加载
dispatch_group_t group =dispatch_group_create();
// 并行队列
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
[self.presenter getHomeRecommendRoomListWithGroup:group];
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
[self.presenter getPlayGameWithTeam:1 withGroup:group];
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
[self.presenter getHomePersonalRoomListWithGroup:group];
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
[self.presenter getHomeTopBannerListWithGroup:group];
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
[self getRoomInfoWithGroup:group];
});
dispatch_group_notify(group,dispatch_get_main_queue(), ^{
self.isRequestData = NO;
[self.tableView.mj_header endRefreshing];
[self.tableView reloadData];
});
}
}
- (void)headerRefresh {
self.isRequestData = NO;
[self requestData];
}
-(void)getRoomInfoWithGroup:(dispatch_group_t)group{
NSString* roomUid = [AccountInfoStorage instance].getUid;
[Api getRoomInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
RoomInfoModel * roomInfo = [RoomInfoModel modelWithJSON:data.data];
self.roomInfo = roomInfo;
}
dispatch_group_leave(group);
} uid:roomUid intoUid:roomUid];
}
- (void)initHeaderAndFooterRrfresh {
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
header.stateLabel.font = [UIFont systemFontOfSize:10.0];
header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:10.0];
header.stateLabel.textColor = [DJDKMIMOMColor secondTextColor];
header.lastUpdatedTimeLabel.textColor = [DJDKMIMOMColor secondTextColor];
self.tableView.mj_header = header;
[self.tableView.mj_header beginRefreshing];
}
#pragma mark - Private Method
- (void)initSubViews {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shieldingNotification) name:kShieldingNotification object:nil];
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
}
-(void)shieldingNotification{
[self requestData];
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return self.recommendList.count > 0 ? 1 : 0;
} else if(section == 1) {
return self.bannerList.count > 0 ? 1 : 0;
} else if(section == 2) {
return self.recommendList != nil ? 1 : 0;
} else {
return self.personalRoomList.count > 0 ? self.personalRoomList.count : 0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return 160 * kScreenScale;
} else if(indexPath.section == 1) {
return 96;
} else if(indexPath.section == 2) {
return 65;
} else {
return 94;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
XPNewHomeRecommendTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPNewHomeRecommendTableViewCell class])];
if (cell == nil) {
cell = [[XPNewHomeRecommendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPNewHomeRecommendTableViewCell class])];
}
[cell setRecommendsList:self.recommendList];
cell.delegate = self;
return cell;
} else if(indexPath.section == 1) {
XPHomeBannerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPHomeBannerTableViewCell class])];
if (cell == nil) {
cell = [[XPHomeBannerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPHomeBannerTableViewCell class])];
}
cell.bannerList = self.bannerList;
cell.delegate = self;
return cell;
} else if(indexPath.section == 2) {
if (self.playTeamList.count > 0) {
XPNewHomePlayTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPNewHomePlayTableViewCell class])];
if (cell == nil) {
cell = [[XPNewHomePlayTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPNewHomePlayTableViewCell class])];
}
// cell.isClan = self.roomInfo.isPermitRoom == PermitRoomType_Licnese;
cell.isClan = YES;
cell.playRoomList = self.playTeamList;
cell.delegate = self;
return cell;
} else {
XPNewHomePlayEmptyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPNewHomePlayEmptyTableViewCell class])];
if (cell == nil) {
cell = [[XPNewHomePlayEmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPNewHomePlayEmptyTableViewCell class])];
}
// cell.isClan = self.roomInfo.isPermitRoom == PermitRoomType_Licnese;
cell.isClan = YES;
cell.delegate = self;
return cell;
}
} else {
XPNewHomePartyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPNewHomePartyTableViewCell class])];
if (cell == nil) {
cell = [[XPNewHomePartyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPNewHomePartyTableViewCell class])];
}
cell.roomInfo = [self.personalRoomList safeObjectAtIndex1:indexPath.row];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 0){return 5;}
if (section == 2) {
return 50;
}else if (section == 3){
return 50;
}
return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView * view = [[UIView alloc] init];
if(self.recommendList == nil && section == 2)return view;
if(self.personalRoomList == nil && section == 3)return view;
if (section == 2 || section == 3) {
view.frame = CGRectMake(0,0, KScreenWidth, 50);
UILabel * label = [[UILabel alloc] init];
label.textColor = [DJDKMIMOMColor mainTextColor];
label.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
if (section == 2) {
label.text = YMLocalizedString(@"XPHomeRecommendViewController0");
} else {
label.text = YMLocalizedString(@"XPHomeRecommendViewController1");
}
[view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(16);
make.left.mas_equalTo(view).offset(15);
make.height.mas_equalTo(22);
}];
}
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [UIView new];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.section == 3) {
if (self.personalRoomList.count > 0) {
HomePlayRoomModel * roomInfo1 = [self.personalRoomList safeObjectAtIndex1:indexPath.row];
if (roomInfo1.uid.length > 0) {
[XPRoomViewController openRoom:roomInfo1.uid viewController:self];
}
}
}
}
#pragma mark - XPNewHomeRecommendTableViewCellDelegate
- (void)xPNewHomeRecommendTableViewCell:(XPNewHomeRecommendTableViewCell *)view didSelectItem:(HomeRecommendRoomModel *)info {
if (info.roomUid.length > 0) {
[XPRoomViewController openRoom:info.roomUid viewController:self];
}
}
#pragma mark - XPNewHomePlayTableViewCellDelegate
- (void)xPNewHomePlayTableViewCell:(XPNewHomePlayTableViewCell *)cell didSelectItem:(HomePlayRoomModel *)info {
if (info.uid.length > 0) {
[XPRoomViewController openRoom:info.uid viewController:self];
}
}
- (void)chooseGameAction{
// XPHomeGameView *gameView = [XPHomeGameView new];
// gameView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
// gameView.playGameList = self.littleGameArray;
// gameView.delegate = self;
// [kWindow addSubview:gameView];
}
#pragma mark - XPNewHomePlayEmptyTableViewCellDelegate
- (void)emptyCellChooseGameAction{
// XPHomeGameView *gameView = [XPHomeGameView new];
// gameView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
// gameView.playGameList = self.littleGameArray;
// gameView.delegate = self;
// [kWindow addSubview:gameView];
}
#pragma mark - XPHomeBannerTableViewCell
- (void)xPHomeBannerTableViewCell:(XPHomeBannerTableViewCell *)view didClickBanner:(HomeBannerInfoModel *)info {
switch (info.skipType) {
case HomeBannerInfoSkipType_Room:
{
if (info.skipUri.length > 0) {
[XPRoomViewController openRoom:info.skipUri viewController:self];
}
}
break;
case HomeBannerInfoSkipType_Web:
{
XPWebViewController *vc = [[XPWebViewController alloc]init];
vc.url = info.skipUri;
[self.navigationController pushViewController:vc animated:YES];
}
break;
default:
break;
}
}
#pragma mark - XPHomeRecommendProtocol
- (void)getHomeTopBannerListSuccess:(NSArray *)list menuList:(NSArray *)memuList withGroup:(nonnull dispatch_group_t)group{
self.bannerList = list;
dispatch_group_leave(group);
}
///推荐
- (void)getHomeRecommendRoomListSuccess:(NSArray *)list withGroup:(nonnull dispatch_group_t)group{
self.recommendList= list;
dispatch_group_leave(group);
}
///房间派对
- (void)getHomePersonalRoomListSuccess:(NSArray *)list withGroup:(nonnull dispatch_group_t)group {
NSMutableArray * array = [NSMutableArray array];
for (HomePlayRoomModel * info in list) {
if(info.isBanner) {
self.bannerList = info.bannerVoList;
} else {
[array addObject:info];
}
}
self.personalRoomList = array;
dispatch_group_leave(group);
}
///扩列交友
- (void)getPlayGameWithTeamSuccess:(NSArray *)list withGroup:(nonnull dispatch_group_t)group{
NSMutableArray * array = [NSMutableArray array];
for (HomePlayRoomModel * info in list) {
if(info.isBanner) {
self.bannerList = info.bannerVoList;
} else {
[array addObject:info];
}
}
self.playTeamList = array;
dispatch_group_leave(group);
}
- (void)getHomeRecommendDataFailWithGroup:(dispatch_group_t)group {
dispatch_group_leave(group);
}
#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
return self.view;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
_tableView.tableFooterView = [UIView new];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[_tableView registerClass:[XPNewHomeRecommendTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPNewHomeRecommendTableViewCell class])];
[_tableView registerClass:[XPNewHomePlayTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPNewHomePlayTableViewCell class])];
[_tableView registerClass:[XPNewHomePlayEmptyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPNewHomePlayEmptyTableViewCell class])];
[_tableView registerClass:[XPHomeBannerTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPHomeBannerTableViewCell class])];
[_tableView registerClass:[XPNewHomePartyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPNewHomePartyTableViewCell class])];
}
return _tableView;
}
@end