// // MOSquareBaseCollectionView.m // MiMoLive // // Created by SuperC on 2024/7/31. // #define SetBtnWidthAndHeight 26.0 //一页的size #define kPageSize 30 #define kCollectionViewLeftAndRight 12.0 #define kInsertBannerIndex 8 #define kShowEnterRoomGuideViewKey @"kShowEnterRoomGuideViewKey" #import "MOSquareBaseCollectionView.h" #import "MOScrollMenuView.h" #import "MONoMoreDataView.h" #import "MOFollowInfoView.h" #import "MOFollowNoDataView.h" #import "MOSquareBannerReusableView.h" #import "MOLiveCell.h" #import "MOShowLivePagesVC.h" #import "MOLiveBase.h" #import "MOLiveList.h" #import "MOCountryBaseData.h" #import "MOCountryList.h" #import "MOMoreCountryView.h" #import "MOThemeManager.h" #import "MOMinimizeManager.h" #import "MOGuideMaskView.h" #import "UIScrollView+Scrollable.h" #import "MOLeftAlignedFlowLayout.h" @interface MOSquareBaseCollectionView () @property (nonatomic, strong) UIView *bgView; /** 国家*/ @property (nonatomic, strong) MOScrollMenuView *countryView; /** 展示所有国家的按钮 */ @property (nonatomic, strong) BigBtn *allCountryBtn; /** 主界面 */ @property (nonatomic, strong) UICollectionView *collectionView; /** 数据源(服务端返回的原数据) */ @property (nonatomic, strong) NSMutableArray *dataArr; /** 数据源(用来显示页面的数据源数组,index == kInsertBannerIndex 的位置插入bannerTag==YES的model) */ @property (nonatomic, strong) NSMutableArray *showDataArr; /** 没有更多的数据视图 */ @property (nonatomic, strong) MONoMoreDataView *noMoreDataView; /** 关注界面 - 视频/音频切换view */ @property (nonatomic, strong) MOScrollMenuView *followTypeView; /** follow数据展示View */ @property (nonatomic, strong) MOFollowInfoView *followInfoView; /** 无数据view */ @property (nonatomic, strong) MOFollowNoDataView *followNoData; /** 关注页面数据 */ @property (nonatomic, strong) MOLiveBase *followInfoData; @property (nonatomic, strong) MOSquareBannerReusableView *headView; @property (nonatomic, strong) NSArray *bannerArr; @property (nonatomic, assign) BOOL isFirstShow; /** 国家列表 */ @property (nonatomic, strong) NSMutableArray *countryDataArr; @property (nonatomic, assign) BOOL isProcessingSelection; //是否下拉刷新 @property (nonatomic, assign) BOOL pullRefresh; @end @implementation MOSquareBaseCollectionView - (instancetype)init{ self = [super init]; if (self) { [self setupUI]; } return self; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setupUI]; } return self; } - (instancetype)initWithFrame:(CGRect)frame AndIsFollow:(BOOL)follow{ self = [super initWithFrame:frame]; if (self) { self.isFollow = follow; [self setupUI]; } return self; } - (void)setupUI{ //默认视频直播 self.searchType = 2; self.next = @""; self.countryString = @""; self.roomType = 1; self.sortType = 1; self.isNeedWillRefresh = NO; self.isFirstShow = NO; [self addSubview:self.bgView]; [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; if(self.isFollow){ // [self.bgView addSubview:self.followTypeView]; // [self.followTypeView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(self.bgView); // make.left.equalTo(self.bgView).offset(15.0); // make.height.equalTo(@24.0); // make.right.equalTo(self.bgView).offset(-15.0); // }]; [self.bgView addSubview:self.followInfoView]; [self.followInfoView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.bgView).offset(12.0); make.left.right.equalTo(self.bgView); make.height.equalTo(@26.0); }]; // [self.bgView addSubview:self.followNoData]; // [self.followNoData mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(self.bgView).offset(12.0); // make.left.right.equalTo(self.bgView); // make.height.equalTo(@180.0); // }]; [self.bgView addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.followInfoView.mas_bottom).offset(15.0); make.left.equalTo(self.bgView).offset(kCollectionViewLeftAndRight); make.right.equalTo(self.bgView).offset(-kCollectionViewLeftAndRight); make.bottom.equalTo(self.bgView); }]; [self setFollowTypeChooseBlock]; } else{ [self.bgView addSubview:self.countryView]; [self.countryView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.bgView).offset(0.0); make.left.equalTo(self.bgView).offset(15.0); // make.height.equalTo(@24.0); make.right.equalTo(self.bgView).offset(-15.0 - 30); make.height.mas_equalTo(0); }]; [self.bgView addSubview:self.allCountryBtn]; [self.allCountryBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.bgView).offset(-13.0); // make.width.height.equalTo(@24.0); make.width.height.mas_equalTo(0); make.centerY.equalTo(self.countryView.mas_centerY); }]; [self.bgView addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(self.countryView.mas_bottom).offset(15.0); make.top.mas_equalTo(0); make.left.equalTo(self.bgView).offset(kCollectionViewLeftAndRight); make.right.equalTo(self.bgView).offset(-kCollectionViewLeftAndRight); make.bottom.equalTo(self.bgView); }]; self.collectionView.backgroundView = self.noMoreDataView; } } - (void)updateFollowViewUI{ if(self.followInfoData.recommend){ //推荐数据 self.followInfoView.hidden = YES; [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.left.equalTo(self.bgView).offset(kCollectionViewLeftAndRight); make.right.equalTo(self.bgView).offset(-kCollectionViewLeftAndRight); make.bottom.equalTo(self.bgView); }]; [UIView animateWithDuration:0.3 animations:^{ [self layoutIfNeeded]; }]; } else{ self.followInfoView.hidden = NO; [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.followInfoView.mas_bottom).offset(15.0); make.left.equalTo(self.bgView).offset(kCollectionViewLeftAndRight); make.right.equalTo(self.bgView).offset(-kCollectionViewLeftAndRight); make.bottom.equalTo(self.bgView); }]; [UIView animateWithDuration:0.3 animations:^{ [self layoutIfNeeded]; }]; self.followInfoView.contentLab.text = [NSString stringWithFormat:@"%.f %@",self.followInfoData.liveCount,NSLocalString(@"mimo_square_following_people_num")]; } } #pragma mark - Block - (void)setFollowTypeChooseBlock{ WEAKSELF self.followTypeView.clickBtnBlock = ^(NSInteger index) { __strong typeof(weakSelf) self = weakSelf; if(index == 0){ self.roomType = 1; } else{ self.roomType = 2; } //第一次位空字符串 self.next = @""; NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"roomType":@(self.roomType), @"sortType":@(self.sortType), @"page":basePage}; [self toGetHttpFollowDataWithDict:baseDict]; }; } - (void)setCountryChooseBlock{ self.countryView.titleArray = [self.countryDataArr copy]; self.countryView.currentButtonIndex = 0; WEAKSELF self.countryView.clickBtnBlock = ^(NSInteger index) { __strong typeof(weakSelf) self = weakSelf; MOCountryList *countryModel = self.countryDataArr[index]; if(index == 0){ //推荐 self.countryString = @""; } else{ self.countryString = countryModel.id; } //第一次位空字符串 self.next = @""; //视频直播 NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"searchType":@(self.searchType), @"category":self.countryString ?: @"", @"page":basePage}; [self getHttpDataWithDict:baseDict]; }; } #pragma mark - JXCategoryListContentViewDelegate - (void)listWillAppear{ if(self.dataArr.count == 0){ if(self.isFollow){ //关注列表数据 NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"roomType":@(self.roomType), @"sortType":@(self.sortType), @"page":basePage}; [self toGetHttpFollowDataWithDict:baseDict]; } else{ if(self.dataArr.count == 0){ //第一次位空字符串 (代表第一页) self.next = @""; //视频直播 NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"searchType":@(self.searchType), @"category":self.countryString ?: @"", @"page":basePage}; [self getHttpDataWithDict:baseDict]; } if(self.bannerArr.count == 0){ if(self.searchType == 2){ //视频直播 [self getSquareBannerListDataWith:1]; } else if (self.searchType == 3){ //音频直播 [self getSquareBannerListDataWith:6]; } else if (self.searchType == 4){ //新人 [self getSquareBannerListDataWith:3]; } } } } //获取国家列表 if(!self.isFollow){ if(self.countryDataArr.count == 0){ [self getHttpDataAboutCategroy]; } //国家列表数据缓存 if([MOSvgaSourceManage shareManager].countryArr.count == 0){ [self getHttpDataAboutCountry]; } } } - (UIView *)listView{ return self; } #pragma mark - Mothed - (void)toBeginRefresh{ [self.collectionView.mj_header beginRefreshing]; } - (void)showEnterRoomGuideView { if (self.dataArr.count == 0) { return; } NSIndexPath *firstIndexPath = [NSIndexPath indexPathForItem:0 inSection:0]; UICollectionViewCell *firstCell = [self.collectionView cellForItemAtIndexPath:firstIndexPath]; if (firstCell) { CGRect cellFrameInWindow = [firstCell convertRect:firstCell.bounds toView:[UIApplication sharedApplication].keyWindow]; // 扩大四边各 5pt CGRect holeRect = CGRectInset(cellFrameInWindow, -5, -5); [self showGuideMaskWithHoleInRect:holeRect]; } } - (void)showGuideMaskWithHoleInRect:(CGRect)holeRect { if (!self.window) {//view没显示不弹出气泡 return; } NSString *showKey = (NSString *)[[MODataCache sharedYYCache] objectForKey:kShowEnterRoomGuideViewKey]; if (showKey.length > 0) {//已经显示过了,不再显示 return; } MOGuideMaskView *guideView = [[MOGuideMaskView alloc] initWithFrame:[UIScreen mainScreen].bounds]; [guideView addHoleWithRect:holeRect cornerRadius:16]; [guideView show]; [[MODataCache sharedYYCache] setObject:kShowEnterRoomGuideViewKey forKey:kShowEnterRoomGuideViewKey]; CGFloat holeCenterX = CGRectGetMidX(holeRect); CGFloat bottomY = CGRectGetMaxY(holeRect); UIView *textView = [[UIView alloc] init]; textView.backgroundColor = [MOTools colorWithHexString:@"#FF4DA6"]; textView.layer.masksToBounds = YES; textView.layer.cornerRadius = 6; [guideView addSubview:textView]; NSString *showText = NSLocalString(@"mimo_2_guide_select_host"); UIFont *showFont = [MOTextTools poppinsMediumFont:12]; CGFloat textWidth = [MOTools getWidthWithString:showText font:showFont]; CGFloat viewWidth = textWidth + 12; [textView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(bottomY + 59); make.centerX.equalTo(guideView.mas_left).offset(holeCenterX); make.size.mas_equalTo(CGSizeMake(viewWidth, 24)); }]; UIView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_guilde_left"]]; [guideView addSubview:leftView]; [leftView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(textView.mas_left); make.centerY.equalTo(textView.mas_top); make.size.mas_equalTo(CGSizeMake(24, 19)); }]; UIView *rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_guilde_right"]]; [guideView addSubview:rightView]; [rightView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(textView); make.bottom.equalTo(textView.mas_top); make.size.mas_equalTo(CGSizeMake(10, 10)); }]; UILabel *textLabel = [[UILabel alloc] init]; textLabel.font = showFont; textLabel.textColor = [MOTools colorWithHexString:@"#FFFFFF"]; textLabel.text = showText; [textView addSubview:textLabel]; [textLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.center.mas_equalTo(0); }]; UIImageView *lineView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_guilde_line_up"]]; [guideView addSubview:lineView]; [lineView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(bottomY + 2); make.centerX.equalTo(guideView.mas_left).offset(holeCenterX); make.size.mas_equalTo(CGSizeMake(10, 58)); }]; UIView *arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_guilde_arrow_up"]]; [guideView addSubview:arrowView]; [arrowView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(textView.mas_top).offset(1); make.centerX.equalTo(lineView); make.size.mas_equalTo(CGSizeMake(11, 6)); }]; } #pragma mark UICollectionViewDelegate,UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.showDataArr.count; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { WEAKSELF MOLiveList *model; //因为牵扯Follow逻辑, 容易出现数组越界崩溃, 先这样处理 if(indexPath.row < self.showDataArr.count){ model = self.showDataArr[indexPath.row]; if (model.bannerTag) { MOSquareBannerReusableView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MOSquareBannerReusableView_ID forIndexPath:indexPath]; cell.imageArr = self.bannerArr; return cell; } else { MOLiveCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MOLiveCell_ID forIndexPath:indexPath]; cell.model = model; //更多按钮点击事件 cell.moreBtnBlock = ^(MOLiveList * _Nonnull model) { for (MOLiveList *object in weakSelf.dataArr) { if([object.id isEqualToString:model.id]){ object.isViewSelect = YES; } } }; cell.cancelBtnBlock = ^(MOLiveList * _Nonnull model) { for (MOLiveList *object in weakSelf.dataArr) { if([object.id isEqualToString:model.id]){ object.isViewSelect = NO; } } }; __weak __typeof__(cell) weakCell = cell; cell.noLikeBlock = ^(MOLiveList * _Nonnull model) { NSIndexPath *newIndexPath = [weakSelf.collectionView indexPathForCell:weakCell]; if(weakSelf.showDataArr.count > newIndexPath.row){ MOLiveList *liveModel = [weakSelf.showDataArr objectAtIndex:newIndexPath.row]; [weakSelf.showDataArr removeObject:liveModel]; [weakSelf.collectionView performBatchUpdates:^{ [weakSelf.collectionView deleteItemsAtIndexPaths:@[newIndexPath]]; } completion:^(BOOL finished) { [weakSelf.dataArr removeObject:liveModel]; [weakSelf handleDataAndReloadCollectionViewData]; }]; } }; return cell; } } return [collectionView dequeueReusableCellWithReuseIdentifier:MOLiveCell_ID forIndexPath:indexPath]; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.isProcessingSelection) { return; } self.isProcessingSelection = YES; [collectionView deselectItemAtIndexPath:indexPath animated:YES]; if(!self.homeVC){ return; } [MOShowAgoraKitManager shareManager].isFirstShow = YES; [MOShowAgoraKitManager shareManager].beginIndex = indexPath.row; [MOShowAgoraKitManager shareManager].isCreatLive = NO; MOLiveList *model = self.showDataArr[indexPath.row]; //TODO: 进入直播房 MOShowLivePagesVC *vc = [[MOShowLivePagesVC alloc] init]; vc.userType = MOShowLiveAudienceType; vc.roomList = [self.dataArr mutableCopy]; vc.searchType = self.searchType; vc.next = self.next; vc.countryString = self.countryString; vc.roomType = self.roomType; vc.sortType = self.sortType; NSInteger index = 0; int x = 0; for (MOLiveList *listModel in self.dataArr) { if([listModel.id isEqualToString:model.id]){ index = x; } x ++; } vc.focusIndex = index; WEAKSELF vc.deleteCloseRoomBlock = ^(NSString * _Nonnull roomId) { if(roomId.length == 0){ return; } BOOL isHave = NO; NSInteger theRow = 0; for (MOLiveList *listModel in weakSelf.dataArr) { if([roomId isEqualToString:listModel.id]){ isHave = YES; break; } theRow ++; } if(isHave == YES){ NSIndexPath *theIndexPath = [NSIndexPath indexPathForRow:theRow inSection:0]; [weakSelf.showDataArr removeObjectAtIndex:theRow]; [weakSelf handleDataAndReloadCollectionViewData]; } }; //关闭直播间的回调 vc.closeRoomBlock = ^{ NSArray *checkArr = [MOShowAgoraKitManager getSurroundingElementsWith:weakSelf.dataArr Range:10]; [[MOShowAgoraKitManager shareManager] toCheckTheLiveRoomWithRoomListArr:checkArr andBlock:^(NSArray * _Nonnull closeArr) { NSMutableArray *deleteIndexArr = [NSMutableArray array]; // 创建一个可变的NSIndexSet,用于添加不连续的索引 NSMutableIndexSet *indexesToRemove = [NSMutableIndexSet indexSet]; for (int x = 0; x < weakSelf.dataArr.count; x ++) { MOLiveList *liveModel = weakSelf.dataArr[x]; NSString *roomIdStr = liveModel.id; if([closeArr containsObject:roomIdStr]){ [indexesToRemove addIndex:x]; } } [weakSelf.dataArr removeObjectsAtIndexes:indexesToRemove]; [weakSelf handleDataAndReloadCollectionViewData]; }]; }; vc.dataChangeBlock = ^(NSString * _Nonnull nextStr, NSMutableArray * _Nonnull roomList) { if(nextStr.length == 0){ //最后一页 [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData]; } if(roomList.count > weakSelf.dataArr.count){ weakSelf.dataArr = [roomList mutableCopy]; [weakSelf handleDataAndReloadCollectionViewData]; weakSelf.collectionView.mj_footer.hidden = (weakSelf.dataArr.count > 0) ? NO : YES; } }; // UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; // nav.modalPresentationStyle = UIModalPresentationFullScreen; // [self.homeVC presentViewController:nav animated:YES completion:nil]; vc.hidesBottomBarWhenPushed = YES; [self.homeVC.navigationController pushViewController:vc animated:YES]; self.isNeedWillRefresh = YES; //重置状态 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.isProcessingSelection = NO; }); } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { if (self.isFollow && self.followInfoData.recommend) { MOFollowNoDataView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MOFollowNoDataView" forIndexPath:indexPath]; headView.backgroundColor = [UIColor clearColor]; return headView; } else { // 返回一个空 header 防止崩溃 UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionHeader" forIndexPath:indexPath]; return view; } } else { UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"sectionFoot" forIndexPath:indexPath]; return view; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ if(self.isFollow && self.followInfoData.recommend){ return CGSizeMake(SCREENWIDTH, 180); } return CGSizeMake(SCREENWIDTH, 0.01); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { MOLiveList* model = self.showDataArr[indexPath.row]; if (model.bannerTag) { CGFloat collectionWidth = SCREENWIDTH - kCollectionViewLeftAndRight * 2; return CGSizeMake(collectionWidth, 82); } else { CGFloat collectionWidth = SCREENWIDTH - kCollectionViewLeftAndRight * 2 - 7.0; return CGSizeMake(collectionWidth / 2.0 , 229); } } // 开始拖动时隐藏 tabBar - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { if (![scrollView isContentVerticallyScrollable]) { return; } [[MOMainTabController sharedSingleton] hideTabbarWithAnimation:YES]; if (self.beginScrollBlock) { self.beginScrollBlock(); } } // 停止滚动时显示 tabBar(包括减速完成) - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (![scrollView isContentVerticallyScrollable]) { return; } [[MOMainTabController sharedSingleton] showTabbarWithAnimation:YES]; } // 如果没有减速(立即停止),也要显示 tabBar - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { [[MOMainTabController sharedSingleton] showTabbarWithAnimation:YES]; } } #pragma mark - Http //获取界面数据 - (void)getHttpDataWithDict:(NSDictionary *)dict{ WEAKSELF [kHttpManager getLiveRoomListWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) { __strong typeof(weakSelf) self = weakSelf; [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; weakSelf.isNeedWillRefresh = NO; if(kCode_Success){ MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]); MOLiveBase *baseModel = [MOLiveBase modelObjectWithDictionary:data[@"data"]]; if(baseModel.next == nil || baseModel.next.length == 0){ if(self.next == nil || self.next.length == 0){ //第一页 [self.dataArr removeAllObjects]; [self toAddNewListDataWith:baseModel.liveList]; } else{ //最后一页 [self toAddNewListDataWith:baseModel.liveList]; } self.next = @""; [self.collectionView.mj_footer endRefreshingWithNoMoreData]; } else{ if(self.next == nil || self.next.length == 0){ //第一页 [self.dataArr removeAllObjects]; [self toAddNewListDataWith:baseModel.liveList]; } else{ //最后一页 [self toAddNewListDataWith:baseModel.liveList]; } self.next = baseModel.next; } // [self.collectionView reloadData]; [self handleDataAndReloadCollectionViewData]; self.collectionView.mj_footer.hidden = (self.dataArr.count > 0) ? NO : YES; self.noMoreDataView.isHaveData = (self.dataArr.count > 0) ? YES : NO; if (self.dataArr.count > 0) { [MOMinimizeManager shareManager].roomList = self.dataArr; } } else{ kShowNetError(data) } }]; } - (void)toAddNewListDataWith:(NSArray *)liveListArr{ NSMutableArray *elementsToAdd = [NSMutableArray array]; BOOL isOpen = [MOTools toJudgeShowWame]; for (MOLiveList *listModel in liveListArr) { BOOL isElementFound = NO; // 检查数组A中是否存在相同元素 for (NSInteger i = 0; i < self.dataArr.count; i++) { MOLiveList *elementA = self.dataArr[i]; if([elementA.anchorUser.id isEqualToString:listModel.anchorUser.id]){ [self.dataArr replaceObjectAtIndex:i withObject:listModel]; isElementFound = YES; break; } } if(!isOpen){ if(listModel.roomType == 2 || listModel.roomType == 3){ continue; } } // 未找到则记录待添加 if (!isElementFound) { [elementsToAdd addObject:listModel]; } } // 将B中独有的元素添加到A [self.dataArr addObjectsFromArray:elementsToAdd]; } - (void)toGetHttpFollowDataWithDict:(NSDictionary *)dict{ WEAKSELF [kHttpManager getLiveRoomFollowListWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) { __strong typeof(weakSelf) self = weakSelf; [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; self.isNeedWillRefresh = NO; if(kCode_Success){ MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]); self.followInfoData = [MOLiveBase modelObjectWithDictionary:data[@"data"]]; if(self.followInfoData.next == nil || self.followInfoData.next.length == 0){ if(self.next == nil || self.next.length == 0){ //第一页 [self.dataArr removeAllObjects]; [self toAddNewListDataWith:self.followInfoData.liveList]; } else{ //最后一页 [self toAddNewListDataWith:self.followInfoData.liveList]; } self.next = @""; [self.collectionView.mj_footer endRefreshingWithNoMoreData]; } else{ if(self.next == nil || self.next.length == 0){ [self.dataArr removeAllObjects]; [self toAddNewListDataWith:self.followInfoData.liveList]; } else{ [self toAddNewListDataWith:self.followInfoData.liveList]; } self.next = self.followInfoData.next; } [self handleDataAndReloadCollectionViewData]; // [self.collectionView reloadData]; [self updateFollowViewUI]; self.collectionView.mj_footer.hidden = (self.dataArr.count > 0) ? NO : YES; self.noMoreDataView.isHaveData = (self.dataArr.count > 0) ? YES : NO; if (self.dataArr.count > 0) { [MOMinimizeManager shareManager].roomList = self.dataArr; } } else{ kShowNetError(data) } }]; } - (void)getSquareBannerListDataWith:(NSInteger)type{ WEAKSELF //banner分类(1=首页图片,2=直播间图片,3=New页图片,4=启动页图片,5=开播页图片,6=语音列表图片) NSDictionary *dict = @{@"category":@(type)}; [kHttpManager getBaseBannerListWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) { if(kCode_Success){ // MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]); MOBannerData *baseModel = [MOBannerData modelObjectWithDictionary:data[@"data"]]; weakSelf.headView.imageArr = [baseModel.JumpList copy]; weakSelf.bannerArr = [baseModel.JumpList copy]; if(!weakSelf.isFirstShow){ weakSelf.isFirstShow = YES; [weakSelf.collectionView reloadData]; } } else{ // kShowNetError(data) } }]; } //获取国家列表 - (void)getHttpDataAboutCountry{ WEAKSELF [[MOSvgaSourceManage shareManager].countryArr removeAllObjects]; [kHttpManager getCountryAndRegionListWithParams:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) { __strong typeof(weakSelf) self = weakSelf; if(kCode_Success){ MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]); MOCountryBaseData *countryBase = [MOCountryBaseData modelObjectWithDictionary:data[@"data"]]; for (MOCountryList *object in countryBase.countryList) { if(object.code.length != 0){ [[MODataCache sharedYYCache] setObject:object forKey:[NSString stringWithFormat:@"%@_%@",kCountryArrInfo,object.code]]; } } [MOSvgaSourceManage shareManager].countryArr = [countryBase.countryList mutableCopy]; // self.countryDataArr = [countryBase.countryList mutableCopy]; // //第一个数据位推荐数据 MOCountryList *firstModel = [[MOCountryList alloc] init]; firstModel.name = NSLocalString(@"mimo_hot_recommend"); firstModel.icon = @""; [[MOSvgaSourceManage shareManager].countryArr insertObject:firstModel atIndex:0]; // // [self setCountryChooseBlock]; [self.collectionView reloadData]; } else { kShowNetError(data) } }]; } - (void)getHttpDataAboutCategroy{ WEAKSELF [kHttpManager toGetTheLiveRoomCategoryDataWithParams:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) { __strong typeof(weakSelf) self = weakSelf; if(kCode_Success){ MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]); MOCountryBaseData *countryBase = [MOCountryBaseData modelObjectWithDictionary:data[@"data"]]; self.countryDataArr = [countryBase.countryList mutableCopy]; //第一个数据位推荐数据 MOCountryList *firstModel = [[MOCountryList alloc] init]; firstModel.name = NSLocalString(@"mimo_hot_recommend"); firstModel.icon = @""; [self.countryDataArr insertObject:firstModel atIndex:0]; [self setCountryChooseBlock]; // [self.collectionView reloadData]; } else { kShowNetError(data) } }]; } - (void)handleDataAndReloadCollectionViewData { self.showDataArr = self.dataArr.mutableCopy; if (!self.isFollow) { MOLiveList *bannerModel = [[MOLiveList alloc] init]; bannerModel.bannerTag = YES; if (self.showDataArr.count <= kInsertBannerIndex) { [self.showDataArr addObject:bannerModel]; } else { [self.showDataArr insertObject:bannerModel atIndex:kInsertBannerIndex]; } } [self.collectionView reloadData]; if (self.pullRefresh) {//下拉刷新需要显示tabbar dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[MOMainTabController sharedSingleton] showTabbarWithAnimation:YES]; self.pullRefresh = NO; }); } } #pragma mark - Lazy - (UICollectionView *)collectionView { if (!_collectionView) { CGFloat collectionWidth = SCREENWIDTH - kCollectionViewLeftAndRight * 2 - 7.0; MOLeftAlignedFlowLayout *layout = [[MOLeftAlignedFlowLayout alloc] init]; layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); layout.minimumLineSpacing = 7.0; layout.minimumInteritemSpacing = 7.0; layout.itemSize = CGSizeMake(collectionWidth / 2.0 , 229); _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, collectionWidth, collectionWidth / 2) collectionViewLayout:layout]; [_collectionView registerClass:[MOLiveCell class] forCellWithReuseIdentifier:MOLiveCell_ID]; [_collectionView registerClass:[MOSquareBannerReusableView class] forCellWithReuseIdentifier:MOSquareBannerReusableView_ID]; [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass(MOFollowNoDataView.class) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass(MOFollowNoDataView.class)]; [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionHeader"]; [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"sectionFoot"]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.backgroundColor = [UIColor clearColor]; WEAKSELF MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ __strong typeof(weakSelf) self = weakSelf; self.pullRefresh = YES; //获取banner数据 self.next = @""; if(self.isFollow){ //关注列表数据 NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"roomType":@(self.roomType), @"sortType":@(self.sortType), @"page":basePage}; [weakSelf toGetHttpFollowDataWithDict:baseDict]; return; } else{ if(self.searchType == 2){ //视频直播 [self getSquareBannerListDataWith:1]; } else if (self.searchType == 3){ //音频直播 [self getSquareBannerListDataWith:6]; } else if (self.searchType == 4){ //新人 [self getSquareBannerListDataWith:3]; } if(self.countryDataArr.count == 0 && !self.isFollow){ [self getHttpDataAboutCategroy]; } //国家列表数据缓存 if([MOSvgaSourceManage shareManager].countryArr.count == 0){ [self getHttpDataAboutCountry]; } //视频直播 NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next ?: @""}; NSDictionary *baseDict = @{@"searchType":@(self.searchType), @"category":self.countryString ?: @"", @"page":basePage}; [self getHttpDataWithDict:baseDict]; } }]; header.lastUpdatedTimeLabel.hidden = YES; header.stateLabel.hidden = YES; _collectionView.mj_header = header; _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ if(weakSelf.next.length == 0){ return; } if(weakSelf.isFollow){ //关注列表数据 NSDictionary *basePage = @{@"size":@(kPageSize),@"next":weakSelf.next}; NSDictionary *baseDict = @{@"roomType":@(weakSelf.roomType), @"sortType":@(weakSelf.sortType), @"page":basePage}; [weakSelf toGetHttpFollowDataWithDict:baseDict]; return; } else{ //视频直播 NSDictionary *basePage = @{@"size":@(kPageSize),@"next":weakSelf.next}; NSDictionary *baseDict = @{@"searchType":@(weakSelf.searchType), @"category":weakSelf.countryString ?: @"", @"page":basePage}; [weakSelf getHttpDataWithDict:baseDict]; } }]; _collectionView.mj_footer.hidden = YES; } return _collectionView; } - (NSMutableArray *)dataArr { if(!_dataArr){ _dataArr = [NSMutableArray array]; } return _dataArr; } - (NSMutableArray *)showDataArr { if (!_showDataArr) { _showDataArr = [NSMutableArray array]; } return _showDataArr; } - (NSMutableArray *)countryDataArr{ if(!_countryDataArr){ _countryDataArr = [NSMutableArray array]; } return _countryDataArr; } - (MOScrollMenuView *)followTypeView{ if(!_followTypeView){ CGFloat width = SCREENWIDTH - 15.0 * 2.0; _followTypeView = [[MOScrollMenuView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 24.0)]; MOCountryList *firstModel = [[MOCountryList alloc] init]; firstModel.name = NSLocalString(@"mimo_Anchor_Live"); firstModel.icon = @""; // MOCountryList *secondModel = [[MOCountryList alloc] init]; // secondModel.name = NSLocalString(@"mimo_Anchor_Party"); // secondModel.icon = @""; // _followTypeView.titleArray = @[firstModel,secondModel]; _followTypeView.titleArray = @[firstModel]; } return _followTypeView; } - (MOFollowInfoView *)followInfoView{ if(!_followInfoView){ _followInfoView = [MOFollowInfoView moFollowInfoView]; } return _followInfoView; } - (MOFollowNoDataView *)followNoData{ if(!_followNoData){ _followNoData = [MOFollowNoDataView moFollowNoDataView]; _followNoData.backgroundColor = [UIColor clearColor]; } return _followNoData; } - (MONoMoreDataView *)noMoreDataView{ if(!_noMoreDataView){ _noMoreDataView = [MONoMoreDataView new]; } return _noMoreDataView; } - (UIView *)bgView{ if(!_bgView){ _bgView = [[UIView alloc] init]; _bgView.backgroundColor = [UIColor clearColor]; } return _bgView; } - (MOScrollMenuView *)countryView{ if(!_countryView){ CGFloat width = SCREENWIDTH - 15.0 * 2.0; _countryView = [[MOScrollMenuView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 24.0)]; MOCountryList *firstModel = [[MOCountryList alloc] init]; firstModel.name = NSLocalString(@"mimo_hot_recommend"); firstModel.icon = @""; _countryView.titleArray = @[firstModel]; } return _countryView; } - (BigBtn *)allCountryBtn{ if (!_allCountryBtn) { _allCountryBtn = [BigBtn buttonWithType:UIButtonTypeCustom]; _allCountryBtn.backgroundColor = [UIColor clearColor]; _allCountryBtn.imageView.contentMode = UIViewContentModeScaleAspectFill; _allCountryBtn.imageView.layer.masksToBounds = YES; [_allCountryBtn setImage:[UIImage imageNamed:@"icon_square_more_back"] forState:UIControlStateNormal]; [_allCountryBtn addTarget:self action:@selector(allCountryBtnClick) forControlEvents:UIControlEventTouchUpInside]; } return _allCountryBtn; } - (void)allCountryBtnClick{ [self showCountryMoreView]; } - (void)showCountryMoreView{ if(self.countryDataArr.count == 0){ return; } WEAKSELF MOMoreCountryView *view = [MOMoreCountryView moMoreCountryView]; NSArray *tempArr = [self.countryDataArr mutableCopy]; for (MOCountryList *model in tempArr) { model.isChoose = NO; } MOCountryList *chooseModel = tempArr[self.countryView.currentButtonIndex]; chooseModel.isChoose = YES; view.dataArr = tempArr; UIWindow *keyWindow = kKeyWindow; [keyWindow addSubview:view]; [view showMoreCountryView]; view.clickBlock = ^(MOCountryList * _Nonnull listModel, NSIndexPath * _Nonnull indexPath) { weakSelf.countryView.currentButtonIndex = indexPath.row; __strong typeof(weakSelf) self = weakSelf; if(indexPath.row == 0){ //推荐 self.countryString = @""; } else{ self.countryString = listModel.id; } //第一次位空字符串 self.next = @""; //视频直播 NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"searchType":@(self.searchType), @"country":self.countryString, @"page":basePage}; [self getHttpDataWithDict:baseDict]; }; } @end