Files
peko-ios/YuMi/Modules/YMNewHome/View/XPHomePartyViewController.m
2024-11-12 16:47:29 +08:00

383 lines
13 KiB
Objective-C

//
// XPHomeViewController.m
// YuMi
//
// Created by YuMi on 2021/11/29.
//
#import "XPHomePartyViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <MJRefresh/MJRefresh.h>
///Tool
#import "YUMIMacroUitls.h"
#import "DJDKMIMOMColor.h"
#import "NSArray+Safe.h"
///Model
#import "HomeRecommendRoomModel.h"
///View
#import "XPNewHomePartyCollectionViewCell.h"
#import "XPGuildEmptyCollectionViewCell.h"
#import "XPWeakTimer.h"
///P
#import "XPHomePresenter.h"
#import "XPHomeProtocol.h"
///VC
#import "XPRoomViewController.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "HomeBannerInfoModel.h"
@interface HomePartyBannerCell : UICollectionViewCell <SDCycleScrollViewDelegate>
@property (nonatomic, strong) SDCycleScrollView *bannerView;
@property (nonatomic, copy) NSArray<HomeBannerInfoModel *> *bannerInfoList;
@property (nonatomic, copy) void(^didTapBannerItem)(HomeBannerInfoModel *model);
@end
@implementation HomePartyBannerCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self.contentView addSubview:self.bannerView];
[self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.contentView);
}];
}
return self;
}
- (void)setBannerInfoList:(NSArray<HomeBannerInfoModel *> *)bannerInfoList {
_bannerInfoList = bannerInfoList;
NSMutableArray *picArray = @[].mutableCopy;
for (HomeBannerInfoModel *model in bannerInfoList) {
if (![NSString isEmpty:model.bannerPic]) {
[picArray addObject:model.bannerPic];
}
}
self.bannerView.imageURLStringsGroup = picArray.copy;
self.bannerView.autoScroll = YES;
}
- (SDCycleScrollView *)bannerView {
if (!_bannerView) {
_bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero
delegate:self
placeholderImage:[UIImageConstant defaultBannerPlaceholder]];
_bannerView.backgroundColor = [UIColor clearColor];
_bannerView.layer.cornerRadius = 12;
_bannerView.layer.masksToBounds = YES;
_bannerView.showPageControl = YES;
_bannerView.autoScrollTimeInterval = 5.0;
_bannerView.pageControlDotSize = CGSizeMake(3, 3);
_bannerView.pageDotColor = [UIColor lightGrayColor];
_bannerView.currentPageDotColor = [UIColor darkGrayColor];
_bannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
if (isMSRTL()) {
for (UIView *subView in _bannerView.subviews) {
subView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
}
}
}
return _bannerView;
}
#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
HomeBannerInfoModel * bannerInfo = [self.bannerInfoList xpSafeObjectAtIndex:index];
if (bannerInfo && self.didTapBannerItem) {
self.didTapBannerItem(bannerInfo);
}
}
@end
@interface XPHomePartyViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, XPHomeProtocol>
///数据源
@property (nonatomic,strong) NSMutableArray *datasource;
@property (nonatomic,strong) NSMutableArray *displayDatasource;
///列表
@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 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.top.mas_equalTo(0);
make.leading.trailing.bottom.equalTo(self.view);
}];
MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
footer.stateLabel.textColor = [DJDKMIMOMColor secondTextColor];
footer.stateLabel.font = [UIFont systemFontOfSize:10.0];
self.collectionView.mj_footer = footer;
}
#pragma mark - 刷新的方法
- (void)headerRefresh {
self.page = 1;
if ([self.tagModel.id isEqualToString:@"-11"]) {
[self.presenter getMyCollectRooms:self.page];
} else if ([self.tagModel.id isEqualToString:@"-12"]) {
[self.presenter getMyRecentRooms:self.page];
} else if([self.tagModel.id isEqualToString:@"-1"]){
[self.presenter getHomePersonalRoomList];
} else{
[self.presenter getRecommendRoomList:self.tagModel.id
page:self.page];
}
}
-(void)footerRefresh{
self.page++;
if ([self.tagModel.id isEqualToString:@"-11"]) {
[self.presenter getMyCollectRooms:self.page];
} else if ([self.tagModel.id isEqualToString:@"-12"]) {
[self.presenter getMyRecentRooms:self.page];
} else if([self.tagModel.id isEqualToString:@"-1"]){
[self.collectionView.mj_footer endRefreshing];
}else{
[self.presenter getRecommendRoomList:self.tagModel.id page:self.page];
}
}
#pragma mark - UICollectionViewDelegate And UICollectionViewDataSource
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if(self.displayDatasource.count == 0){
XPGuildEmptyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGuildEmptyCollectionViewCell class]) forIndexPath:indexPath];
[cell setConstraints];
[cell setTitle:YMLocalizedString(@"XPGuildEmptyCollectionViewCell0")];
return cell;
}
id item = [self.displayDatasource xpSafeObjectAtIndex:indexPath.row];
if ([item isKindOfClass:[NSArray class]]) {
HomePartyBannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomePartyBannerCell class])
forIndexPath:indexPath];
cell.bannerInfoList = item;
@kWeakify(self);
[cell setDidTapBannerItem:^(HomeBannerInfoModel *model) {
@kStrongify(self);
if (self.didTapBannerItem) {
self.didTapBannerItem(model);
}
}];
return cell;
} else {
XPNewHomePartyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPNewHomePartyCollectionViewCell class]) forIndexPath:indexPath];
cell.roomInfo = [self.displayDatasource xpSafeObjectAtIndex:indexPath.row];
return cell;
}
}
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if(self.displayDatasource.count == 0) {
return 1;
}
return self.displayDatasource.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if(self.displayDatasource.count == 0) {
return self.collectionView.frame.size;
}
id item = [self.displayDatasource xpSafeObjectAtIndex:indexPath.row];
if ([item isKindOfClass:[NSArray class]]) {
return CGSizeMake(kGetScaleWidth(345), kGetScaleWidth(118));
} else {
return CGSizeMake(kGetScaleWidth(345), kGetScaleWidth(92));
}
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
id item = [self.displayDatasource xpSafeObjectAtIndex:indexPath.row];
if ([item isKindOfClass:[NSArray class]]) {
} else {
HomePlayRoomModel * roomInfo = (HomePlayRoomModel *)item;
if (roomInfo.uid.length > 0) {
[XPRoomViewController openRoom:roomInfo.uid viewController:self];
}
}
}
#pragma mark - XPHomeProtocol
- (void)getHomePersonalRoomListSuccess:(NSArray *)list{
[[NSNotificationCenter defaultCenter] postNotificationName:@"khomeVCRefreshComplete" object:nil];
for (HomePlayRoomModel *model in list) {
model.width = [UILabel getWidthWithText:@(model.onlineNum).stringValue height:kGetScaleWidth(12) font:kFontBold(10)]+1;
}
self.datasource = [NSMutableArray arrayWithArray:list];
[self.collectionView reloadData];
}
- (void)getHomePersonalRoomListFail{
[[NSNotificationCenter defaultCenter] postNotificationName:@"khomeVCRefreshComplete" object:nil];
}
- (void)sortDataSource {
NSMutableArray *mutableArrayA = [self.datasource mutableCopy];
NSInteger insertIndex = 5;
if (self.bannerInfoList.count > 0) {
if (mutableArrayA.count < insertIndex) {
[mutableArrayA addObject:self.bannerInfoList];
} else {
[mutableArrayA insertObject:self.bannerInfoList atIndex:insertIndex];
}
}
self.displayDatasource = mutableArrayA.mutableCopy;
[self.collectionView reloadData];
}
- (void)getHomeRecommendRoomListSuccess:(NSArray *)list{
[self.collectionView.mj_footer endRefreshing];
for (HomePlayRoomModel *model in list) {
model.width = [UILabel getWidthWithText:@(model.onlineNum).stringValue height:kGetScaleWidth(12) font:kFontBold(10)]+1;
}
if(self.page == 1){
self.datasource = [NSMutableArray arrayWithArray:list];
[self sortDataSource];
}else{
[self.datasource addObjectsFromArray:list];
}
[[NSNotificationCenter defaultCenter]postNotificationName:@"khomeVCRefreshComplete" object:nil];
}
- (void)getHomeSecondBannerSuccess:(NSArray *)banners {
self.bannerInfoList = banners;
[self sortDataSource];
}
- (void)getHomeRecommendRoomListFail:(NSString *)message{
[self.collectionView.mj_footer endRefreshing];
[[NSNotificationCenter defaultCenter]postNotificationName:@"khomeVCRefreshComplete" object:nil];
}
- (void)getMineCollectRoomsSuccess:(NSArray *)rooms {
[[NSNotificationCenter defaultCenter]postNotificationName:@"khomeVCRefreshComplete" object:nil];
[self.collectionView.mj_footer endRefreshing];
for (HomePlayRoomModel *model in rooms) {
model.width = [UILabel getWidthWithText:@(model.onlineNum).stringValue
height:kGetScaleWidth(12)
font:kFontBold(10)] + 1;
}
if(self.page == 1){
self.datasource = [NSMutableArray arrayWithArray:rooms];
}else{
[self.datasource addObjectsFromArray:rooms];
}
[self.collectionView reloadData];
}
- (void)getMineRecentRoomsSuccess:(NSArray *)rooms {
[[NSNotificationCenter defaultCenter]postNotificationName:@"khomeVCRefreshComplete" object:nil];
[self.collectionView.mj_footer endRefreshing];
for (HomePlayRoomModel *model in rooms) {
model.width = [UILabel getWidthWithText:@(model.onlineNum).stringValue
height:kGetScaleWidth(12)
font:kFontBold(10)] + 1;
}
if(self.page == 1){
self.datasource = [NSMutableArray arrayWithArray:rooms];
}else{
[self.datasource addObjectsFromArray:rooms];
}
[self.collectionView reloadData];
}
#pragma mark - JXPagingViewListViewDelegate
- (UIScrollView *)listScrollView {
return self.collectionView;
}
- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
self.scrollCallback = callback;
}
- (UIView *)listView {
return self.view;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if(self.scrollCallback){
self.scrollCallback(scrollView);
}
}
#pragma mark - Getters And Setters
-(void)setTagModel:(PIHomeCategoryTitleModel *)tagModel{
_tagModel = tagModel;
@kWeakify(self);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@kStrongify(self);
[self headerRefresh];
});
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout alloc] init];
layout.minimumInteritemSpacing = kGetScaleWidth(0);
layout.minimumLineSpacing = kGetScaleWidth(10);
layout.sectionInset = UIEdgeInsetsMake(0, kGetScaleWidth(15), 0, kGetScaleWidth(15));
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.tag = 78574;
[_collectionView registerClass:[XPNewHomePartyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPNewHomePartyCollectionViewCell class])];
[_collectionView registerClass:[XPGuildEmptyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGuildEmptyCollectionViewCell class])];
[_collectionView registerClass:[HomePartyBannerCell class] forCellWithReuseIdentifier:NSStringFromClass([HomePartyBannerCell class])];
_collectionView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
}
return _collectionView;
}
- (NSMutableArray *)datasource {
if (!_datasource) {
_datasource = [NSMutableArray array];
}
return _datasource;
}
@end