Files
yinmeng-ios/xplan-ios/Main/Home/View/XPHomeLivePageViewController.m
2022-11-11 17:46:37 +08:00

216 lines
6.8 KiB
Objective-C

//
// XPHomeLivePageViewController.m
// xplan-ios
//
// Created by GreenLand on 2022/7/18.
//
#import "XPHomeLivePageViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <MJRefresh/MJRefresh.h>
///Tool
#import "ThemeColor.h"
#import "XPMacro.h"
#import "XCHUDTool.h"
#import "NSArray+Safe.h"
///View
#import "XPHomeHapppyRoomTableViewCell.h"
#import "XPHomeBannerTableViewCell.h"
#import "XPHomeListEmptyTableViewCell.h"
#import "XPHomeListCollectionViewCell.h"
#import "XPHomeListEmptyCollectionViewCell.h"
///Model
#import "HomePlayRoomModel.h"
#import "HomeRecommendRoomModel.h"
///VC
#import "XPWebViewController.h"
#import "XPRoomViewController.h"
#import "XPHomeLivePresenter.h"
#import "XPHomeLiveProtocol.h"
@interface XPHomeLivePageViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, XPHomeLiveProtocol>
///列表
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
///没有新的数据了
@property (nonatomic,assign) BOOL hasNoMoreData;
///数据源
@property (nonatomic,strong) NSMutableArray *datasource;
@property (nonatomic, assign) BOOL hadLoad;
@end
@implementation XPHomeLivePageViewController
- (BOOL)isHiddenNavBar {
return YES;
}
- (XPHomeLivePresenter *)createPresenter {
return [[XPHomeLivePresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
}
#pragma mark - Private Method
- (void)initSubViews {
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.collectionView];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!self.hadLoad) {
self.hadLoad = YES;
[XCHUDTool showAnchorLoading];
}
[self headerRefresh];
}
- (void)initSubViewConstraints {
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
}
#pragma mark - 刷新的fangfa
- (void)headerRefresh {
[self.presenter getHomeMoreAnchorRoomListWithTabId:self.tabId];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.datasource.count > 0 ? self.datasource.count : 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.datasource.count > 0) {
XPHomeListCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeListCollectionViewCell class]) forIndexPath:indexPath];
HomeRecommendRoomModel * model = [self.datasource safeObjectAtIndex1:indexPath.item];
cell.roomModel = model;
return cell;
}
XPHomeListEmptyCollectionViewCell * emptyCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeListEmptyCollectionViewCell class]) forIndexPath:indexPath];
return emptyCell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if (self.datasource.count > 0) {
HomeRecommendRoomModel * model = [self.datasource safeObjectAtIndex1:indexPath.item];
NSString *roomUid = model.uid;
if (roomUid.length > 0) {
[XPRoomViewController openRoom:roomUid viewController:self];
}
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.datasource.count <= 0) {
return CGSizeMake(KScreenWidth, KScreenHeight- kNavigationHeight - 49 - 20);
}
return CGSizeMake((KScreenWidth - 15 * 3) / 2, 157 + 36);
}
#pragma mark - XPHomeProtocol
- (void)getHomeRecommendRoomListSuccess:(NSArray *)list state:(BOOL)state {
if (state == 0) {
self.hasNoMoreData = NO;
[self.datasource removeAllObjects];
[self.collectionView.mj_header endRefreshing];
} else {
[self.collectionView.mj_footer endRefreshing];
}
if (list.count > 0) {
self.hasNoMoreData = NO;
[self.datasource addObjectsFromArray:list];
} else {
self.hasNoMoreData = YES;
}
[self.collectionView reloadData];
}
- (void)getHomeRecommendRoomListFail:(NSString *)message state:(BOOL)state {
if (state ==0) {
[self.collectionView.mj_header endRefreshing];
} else {
[self.collectionView.mj_footer endRefreshing];
}
}
///获取个播房列表成功
- (void)getAnchorRoomListSuccess:(NSArray<HomeRecommendRoomModel *> *)array {
[self.datasource removeAllObjects];
[self.datasource addObjectsFromArray:array];
[self.collectionView reloadData];
[XCHUDTool hideHUD];
}
///获取个播房失败
- (void)getAnchorRoomListFail {
[XCHUDTool hideHUD];
}
- (void)getHomeMoreAnchorRoomListFail:(NSString *)messag {
[self.collectionView.mj_header endRefreshing];
[self.collectionView.mj_footer endRefreshing];
}
#pragma mark - JXPagingViewListViewDelegate
- (UIView *)listView {
return self.view;
}
- (UIScrollView *)listScrollView {
return self.collectionView;
}
- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
self.scrollCallback = callback;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.scrollCallback(scrollView);
}
#pragma mark - Getters And Setters
- (void)setTabId:(NSString *)tabId {
_tabId = tabId;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [self headerRefresh];
});
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 15;
layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor clearColor];
[_collectionView registerClass:[XPHomeListCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeListCollectionViewCell class])];
[_collectionView registerClass:[XPHomeListEmptyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeListEmptyCollectionViewCell class])];
}
return _collectionView;
}
- (NSMutableArray *)datasource {
if (!_datasource) {
_datasource = [NSMutableArray array];
}
return _datasource;
}
@end