CNLiveCycleCell.m 6.34 KB
//
//  CNLiveCycleCell.m
//  CNLiveCServiceModule
//
//  Created by CNLive on 2021/11/27.
//

#import "CNLiveCycleCell.h"
#import "CNLiveSearchHotsCell.h"
#import "CNLiveMyServicesVC.h"
#import "CNLiveAllServiceVC.h"
#import "CNLiveServiceModel.h"
#import "CNLiveChineServiceCell.h"
#import "CNLiveServiceOrLookCell.h"

@interface CNLiveCycleCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *listCollectionView;
@property (nonatomic, strong) NSArray *dataSource;
@end

static NSString *HotListCellID = @"HotListCellID";
static NSString *ChineseServiceID = @"CNLiveChineServiceCell";
static NSString *CNServiceOrLookID = @"CNLiveServiceOrLookCell";

@implementation CNLiveCycleCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.listCollectionView];
        [self makeSubViewsLayout];
    }
    return self;
}
-(void)makeSubViewsLayout
{
    [super layoutSubviews];
    [self.listCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.dataSource.count) {
        [CNLiveMyServicesVC myServiceVC];
    }else if (indexPath.row == self.dataSource.count + 1){
        [CNLiveAllServiceVC allServiceVCWithId:@"" title:@"全部服务"];
    }else{
        NSObject *model = self.dataSource[indexPath.row];
        if ([model isKindOfClass:[CNCategoryModel class]]) {
            CNCategoryModel *categoryModel = (CNCategoryModel *)model;
            [CNLiveAllServiceVC allServiceVCWithId:categoryModel.id title:categoryModel.title];
        }
        if ([model isKindOfClass:[CNLiveSearchModel class]]) {
            CNLiveSearchModel *searchModel = (CNLiveSearchModel *)model;
            [CNLiveCServiceManagerBridge jumpServiceController:searchModel.id type:searchModel.type to:searchModel.to shopType:searchModel.shopType controller:[CNLiveCServiceManagerBridge viewControllerFromView:self]];
        }
        if ([model isKindOfClass:[CnLivePageContentModel class]]) {
            [CNLiveCServiceManagerBridge jumpViewControllerType:CNCServiceJumpTypeGetPage param:[model mj_keyValues] controller:[CNLiveCServiceManagerBridge viewControllerFromView:self]];
        }
    }
   
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([self.model.title isEqualToString:@""]) {
        return self.dataSource.count + 2;
    }
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([_model.title isEqualToString:@"中国服务号"]) {
        CNLiveChineServiceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ChineseServiceID forIndexPath:indexPath];
        cell.model = self.dataSource[indexPath.row];
        return cell;
    }else if ([_model.title isEqual:@"大家都在用"]){
        CNLiveServiceOrLookCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNServiceOrLookID forIndexPath:indexPath];
        cell.model = self.dataSource[indexPath.row];
        cell.type = CNLiveCellShowTypeLook;
        return cell;
    }else{
    CNLiveSearchHotsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HotListCellID forIndexPath:indexPath];
    if (indexPath.row < self.dataSource.count) {
            cell.model = self.dataSource[indexPath.row];
        
    }else if (indexPath.row == self.dataSource.count){
        [cell.logoView setImage:[UIImage loadCServiceBundle:@"service_mine"]];
        cell.titleLabel.text = @"我的服务";
        [cell.titleLabel sizeToFit];
        [cell setNeedsLayout];
    }else{
        [cell.logoView setImage:[UIImage loadCServiceBundle:@"service_more"]];
        cell.titleLabel.text = @"更多服务";
        [cell.titleLabel sizeToFit];
        [cell setNeedsLayout];
    }
        
        return cell;
    }
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([_model.title isEqualToString:@"大家都在用"]) {
        return CGSizeMake(collectionView.width, (collectionView.height -40)/3);
    }
    return CGSizeMake(_model.layout.width, _model.layout.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if ([_model.title isEqual:@"中国服务号"]) {
        return UIEdgeInsetsMake(10, 5, 10, 5);
    }
    if ([_model.title isEqual:@""]) {
        return UIEdgeInsetsMake(5, 0, 5, 0);
    }
    if ([_model.title isEqual:@"大家都在用"]) {
        return UIEdgeInsetsMake(15, 0, 0, 0);
    }
    return UIEdgeInsetsMake(15, 15, 0, 15);
}
#pragma mark - Setting&&Getting
-(void)setModel:(CNLiveSearchHotsModel *)model
{
    _model = model;
    self.dataSource = model.lists;
}
- (void)setDataSource:(NSArray *)dataSource
{
    _dataSource = dataSource;
    [self.listCollectionView reloadData];
}
- (UICollectionView *)listCollectionView
{
    if (!_listCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _listCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _listCollectionView.scrollEnabled = NO;
        _listCollectionView.backgroundColor = kWhiteColor;
        _listCollectionView.collectionViewLayout = layout;
        _listCollectionView.delegate = self;
        _listCollectionView.dataSource = self;
        _listCollectionView.backgroundColor = kClearColor;
        _listCollectionView.showsVerticalScrollIndicator = NO;
        [_listCollectionView registerClass:[CNLiveSearchHotsCell class] forCellWithReuseIdentifier:HotListCellID];
        [_listCollectionView registerClass:[CNLiveChineServiceCell class] forCellWithReuseIdentifier:ChineseServiceID];
        [_listCollectionView registerClass:[CNLiveServiceOrLookCell class] forCellWithReuseIdentifier:CNServiceOrLookID];
        
    }
    return _listCollectionView;
}
@end