Files
yinmeng-ios/xplan-ios/Main/Home/View/XPPartyListViewController.m
2023-10-10 12:11:37 +08:00

364 lines
14 KiB
Objective-C

//
// XPPartyListViewController.m
// xplan-ios
//
// Created by XY on 2023/3/7.
//
#import "XPPartyListViewController.h"
///Third
#import <Masonry.h>
#import <MJRefresh.h>
///Tool
#import "XPMacro.h"
#import "XCHUDTool.h"
#import "NSArray+Safe.h"
#import "ThemeColor.h"
///View
#import "XPPartyListCollectionViewCell.h"
#import "XPNewPartyListCollectionViewCell.h"
#import "XPHomeListEmptyCollectionViewCell.h"
///P
#import "XPHomePresenter.h"
#import "XPHomeProtocol.h"
/// VC
#import "XPRoomViewController.h"
/// Model
#import "HomeRecommendRoomModel.h"
@interface XPPartyListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, XPHomeProtocol>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
/// 数据源
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *personList;
///当前的页数
@property (nonatomic,assign) int page;
/// 没有新的数据了
@property (nonatomic,assign) BOOL hasNoMoreData;
@end
@implementation XPPartyListViewController
- (XPHomePresenter *)createPresenter {
return [[XPHomePresenter alloc] init];
}
- (BOOL)isHiddenNavBar {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
[self initHeaderAndFooterRrfresh];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)initHeaderAndFooterRrfresh {
MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
footer.stateLabel.textColor = [ThemeColor secondTextColor];
footer.stateLabel.font = [UIFont systemFontOfSize:10.0];
self.collectionView.mj_footer = footer;
[self headerRefresh];
}
#pragma mark - 刷新的fangfa
- (void)headerRefresh {
if ([self.tabId isEqualToString:@"recommend"]) {
[self.presenter getPartyRecommendRoomList];
}else{
self.page = 1;
[self.presenter getRecommendRoomList:self.tabId page:self.page pageSize:20 state:0];
}
if ([[AccountInfoStorage instance] getUid].length > 0 && [self.tabId isEqualToString:@"recommend"]) {
[self.presenter getHomePersonalRoomListWithUid:[AccountInfoStorage instance].getUid];
}
}
- (void)footerRefresh {
if (self.hasNoMoreData) {
[self showErrorToast:@"没有更多房间了"];
[self.collectionView.mj_footer endRefreshing];
return;
}
if ([self.tabId isEqualToString:@"recommend"]) {
}else{
self.page++;
[self.presenter getRecommendRoomList:self.tabId page:self.page pageSize:20 state:1];
}
if ([[AccountInfoStorage instance] getUid].length > 0 && [self.tabId isEqualToString:@"recommend"]) {
[self.presenter getHomePersonalRoomListWithUid:[AccountInfoStorage instance].getUid];
}
}
#pragma mark - Private Method
- (void)initSubViews {
self.view.backgroundColor = UIColor.whiteColor;
[self.view addSubview:self.collectionView];
}
- (void)initSubViewConstraints {
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.mas_equalTo(self.view);
make.top.mas_equalTo(6);
}];
}
#pragma mark - XPHomeProtocol
- (void)getHomeRecommendRoomListSuccess:(NSArray *)list state:(BOOL)state {
if (state == 0) {
self.hasNoMoreData = NO;
[self.dataSource removeAllObjects];
if(self.delegate && [self.delegate respondsToSelector:@selector(headerRefreshComplete)]){
[self.delegate headerRefreshComplete];
}
} else {
[self.collectionView.mj_footer endRefreshing];
}
if (list.count > 0) {
self.hasNoMoreData = NO;
[self.dataSource addObjectsFromArray:list];
} else {
self.hasNoMoreData = YES;
}
[self.collectionView reloadData];
[XCHUDTool hideHUD];
}
- (void)getHomePersonalRoomListSuccess:(NSArray *)list{
self.personList = [[NSMutableArray alloc]initWithArray:list];
[self.collectionView reloadData];
}
- (void)getHomeRecommendRoomListFail:(NSString *)message state:(BOOL)state {
if (state == 0) {
if(self.delegate && [self.delegate respondsToSelector:@selector(headerRefreshComplete)]){
[self.delegate headerRefreshComplete];
}
} else {
[self.collectionView.mj_footer endRefreshing];
}
[XCHUDTool hideHUD];
}
- (void)getPartyRecommendRoomListSuccess:(NSArray *)list {
self.hasNoMoreData = YES;
[self.dataSource removeAllObjects];
[self.dataSource addObjectsFromArray:list];
[self.collectionView reloadData];
if(self.delegate && [self.delegate respondsToSelector:@selector(headerRefreshComplete)]){
[self.delegate headerRefreshComplete];
}
[self.collectionView.mj_footer endRefreshing];
[XCHUDTool hideHUD];
}
- (void)getPartyRecommendDataFail {
[self.collectionView.mj_footer endRefreshing];
[XCHUDTool hideHUD];
}
#pragma mark - UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
if([self.tabId isEqualToString:@"recommend"]){
return 2;
}
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if([self.tabId isEqualToString:@"recommend"]){
if(section == 1)return self.personList.count;
return self.dataSource.count;
}
return self.dataSource.count > 0 ? self.dataSource.count : 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if([self.tabId isEqualToString:@"recommend"]){
return indexPath.section == 0 ? CGSizeMake(kGetScaleWidth(168), kGetScaleWidth(148)) : CGSizeMake(KScreenWidth, 102);
}
return CGSizeMake(KScreenWidth, 102);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
if([self.tabId isEqualToString:@"recommend"]){
return section == 0 ? UIEdgeInsetsMake(kGetScaleWidth(8), kGetScaleWidth(15), 0, kGetScaleWidth(15)):UIEdgeInsetsMake(0, 0, 0, 0);
}
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
if([self.tabId isEqualToString:@"recommend"]){
return section == 0 ? kGetScaleWidth(7) : 12;
}
return 12 ;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
if([self.tabId isEqualToString:@"recommend"]){
return section == 0 ? kGetScaleWidth(7) : 12;
}
return 12 ;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
if([self.tabId isEqualToString:@"recommend"]){
return section == 0 ? CGSizeMake(0, 0): CGSizeMake(KScreenWidth, kGetScaleWidth(40));
}
return CGSizeMake(0, 0);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if([self.tabId isEqualToString:@"recommend"] && [kind isEqualToString:UICollectionElementKindSectionHeader]){
UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
if(headView.subviews.count == 0){
UILabel *titelView = [UILabel labelInitWithText:@"更多房间" font:kFontMedium(16) textColor:UIColorFromRGB(0x2B2D33)];
[headView addSubview:titelView];
[titelView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(kGetScaleWidth(28));
make.centerY.equalTo(headView);
}];
}
return headView;
}
return nil;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if([self.tabId isEqualToString:@"recommend"]){
if(indexPath.section == 0){
XPNewPartyListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPNewPartyListCollectionViewCell class]) forIndexPath:indexPath];
HomeRecommendRoomModel *model = [self.dataSource safeObjectAtIndex1:indexPath.row];
cell.roomModel = model;
return cell;
}
XPPartyListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPPartyListCollectionViewCell class]) forIndexPath:indexPath];
HomeRecommendRoomModel *model = [self.personList safeObjectAtIndex1:indexPath.row];
cell.roomModel = model;
NSInteger index = (indexPath.row+1) % 3;
if (index == 1) {
cell.backView.image = [UIImage imageNamed:@"home_party_bg_1"];
}else if (index == 2) {
cell.backView.image = [UIImage imageNamed:@"home_party_bg_2"];
}else{
cell.backView.image = [UIImage imageNamed:@"home_party_bg_3"];
}
return cell;
}
if (self.dataSource.count > 0) {
XPPartyListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPPartyListCollectionViewCell class]) forIndexPath:indexPath];
HomeRecommendRoomModel *model = [self.dataSource safeObjectAtIndex1:indexPath.row];
cell.roomModel = model;
NSInteger index = (indexPath.row+1) % 3;
if (index == 1) {
cell.backView.image = [UIImage imageNamed:@"home_party_bg_1"];
}else if (index == 2) {
cell.backView.image = [UIImage imageNamed:@"home_party_bg_2"];
}else{
cell.backView.image = [UIImage imageNamed:@"home_party_bg_3"];
}
return cell;
}
XPHomeListEmptyCollectionViewCell * emptyCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeListEmptyCollectionViewCell class]) forIndexPath:indexPath];
return emptyCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if([self.tabId isEqualToString:@"recommend"]){
NSString *roomUid = @"";
if(indexPath.section == 0){
if (self.dataSource.count > 0) {
HomeRecommendRoomModel * model = [self.dataSource safeObjectAtIndex1:indexPath.item];
roomUid = model.roomUid;
}
}else{
if (self.personList.count > 0) {
HomeRecommendRoomModel * model = [self.personList safeObjectAtIndex1:indexPath.item];
roomUid = model.uid;
}
}
if (roomUid.length > 0) {
[XPRoomViewController openRoom:roomUid viewController:self];
}
return;
}
NSString *roomUid = @"";
if (self.dataSource.count > 0) {
HomeRecommendRoomModel * model = [self.dataSource safeObjectAtIndex1:indexPath.item];
roomUid = model.roomUid;
}
if (roomUid.length > 0) {
[XPRoomViewController openRoom:roomUid viewController:self];
}
}
#pragma mark - JXCategoryListContentViewDelegate
- (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
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = UIColor.clearColor;
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerClass:[XPPartyListCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPPartyListCollectionViewCell class])];
[_collectionView registerClass:[XPHomeListEmptyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeListEmptyCollectionViewCell class])];
[_collectionView registerClass:[XPNewPartyListCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPNewPartyListCollectionViewCell class])];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
}
return _collectionView;
}
- (NSMutableArray *)dataSource {
if (!_dataSource) {
_dataSource = [NSMutableArray array];
}
return _dataSource;
}
@end