// // MOGiftCollectionView.m // MiMoLive // // Created by SuperC on 2023/11/23. // //一页的size #define kPageSize 60 #import "MOGiftCollectionView.h" #import "MOShowBubbleTool.h" @interface MOGiftCollectionView () @property (nonatomic, strong) UICollectionView *collectionView; /** 没有更多的数据视图 */ @property (nonatomic, strong) MONoMoreDataView *noMoreDataView; @property (nonatomic, strong) MOGiftListViewModel *listViewModel; @property (nonatomic, assign) BOOL isOnShow; @end @implementation MOGiftCollectionView - (instancetype)initWith:(MOGiftListViewModel *)listViewModel { self = [super init]; if (self) { [self setupUI]; self.listViewModel = listViewModel; [listViewModel addObserver:self]; } return self; } - (void)setupUI{ [self addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; } - (void)getHttpDataWithDict:(BOOL)firstPage { __weak typeof(self) weakSelf = self; [self.listViewModel loadGiftListWithCategory:self.type reload:firstPage completion:^(BOOL success) { __strong typeof(self) strongSelf = weakSelf; if (!strongSelf) return; if ([strongSelf.listViewModel isGiftListHasNext:strongSelf.type]) { [strongSelf.collectionView.mj_footer endRefreshing]; } else { [strongSelf.collectionView.mj_footer endRefreshingWithNoMoreData]; } }]; } - (void)handleCellClick:(NSIndexPath *)indexPath { NSArray *giftList = [self.listViewModel giftListFor:self.type]; if (self.selectIndexPath == indexPath) return; if (indexPath.row >= giftList.count) return; MOGiftlist *modle = giftList[indexPath.row]; if (modle.lock) return; for (MOGiftlist *giftModel in giftList) { giftModel.isSelect = NO; } modle.isSelect = YES; self.selectIndexPath = indexPath; self.selectCellBlock(modle, self.type); } #pragma mark - JXCategoryListContentViewDelegate - (void)listWillAppear { self.isOnShow = YES; NSArray *giftList = [self.listViewModel giftListFor:self.type]; self.noMoreDataView.isHaveData = giftList.count > 0; } - (void)listDidAppear { NSArray *giftList = [self.listViewModel giftListFor:self.type]; if (giftList.count == 0) { [self getHttpDataWithDict:YES]; } else{ for (MOGiftlist *giftModel in giftList) { giftModel.isSelect = NO; } [self toChangeTheFirstChooseWith:giftList];//默认选中第一个 [self toSelectTheHitGift:giftList];//跳转Hit礼物 [self.collectionView reloadData]; } } - (void)listWillDisappear { self.isOnShow = NO; } - (void)toChangeTheFirstChooseWith:(NSArray *)dataArr{ if (![MOShowBubbleTool hadShowSendGiftGuideView]) { return; } if(dataArr.count == 0){ return; } BOOL isHaveHit = NO; //是否有冲榜礼物对象 if([MOSvgaSourceManage shareManager].hitGiftModel){ NSString *theHitGiftId = [MOSvgaSourceManage shareManager].hitGiftModel.giftId; NSInteger theIndex = 0; for (MOGiftlist *object in dataArr) { if([object.giftInfo.id isEqualToString:theHitGiftId]){ isHaveHit = YES; break; } theIndex ++; } } if(isHaveHit){ return; } MOGiftlist *giftModel = dataArr.firstObject; if(!giftModel.lock){ giftModel.isSelect = YES; self.selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];//选中第一个 self.selectCellBlock(giftModel, self.type); } else{ self.theFirstGiftBlock ? self.theFirstGiftBlock() : nil; } } - (void)toSelectTheHitGift:(NSArray *)giftList { //是否有冲榜礼物对象 if([MOSvgaSourceManage shareManager].hitGiftModel){ NSString *theHitGiftId = [MOSvgaSourceManage shareManager].hitGiftModel.giftId; NSInteger theIndex = 0; for (MOGiftlist *object in giftList) { if([object.giftInfo.id isEqualToString:theHitGiftId]){ object.isSelect = YES; self.selectIndexPath = [NSIndexPath indexPathForRow:theIndex inSection:0];//选中第一个 self.selectCellBlock(object, self.type); [self.collectionView scrollToItemAtIndexPath:self.selectIndexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO]; [MOSvgaSourceManage shareManager].hitGiftModel = nil; break; } theIndex ++; } } } - (UIView *)listView{ return self; } #pragma mark MOGiftListViewModelDelegate - (void)onGiftListChangedWithCategory:(NSInteger)category list:(NSArray *)list { if (self.type != category || !self.isOnShow) return; BOOL isHaveSelet = NO; for (MOGiftlist *giftModel in list) { if(giftModel.isSelect){ isHaveSelet = YES; break; } } if(!isHaveSelet){ [self toChangeTheFirstChooseWith:list]; } [self.collectionView reloadData]; self.noMoreDataView.isHaveData = list.count > 0 ? YES : NO; [self toSelectTheHitGift:list];//跳转Hit礼物 } #pragma mark UICollectionViewDelegate,UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [self.listViewModel giftListFor:self.type].count; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSArray *giftList = [self.listViewModel giftListFor:self.type]; WEAKSELF MOGiftlist *modle = giftList[indexPath.row]; MOGiftItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MOGiftItemCell_ID forIndexPath:indexPath]; cell.isBag = false; cell.listModel = modle; cell.selectSkin = self.selectSkin; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; [self handleCellClick:indexPath]; [self.collectionView reloadData]; } - (void)toUpdataTheSelectIndexPathCell{ if(self.selectIndexPath){ MOGiftItemCell *cell = (MOGiftItemCell *)[self.collectionView cellForItemAtIndexPath:self.selectIndexPath]; if(cell){ cell.selectSkin = self.selectSkin; [cell updataCellShowData]; } } } - (void)oneSecondPassed{ } #pragma mark - Lazy - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; CGFloat width = SCREENWIDTH / 4.0; CGFloat height = 104; flow.itemSize = CGSizeMake(width, height); flow.minimumLineSpacing = 4.0;//行间距 flow.minimumInteritemSpacing = 0.0;//列间距 flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 100) collectionViewLayout:flow]; _collectionView.backgroundColor = [UIColor clearColor]; _collectionView.dataSource = self; _collectionView.delegate = self; [_collectionView registerClass:[MOGiftItemCell class] forCellWithReuseIdentifier:MOGiftItemCell_ID]; WEAKSELF MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)]; _collectionView.mj_footer = footer; [footer setTitle:@" " forState:MJRefreshStateNoMoreData]; // 没有更多数据 _collectionView.mj_footer.hidden = YES; _collectionView.backgroundView = self.noMoreDataView; } return _collectionView; } - (void)loadMoreData{ if (![self.listViewModel isGiftListHasNext:self.type]) { return; } [self getHttpDataWithDict:NO]; } - (MONoMoreDataView *)noMoreDataView{ if(!_noMoreDataView){ _noMoreDataView = [MONoMoreDataView new]; _noMoreDataView.topImg.alpha = 0.6; _noMoreDataView.tipLab.textColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:0.5]; } return _noMoreDataView; } @end