Files
yinmeng-ios-store/yinmeng-ios/DingDangApp/V2Versions/Home/DDV2HomeVC.m
2023-12-13 19:18:00 -08:00

287 lines
11 KiB
Objective-C

//
// DDV2HomeVC.m
// DingDangApp
//
// Created by apple on 2023/7/1.
//
#import "DDV2HomeVC.h"
#import "DDRoomPartyModel.h"
#import "DDV2HomeVerticalCell.h"
#import "DDV2HomeVerticalHeadView.h"
#import "Api+DDHomeApi.h"
#import "AccountInfoStorage.h"
@interface DDV2HomeVC () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic,assign) BOOL isClick;
@property (nonatomic, strong) NSMutableArray *noDataArr;
@property (nonatomic, assign) BOOL isNoCollection;
@end
@implementation DDV2HomeVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initWithSubView];
self.pageNum = 1;
[self requestData];
}
- (void)initWithSubView {
self.view.backgroundColor = DDHEXColor(0xF6F6F6);
[self.view addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.equalTo(self.view).inset(0);
make.bottom.mas_equalTo(-KTABBAR_H);
}];
}
- (void)requestData {
WeakSelf(ws)
if ([self.type isEqualToString:@"hot"]){
NSString * uid = [AccountInfoStorage instance].getUid;
[Api homeRecommendRoomList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
[ws endrefresh];
if(code == 200){
NSArray<DDRoomPartyModel *> * array = [DDRoomPartyModel DD_ModelsWithArray:data.data];
[ws.dataArr removeAllObjects];
[ws.dataArr addObjectsFromArray:array];
[ws.collectionView reloadData];
}
} uid:uid] ;
return;
}
return;
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@(20) forKey:@"current_page"];
[dic setObject:@(self.pageNum) forKey:@"page_number"];
if ([self.type isEqualToString:@"collect"] || [self.type isEqualToString:@"hot"]) {
[dic setObject:self.type forKey:@"scene"];
}else{
[dic setObject:@"type" forKey:@"scene"];
[dic setObject:self.type forKey:@"type_id"];
}
[NetworkRequest requestPOST:@"/room/list" parameters:dic block:^(BaseResponse * _Nonnull response) {
if (response.isSuccess) {
if ([ToolsObject IsNullWithObject:response.data]) {
[ws endrefresh];
return;
}
if (self.pageNum == 1) {
[ws.dataArr removeAllObjects];
}
[ws.dataArr addObjectsFromArray:[NSArray yy_modelArrayWithClass:DDRoomPartyModel.class json:response.data[@"record"]]];
dispatch_async(dispatch_get_main_queue(), ^{
if ([self.type isEqualToString:@"collect"] && self.dataArr.count==0) {
self.isNoCollection = YES;
[self.collectionView.mj_footer endRefreshingWithNoMoreData];
[self requestNoCollectionData];
}else{
self.isNoCollection = NO;
[self endrefresh];
}
[self.collectionView reloadData];
});
if ([response.data[@"next"]integerValue] == 0) {
[self.collectionView.mj_footer endRefreshingWithNoMoreData];
}
}
}];
}
-(void)requestNoCollectionData
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"1" forKey:@"type"];
WeakSelf(ws)
[NetworkRequest requestPOST:@"/room/random/room" parameters:dic block:^(BaseResponse * _Nonnull response) {
if (response.isSuccess) {
if ([ToolsObject IsNullWithObject:response.data]) {
[ws endrefresh];
return;
}
[ws.noDataArr removeAllObjects];
[ws.noDataArr addObjectsFromArray:[NSArray yy_modelArrayWithClass:DDRoomPartyModel.class json:response.data]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
}];
}
-(void)endrefresh
{
[self.collectionView.mj_header endRefreshing];
[self.collectionView.mj_footer endRefreshing];
}
#pragma mark - <UICollectionViewDataSource, UICollectionViewDelegate>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if ([self.type isEqualToString:@"collect"] && self.dataArr.count==0) {
return self.noDataArr.count;
}else{
return self.dataArr.count;
}
}
#pragma mark - header宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
if ([self.type isEqualToString:@"collect"] && self.dataArr.count==0) {
return CGSizeMake(kWidth, 80);
}
return CGSizeMake(kWidth, 0.01);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if(kind == UICollectionElementKindSectionHeader){
if ([self.type isEqualToString:@"collect"] && self.dataArr.count==0) {
DDV2HomeVerticalHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"DDV2HomeVerticalHeadView" forIndexPath:indexPath];
WeakSelf(ws)
headerView.backChangeBtnBlock = ^{
[ws requestNoCollectionData];
};
return headerView;
}
}
reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
[reusableview addSubview:[UIView new]];
return reusableview;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if ([self.type isEqualToString:@"collect"] && self.dataArr.count==0) {
return CGSizeMake((kWidth-40)/2, (kWidth-40)/2+50);
}else{
if(indexPath.item<6){
return CGSizeMake((kWidth-50)/3, (kWidth-50)/3+50);
}else{
return CGSizeMake((kWidth-40)/2, (kWidth-40)/2+50);
}
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
DDV2HomeVerticalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DDV2HomeVerticalCell" forIndexPath:indexPath];
if (self.isNoCollection) {
if (indexPath.row < self.noDataArr.count) {
DDRoomPartyModel *model = self.noDataArr[indexPath.row];
cell.model = model;
}
}else{
if (indexPath.row < self.dataArr.count) {
DDRoomPartyModel *model = self.dataArr[indexPath.row];
cell.model = model;
}
}
cell.contentView.backgroundColor = UIColor.clearColor;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.isClick) {
return ;
}
self.isClick = YES;
if (self.isNoCollection) {
DDRoomPartyModel *model = self.noDataArr[indexPath.item];
[[ToolsObject shareTools] jumpToRoomVC:model.roomId followUser:[RoomUserInfoModel new]];
}else{
DDRoomPartyModel *model = self.dataArr[indexPath.item];
[[ToolsObject shareTools] jumpToRoomVC:model.roomId followUser:[RoomUserInfoModel new]];
}
WeakSelf(ws)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
ws.isClick = NO;
});
}
-(NSMutableArray *)dataArr
{
if (!_dataArr) {
_dataArr = [NSMutableArray array];
}
return _dataArr;
}
-(NSMutableArray *)noDataArr
{
if (!_noDataArr) {
_noDataArr = [NSMutableArray array];
}
return _noDataArr;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.minimumLineSpacing = 12;
layout.minimumInteritemSpacing = 7;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.sectionInset = UIEdgeInsetsMake(10, 16, 0, 16);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = UIColor.clearColor;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.layer.masksToBounds = YES;
_collectionView.layer.cornerRadius = 12;
[_collectionView registerClass:[DDV2HomeVerticalCell class] forCellWithReuseIdentifier:@"DDV2HomeVerticalCell"];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
[_collectionView registerClass:[DDV2HomeVerticalHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"DDV2HomeVerticalHeadView"];
if ([_collectionView respondsToSelector:@selector(setLayoutMargins:)]){
[_collectionView setLayoutMargins:UIEdgeInsetsZero];
}
_collectionView.translatesAutoresizingMaskIntoConstraints = NO;
if (@available(iOS 11.0, *)) {
[_collectionView adjustedContentInsetDidChange];
_collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
WeakSelf(ws)
// 下拉刷新
_collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[ws requestData];
}];
// 上拉加载
_collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
ws.pageNum ++ ;
[ws requestData];/** 列表 */
}];
}
return _collectionView;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end