Files
peko-ios/YuMi/Modules/YMNewHome/View/XPHomePartyViewController.m

527 lines
19 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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 "XPBlankCollectionViewCell.h"
#import "XPBlankRoomModel.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]) {
NSCharacterSet *allowedCharacters = [NSCharacterSet URLFragmentAllowedCharacterSet];
NSString *encodedURLString = [model.bannerPic stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
[picArray addObject:encodedURLString];
}
}
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 = UIViewContentModeScaleToFill;
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) UICollectionView *collectionView;
///当前的页数
@property (nonatomic,assign) int page;
///没有新的数据了
@property (nonatomic,assign) BOOL hasNoMoreData;
///布局模式YES为两列NO为一列
@property (nonatomic,assign) BOOL isTwoColumnLayout;
@end
@implementation XPHomePartyViewController
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)isHiddenNavBar {
return YES;
}
- (XPHomePresenter *)createPresenter {
return [[XPHomePresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
[self setupLayoutNotification];
}
- (void)starBeginHeaderRefresh {
}
#pragma mark - Private Method
- (void)initSubViews {
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.collectionView];
}
- (void)setupLayoutNotification {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleLayoutToggle)
name:@"kHomeLayoutToggle"
object:nil];
}
- (void)handleLayoutToggle {
self.isTwoColumnLayout = !self.isTwoColumnLayout;
[self processCurrentDataSourceForLayout];
[self.collectionView reloadData];
}
- (void)processCurrentDataSourceForLayout {
if (self.datasource.count == 0) return;
if (self.isTwoColumnLayout) {
// 两列布局为奇数个非banner元素添加占位model
NSInteger nonBannerCount = 0;
for (id item in self.datasource) {
if (![item isKindOfClass:[NSArray class]] && ![item isKindOfClass:[XPBlankRoomModel class]]) {
nonBannerCount++;
}
}
// 如果非banner元素为奇数个添加占位model
if (nonBannerCount % 2 != 0) {
// 找到最后一个非banner元素的位置在其后插入占位model
NSInteger insertIndex = -1;
for (NSInteger i = self.datasource.count - 1; i >= 0; i--) {
id item = self.datasource[i];
if (![item isKindOfClass:[NSArray class]] && ![item isKindOfClass:[XPBlankRoomModel class]]) {
insertIndex = i + 1;
break;
}
}
if (insertIndex >= 0) {
[self.datasource insertObject:[[XPBlankRoomModel alloc] init] atIndex:insertIndex];
} else {
[self.datasource addObject:[[XPBlankRoomModel alloc] init]];
}
}
} else {
// 一列布局移除所有占位model
NSMutableArray *processedDataSource = [NSMutableArray array];
for (id item in self.datasource) {
if (![item isKindOfClass:[XPBlankRoomModel class]]) {
[processedDataSource addObject:item];
}
}
self.datasource = processedDataSource;
}
}
- (NSArray *)processDataSourceForLayout:(NSArray *)originalList {
if (originalList.count == 0) return originalList;
if (self.isTwoColumnLayout) {
// 两列布局为奇数个非banner元素添加占位model
NSMutableArray *processedDataSource = [NSMutableArray arrayWithArray:originalList];
NSInteger nonBannerCount = 0;
for (id item in originalList) {
if (![item isKindOfClass:[NSArray class]]) {
nonBannerCount++;
}
}
// 如果非banner元素为奇数个添加占位model
if (nonBannerCount % 2 != 0) {
[processedDataSource addObject:[[XPBlankRoomModel alloc] init]];
}
return processedDataSource;
} else {
// 一列布局移除所有占位model
NSMutableArray *processedDataSource = [NSMutableArray array];
for (id item in originalList) {
if (![item isKindOfClass:[XPBlankRoomModel class]]) {
[processedDataSource addObject:item];
}
}
return processedDataSource;
}
}
- (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{
[XNDJTDDLoadingTool showLoading];
[self.presenter loadSecondBanner];
}
}
-(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.datasource.count == 0){
XPGuildEmptyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGuildEmptyCollectionViewCell class]) forIndexPath:indexPath];
[cell setConstraints];
[cell setTitle:YMLocalizedString(@"XPGuildEmptyCollectionViewCell0")];
return cell;
}
id item = [self.datasource xpSafeObjectAtIndex:indexPath.row];
// 检查是否为占位 model
if ([item isKindOfClass:[XPBlankRoomModel class]]) {
XPBlankCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPBlankCollectionViewCell class]) forIndexPath:indexPath];
return cell;
}
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.datasource xpSafeObjectAtIndex:indexPath.row];
return cell;
}
}
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if(self.datasource.count == 0) {
return 1;
}
return self.datasource.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if(self.datasource.count == 0) {
return self.collectionView.frame.size;
}
id item = [self.datasource xpSafeObjectAtIndex:indexPath.row];
// 占位 model高度为0宽度为半宽
if ([item isKindOfClass:[XPBlankRoomModel class]]) {
return CGSizeMake(kGetScaleWidth(165), kGetScaleWidth(165));
}
// 计算尺寸:两列模式非 banner 为正方形半宽banner 始终全宽且固定高
BOOL isBanner = [item isKindOfClass:[NSArray class]];
if (isBanner) {
return CGSizeMake(kGetScaleWidth(345), kGetScaleWidth(105));
}
if (self.isTwoColumnLayout) {
CGFloat side = kGetScaleWidth(165);
return CGSizeMake(side, side);
}
return CGSizeMake(kGetScaleWidth(345), kGetScaleWidth(92));
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
id item = [self.datasource xpSafeObjectAtIndex:indexPath.row];
// 跳过占位 model 的点击
if ([item isKindOfClass:[XPBlankRoomModel class]]) {
return;
}
if ([item isKindOfClass:[HomePlayRoomModel class]]) {
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:[self processDataSourceForLayout:list]];
[self.collectionView reloadData];
}
- (void)getHomePersonalRoomListFail{
[[NSNotificationCenter defaultCenter] postNotificationName:@"khomeVCRefreshComplete" object:nil];
}
- (NSMutableArray *)insertBannerData:(NSArray *)firstPageList {
// 先处理数据布局在插入banner之前
NSArray *processedList = [self processDataSourceForLayout:firstPageList];
NSMutableArray *mutableArrayA = [processedList mutableCopy];
if (self.bannerInfoList.count > 0) {
NSInteger insertIndex = 5;
if (mutableArrayA.count < insertIndex) {
[mutableArrayA addObject:self.bannerInfoList];
} else {
[mutableArrayA insertObject:self.bannerInfoList atIndex:insertIndex];
}
}
return mutableArrayA;
}
- (void)getHomeRecommendRoomListSuccess:(NSArray *)list{
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 = [self insertBannerData:list];
}else{
NSArray *processedList = [self processDataSourceForLayout:list];
[self.datasource addObjectsFromArray:processedList];
}
[self.collectionView reloadData];
[[NSNotificationCenter defaultCenter]postNotificationName:@"khomeVCRefreshComplete" object:nil];
[self.collectionView.mj_footer endRefreshing];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[XNDJTDDLoadingTool hideHUD];
});
}
- (void)getHomeSecondBannerSuccess:(NSArray *)banners {
self.bannerInfoList = banners;
[self.presenter getRecommendRoomList:self.tagModel.id
page:self.page];
}
- (void)getHomeRecommendRoomListFail:(NSString *)message{
self.page--;
[self.collectionView.mj_footer endRefreshing];
[[NSNotificationCenter defaultCenter]postNotificationName:@"khomeVCRefreshComplete" object:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[XNDJTDDLoadingTool hideHUD];
});
}
- (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:[self processDataSourceForLayout:rooms]];
}else{
NSArray *processedList = [self processDataSourceForLayout:rooms];
[self.datasource addObjectsFromArray:processedList];
}
[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:[self processDataSourceForLayout:rooms]];
}else{
NSArray *processedList = [self processDataSourceForLayout:rooms];
[self.datasource addObjectsFromArray:processedList];
}
[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);
}
}
- (void)listDidAppear {
if (self.didAppear && self.tagModel) {
self.didAppear(self.tagModel);
}
}
#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(18);
layout.sectionInset = UIEdgeInsetsMake(20, 15, 0, 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 registerClass:[XPBlankCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPBlankCollectionViewCell class])];
}
return _collectionView;
}
- (NSMutableArray *)datasource {
if (!_datasource) {
_datasource = [NSMutableArray array];
}
return _datasource;
}
@end