// // XPHomeViewController.m // xplan-ios // // Created by 冯硕 on 2021/11/29. // #import "XPHomePartyViewController.h" ///Third #import #import ///Tool #import "XPMacro.h" #import "ThemeColor.h" ///Model #import "HomeRecommendRoomModel.h" ///View #import "XPHomeListCollectionViewCell.h" #import "XPHomeListEmptyCollectionViewCell.h" ///P #import "XPHomePresenter.h" #import "XPHomeProtocol.h" ///VC #import "XPRoomViewController.h" @interface XPHomePartyViewController () ///数据源 @property (nonatomic,strong) NSMutableArray *datasource; ///列表 @property (nonatomic,strong) UICollectionView *collectionView; ///当前的页数 @property (nonatomic,assign) int page; ///没有新的数据了 @property (nonatomic,assign) BOOL hasNoMoreData; @end @implementation XPHomePartyViewController - (BOOL)isHiddenNavBar { return YES; } - (XPHomePresenter *)createPresenter { return [[XPHomePresenter alloc] init]; } - (void)viewDidLoad { [super viewDidLoad]; [self initHeaderAndFooterRrfresh]; [self initSubViews]; [self initSubViewConstraints]; } #pragma mark - Private Method - (void)initSubViews { self.view.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.collectionView]; } - (void)initSubViewConstraints { [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); }]; } - (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 { self.page = 1; if (self.isAnchor) { [self.presenter getHomeMoreAnchorRoomList]; } else { [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; } self.page++; if (self.isAnchor) { [self.presenter getHomeMoreAnchorRoomList]; } else { [self.presenter getRecommendRoomList:self.tabId page:self.page pageSize:20 state:1]; } } #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 objectAtIndex: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 objectAtIndex:indexPath.item]; NSString * roomUid; if (self.isAnchor) { roomUid = model.uid; } else { roomUid = model.roomUid; } 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)getHomeMoreAnchorRoomListFail:(NSString *)messag { [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; } - (void)getHomeMoreAnchorRoomListSuccess:(NSArray *)list { [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; [self.datasource removeAllObjects]; [self.datasource addObjectsFromArray:list]; [self.collectionView reloadData]; } #pragma mark - JXCategoryListContentViewDelegate - (UIView *)listView { return self.view; } #pragma mark - Getters And Setters - (void)setIsAnchor:(BOOL)isAnchor { _isAnchor = isAnchor; if (_isAnchor) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self headerRefresh]; }); } } - (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])]; _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } return _collectionView; } - (NSMutableArray *)datasource { if (!_datasource) { _datasource = [NSMutableArray array]; } return _datasource; } @end