// // XPPartyListViewController.m // xplan-ios // // Created by XY on 2023/3/7. // #import "XPPartyListViewController.h" ///Third #import #import ///Tool #import "XPMacro.h" #import "XCHUDTool.h" #import "NSArray+Safe.h" #import "ThemeColor.h" ///View #import "XPPartyListCollectionViewCell.h" #import "XPHomeListEmptyCollectionViewCell.h" ///P #import "XPHomePresenter.h" #import "XPHomeProtocol.h" /// VC #import "XPRoomViewController.h" /// Model #import "HomeRecommendRoomModel.h" @interface XPPartyListViewController () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView); /// 数据源 @property (nonatomic, strong) NSMutableArray *dataSource; ///当前的页数 @property (nonatomic,assign) int page; /// 没有新的数据了 @property (nonatomic,assign) BOOL hasNoMoreData; @property (nonatomic, assign) BOOL hadLoad; @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]; if (!self.hadLoad) { self.hadLoad = YES; [XCHUDTool showAnchorLoading]; } [self headerRefresh]; } - (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 = [ThemeColor secondTextColor]; header.lastUpdatedTimeLabel.textColor = [ThemeColor secondTextColor]; self.collectionView.mj_header = header; 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; } #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]; } } - (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]; } } #pragma mark - Private Method - (void)initSubViews { [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(8); }]; } #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]; [XCHUDTool hideHUD]; } - (void)getHomeRecommendRoomListFail:(NSString *)message state:(BOOL)state { if (state == 0) { [self.collectionView.mj_header endRefreshing]; } 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]; [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; [XCHUDTool hideHUD]; } - (void)getPartyRecommendDataFail { [self.collectionView.mj_footer endRefreshing]; [XCHUDTool hideHUD]; } #pragma mark - UICollectionViewDelegate - (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) { 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.dataSource.count > 0) { HomeRecommendRoomModel * model = [self.dataSource safeObjectAtIndex1:indexPath.item]; NSString *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; layout.itemSize = CGSizeMake(KScreenWidth, 102); layout.minimumLineSpacing = 12; layout.minimumInteritemSpacing = 12; _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])]; } return _collectionView; } - (NSMutableArray *)dataSource { if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } @end