Files
peko-ios/YuMi/Modules/YMRoom/View/SendGiftView/View/XPGiftInfoView.m
2024-03-29 19:18:58 +08:00

347 lines
12 KiB
Objective-C

//
// YMGiftMiddleView.m
// YUMI
//
// Created by YUMI on 2021/11/9.
//
#import "XPGiftInfoView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
///#import "CountDownHelper.h"
#import "YUMIMacroUitls.h"
#import "YUMIHtmlUrl.h"
#import "ThemeColor+SendGift.h"
#import "NSArray+Safe.h"
#import "UIImage+Utils.h"
#import "CountDownHelper.h"
///Model
#import "GiftInfoModel.h"
#import "GiftReceiveInfoModel.h"
#import "RoomInfoModel.h"
///View
#import "XPGiftItemCollectionViewCell.h"
#import "XPGiftEmptyCollectionViewCell.h"
#import "XPGiftWeekStarCollectionViewCell.h"
#import "XPGiftCollectionViewFlowLayout.h"
#import "XPGiftFreeItemCell.h"
@interface XPGiftInfoView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, XPGiftWeekStarCollectionViewCellDelegate>
///l礼物列表
@property (nonatomic,strong) UICollectionView *giftcollectionView;
///分页控件
@property (nonatomic, strong) UIPageControl *pageController;
///展示的数据源
@property (nonatomic,strong) NSMutableArray <GiftInfoModel *> *datasource;
///最后一次选中的礼物
@property (nonatomic,strong) GiftInfoModel *lastSelectGift;
@end
@implementation XPGiftInfoView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.segmentType = GiftSegmentType_Normal;
// /礼物
[self addSubview:self.giftcollectionView];
[self addSubview:self.pageController];
}
- (void)initSubViewConstraints {
[self.giftcollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[self.pageController mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(10);
make.centerX.equalTo(self);
make.bottom.mas_equalTo(-10);
}];
}
#pragma mark - Public Method
- (void)updatePackSource:(GiftReceiveInfoModel *)giftReceiveInfo numberUser:(NSInteger)numberUser {
GiftInfoModel * giftInfo = [self findGiftInfoByGiftId:giftReceiveInfo.giftId.integerValue];
giftInfo.count -= giftReceiveInfo.giftNum * numberUser;
if (giftInfo.count == 0) {
if(self.freeModel.giftId.integerValue == giftInfo.giftId){
if([CountDownHelper shareHelper].isCountdownFinish == YES){
self.isDelFreeGift = YES;
[self.datasource removeObject:giftInfo];
}
}else{
[self.datasource removeObject:giftInfo];
}
if(self.segmentType == GiftSegmentType_Pack && self.datasource.count > 0){
NSInteger index = self.pageController.currentPage * 8 + 1;
if(self.datasource.count >= index){
GiftInfoModel * gift = [self.datasource safeObjectAtIndex1:index - 1];
[self dealSelectGift:gift];
}else{
NSInteger page = 0;
if (self.datasource.count % 8 == 0) { //刚好满页
page = self.datasource.count / 8;
} else {
page = self.datasource.count / 8 + 1;
}
[self.pageController setNumberOfPages:page];
GiftInfoModel * gift = [self.datasource firstObject];
self.pageController.hidden = page <= 1 || _segmentType == GiftSegmentType_WeekStar;
self.pageController.currentPage = page > 1 ? page - 1 : 0;
[self dealSelectGift:gift];
}
}
}
[self.giftcollectionView reloadData];
}
- (void)dealSelectGift:(GiftInfoModel *)giftInfo {
self.lastSelectGift = giftInfo;
if (self.segmentType == GiftSegmentType_Pack) {
giftInfo.sourceType = GiftSourceType_Pack;
} else {
giftInfo.sourceType = GiftSourceType_Normal;
}
giftInfo.isSelected = YES;
}
// 根据礼物id查找
- (GiftInfoModel *)findGiftInfoByGiftId:(NSInteger)giftId {
for (int i=0; i<self.datasource.count ; i++) {
GiftInfoModel *giftInfo = [self.datasource safeObjectAtIndex1:i];
if (giftInfo.giftId == giftId) {
return giftInfo;
}
}
return nil;
}
#pragma mark - scrollviewdelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.giftcollectionView) {
CGFloat offX = scrollView.contentOffset.x;
CGFloat width = CGRectGetWidth(scrollView.frame);
self.pageController.currentPage = ceilf(offX/width);
}
}
#pragma mark - UICollectionViewDelegate And UICollectionDatasource
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.segmentType == GiftSegmentType_Pack && self.datasource.count == 0) {
return CGSizeMake(KScreenWidth, 105 * 2 + 10);
}
if (self.segmentType == GiftSegmentType_WeekStar) {
return CGSizeMake(KScreenWidth, 105 * 2 + 10);
}
CGFloat itemWidth = (CGFloat)(KScreenWidth - 15 * 2 - 5 * 3) / (CGFloat)4;
return CGSizeMake(itemWidth, 108);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.segmentType == GiftSegmentType_Pack && self.datasource.count == 0) {
return 1;
}
if (self.segmentType == GiftSegmentType_WeekStar) {
return 1;
}
return self.datasource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if ((self.segmentType == GiftSegmentType_Pack || self.segmentType == GiftSegmentType_WeekStar) && self.datasource.count == 0) {
XPGiftEmptyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGiftEmptyCollectionViewCell class]) forIndexPath:indexPath];
cell.emptyTitle = YMLocalizedString(@"XPGiftInfoView1");
return cell;
}
if (self.segmentType == GiftSegmentType_WeekStar) {
XPGiftWeekStarCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGiftWeekStarCollectionViewCell class]) forIndexPath:indexPath];
cell.delegate = self;
cell.usingplaceType = self.usingplaceType;
cell.weekStarGiftList = self.datasource;
return cell;
}
GiftInfoModel * giftInfo = [self.datasource safeObjectAtIndex1:indexPath.item];
giftInfo.isSelected = self.lastSelectGift.giftId == giftInfo.giftId;
if(self.freeModel != nil && self.segmentType == GiftSegmentType_Pack && giftInfo.giftId == self.freeModel.giftId.integerValue){
XPGiftFreeItemCell *itemCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGiftFreeItemCell class]) forIndexPath:indexPath];
itemCell.giftInfo = giftInfo;
itemCell.freeModel = self.freeModel;
return itemCell;
}
XPGiftItemCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGiftItemCollectionViewCell class]) forIndexPath:indexPath];
cell.usingplaceType = self.usingplaceType;
cell.curUserNobleLevel = self.curUserNobleLevel;
cell.giftInfo = giftInfo;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if (self.datasource.count > 0) {
GiftInfoModel * giftInfo= [self.datasource safeObjectAtIndex1:indexPath.item];
[self dealSelectGift:giftInfo];
[self.giftcollectionView reloadData];
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftInfoView:didClickItem:type:)]) {
[self.delegate xPGiftInfoView:self didClickItem:giftInfo type:self.segmentType];
}
}
}
#pragma mark - XPGiftWeekStarCollectionViewCellDelegate
- (void)xPGiftWeekStarCollectionViewCell:(XPGiftWeekStarCollectionViewCell *)view didSelectGift:(GiftInfoModel *)giftInfo {
[self dealSelectGift:giftInfo];
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftInfoView:didClickItem:type:)]) {
[self.delegate xPGiftInfoView:self didClickItem:giftInfo type:self.segmentType];
}
}
#pragma mark - Getters And Setters
- (void)setNormalOriginArray:(NSArray *)normalOriginArray {
_normalOriginArray = normalOriginArray;
if(_normalOriginArray.count > 0 && self.lastSelectGift == nil){
GiftInfoModel *giftModel = _normalOriginArray.firstObject;
[self dealSelectGift:giftModel];
if (giftModel.giftType == GiftType_super){
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftInfoView:didClickItem:type:)]) {
[self.delegate xPGiftInfoView:self didClickItem:giftModel type:self.segmentType];
}
}
}
self.datasource = [[NSMutableArray alloc]initWithArray:_normalOriginArray];
NSInteger currentPage = 0;
NSInteger page = 0;
if (self.datasource.count % 8 == 0) { //刚好满页
page = self.datasource.count / 8;
} else {
page = self.datasource.count / 8 + 1;
}
self.pageController.hidden = page <= 1 || _segmentType == GiftSegmentType_WeekStar;
[self.pageController setNumberOfPages:page];
self.pageController.currentPage = currentPage;
[self.giftcollectionView reloadData];
}
- (void)setPackOriginArray:(NSArray *)packOriginArray {
_packOriginArray = packOriginArray;
if(_packOriginArray.count > 0 && self.lastSelectGift == nil){
[self dealSelectGift:_packOriginArray.firstObject];
}
if(self.isDelFreeGift == YES){
NSMutableArray *originArray = [NSMutableArray array];
for (GiftInfoModel *giftModel in self.packOriginArray) {
if(giftModel.giftId != self.freeModel.giftId.integerValue){
[originArray addObject:giftModel];
}
}
[self.datasource addObjectsFromArray:originArray];
}else{
[self.datasource addObjectsFromArray:self.packOriginArray];
}
NSInteger currentPage = 0;
NSInteger page = 0;
if (self.datasource.count % 8 == 0) { //刚好满页
page = self.datasource.count / 8;
} else {
page = self.datasource.count / 8 + 1;
}
self.pageController.hidden = page <= 1;
[self.pageController setNumberOfPages:page];
self.pageController.currentPage = currentPage;
[self.giftcollectionView reloadData];
}
- (void)setCurUserNobleLevel:(NSInteger)curUserNobleLevel {
_curUserNobleLevel = curUserNobleLevel;
[self.giftcollectionView reloadData];
}
#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
return self;
}
- (UICollectionView *)giftcollectionView{
if (!_giftcollectionView) {
XPGiftCollectionViewFlowLayout *layout = [[XPGiftCollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 5;
layout.minimumInteritemSpacing = 5;
layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
_giftcollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_giftcollectionView.dataSource = self;
_giftcollectionView.delegate = self;
_giftcollectionView.backgroundColor = [UIColor clearColor];
_giftcollectionView.pagingEnabled = YES;
[_giftcollectionView registerClass:[XPGiftItemCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftItemCollectionViewCell class])];
[_giftcollectionView registerClass:[XPGiftEmptyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftEmptyCollectionViewCell class])];
[_giftcollectionView registerClass:[XPGiftWeekStarCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftWeekStarCollectionViewCell class])];
[_giftcollectionView registerClass:[XPGiftFreeItemCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftFreeItemCell class])];
_giftcollectionView.showsHorizontalScrollIndicator = NO;
}
return _giftcollectionView;
}
- (UIPageControl *)pageController {
if (!_pageController) {
_pageController = [[UIPageControl alloc] init];
_pageController.currentPageIndicatorTintColor = [DJDKMIMOMColor giftPageIndicatorColor];
}
return _pageController;
}
- (NSMutableArray<GiftInfoModel *> *)datasource {
if (!_datasource) {
_datasource = [NSMutableArray array];
}
return _datasource;
}
@end