CNLiveServiceVC.m 12.7 KB
//
//  CNLiveServiceVC.m
//  CNLiveNewTripartiteManagement
//
//  Created by CNLive on 2021/11/22.
//

#import "CNLiveServiceVC.h"
#import "CNLiveMainSearch.h"
#import "CNLiveServiceSearchVC.h"
#import "CNLiveServicePresent.h"
#import "CNLiveServiceModel.h"
#import "CNLiveMyServicesVC.h"
#import "CNLiveSearchHeaderView.h"
#import "CNLiveGoodsOrVideoHeaderView.h"
#import "CNLiveServiceChinasCell.h"
#import "CNLiveServiceEpiCell.h"
#import "CNLiveServiceLookCell.h"
#import "CNLiveRecommendCell.h"
#import "CNLiveVideoRecommendCell.h"
#import "CNLiveCycleActiveCell.h"

static NSString *CNServiceChinaCellID   =   @"CNLiveServiceChinasCell";
static NSString *CNServiceEpiCellID     =   @"CNLiveServiceEpiCell";
static NSString *CNServiceLookCellID    =   @"CNLiveServiceLookCell";
static NSString *CNCycleActiveCellID    =   @"CNLiveCycleActiveCell";
static NSString *CNRecommendCellID      =   @"CNLiveRecommendCell";
static NSString *CNServiceheaderID      =   @"CNLiveSearchHeaderView";
static NSString *CNGoodsOrVideoheaderID =   @"CNLiveGoodsOrVideoHeaderView";

@interface CNLiveServiceVC ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIImageView *topBackView;//头部背景色
@property (nonatomic, strong) CNLiveMainSearch *searchView;//搜索栏目
@property (nonatomic, strong) NSArray *dataSource;//服务号数据
@property (nonatomic, strong) NSArray *pageListSource;
@property (nonatomic, strong) NSArray *hots;
@property (nonatomic, strong) UICollectionView *listCollectionView;

@end

@implementation CNLiveServiceVC
- (void)requestService{
    if (![CNLiveNetworking isNetworking]) {
        if (self.dataSource.count>0 && self.pageListSource.count>0) {
            return;
        }
        [self showEmptyViewWithImage:[UIImage loadCServiceBundle:@"service_noNet"] text:@"网络开小差了,刷新试试" detailText:nil buttonTitle:@"     刷新     " buttonAction:@selector(requestService)];
        self.emptyView.frame = CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop);
        [self setCNAPIErrorEmptyView];
        return;
    }
    dispatch_group_t requestGroup = dispatch_group_create();
    dispatch_group_enter(requestGroup);
    weakself
    [CNLiveServicePresent requestServiceListResult:^(NSArray * _Nonnull dataArray, NSArray * _Nonnull hots) {
        strongself
        [self removeEmptyView];
        self.dataSource = dataArray;
        self.hots = hots;
        dispatch_group_leave(requestGroup);
    } error:^{
        
        strongself
        if (self.dataSource.count>0 && self.pageListSource.count>0) {
            return;
        }
        [self showEmptyViewWithImage:[UIImage loadCServiceBundle:@"service_failure"] text:@"加载失败,请点击重试" detailText:nil buttonTitle:@"     重试     " buttonAction:@selector(requestService)];
        self.emptyView.frame = CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop);
        [self setCNAPIErrorEmptyView];
        dispatch_group_leave(requestGroup);
    }];
    dispatch_group_enter(requestGroup);
    [CNLiveServicePresent requesrServiceGetPageResult:^(NSArray * _Nonnull dataArray) {
        weakSelf.pageListSource = dataArray;
        dispatch_group_leave(requestGroup);
    } error:^{
        
        strongself
        if (self.dataSource.count>0 && self.pageListSource.count>0) {
            return;
        }
        [self showEmptyViewWithImage:[UIImage loadCServiceBundle:@"service_failure"] text:@"加载失败,请点击重试" detailText:nil buttonTitle:@"     重试     " buttonAction:@selector(requestService)];
        self.emptyView.frame = CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop);
        [self setCNAPIErrorEmptyView];
        dispatch_group_leave(requestGroup);
    }];
    dispatch_group_notify(requestGroup, dispatch_get_main_queue(), ^{
        strongself
        if (self.dataSource.count>0&&self.pageListSource.count>0) {
            [self.listCollectionView reloadData];
        }
        
    });
}
-(void)removeEmptyView
{
    [self.emptyView removeFromSuperview];
    self.emptyView = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.topBackView];
    [self.view sendSubviewToBack:self.topBackView];
    [self.view addSubview:self.searchView];
    [self.view addSubview:self.listCollectionView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [self requestService];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self.topBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.offset(159.5 *KScreenHeight / 667);
    }];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.mas_topLayoutGuide > 0) {
            make.top.equalTo(self.mas_topLayoutGuide);
        }else{
        make.top.equalTo(self.view).offset(35.5);
        }
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.height.offset(36);
    }];
    [self.listCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.searchView.mas_bottom).offset(10);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];
}
#pragma mark - Action
- (void)seachPushTap
{
    [CNLiveServiceSearchVC serviceSearchVC:self.hots];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section < self.dataSource.count) {
        CNLiveSearchHotsModel *model = self.dataSource[section];
        return model.lists.count;
    }else {
        return 1;
    }
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.dataSource.count + self.pageListSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section < self.dataSource.count) {
        CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
        CNLiveServiceChinasCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNServiceChinaCellID forIndexPath:indexPath];
        cell.model = model.lists[indexPath.row];
        return cell;
    }else{
        CNliveServicePageModel *model = self.pageListSource[indexPath.section - self.dataSource.count];
        if ([model.blockName isEqualToString:@"疫情服务专区"]){
            CNLiveServiceEpiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNServiceEpiCellID forIndexPath:indexPath];
            cell.model = model;
            return cell;
        }else if ([model.blockName isEqualToString:@"大家都在用"]){
            CNLiveServiceLookCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNServiceLookCellID forIndexPath:indexPath];
            cell.model = model;
            return cell;
        }else if ([model.blockName isEqualToString:@"轮播"]){
            CNLiveCycleActiveCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNCycleActiveCellID forIndexPath:indexPath];
            cell.model = model;
            return cell;
        }
        else{
            CNLiveRecommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNRecommendCellID forIndexPath:indexPath];
            cell.model = model;
            return cell;
        }
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section < self.dataSource.count) {
        CNLiveSearchHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:CNServiceheaderID forIndexPath:indexPath];
        CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
        header.type = CNServiceHeaderTypeService;
        header.model = model.lists[indexPath.row];
        return header;
    }else{
        CNLiveGoodsOrVideoHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:CNGoodsOrVideoheaderID forIndexPath:indexPath];
        CNliveServicePageModel *model = self.pageListSource[indexPath.section - self.dataSource.count];
        header.pageModel = model;
        return header;
    }
    
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section < self.dataSource.count) {
        CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
        return CGSizeMake(collectionView.width, model.cellHeight);
    }else{
        CNliveServicePageModel *model = self.pageListSource[indexPath.section - self.dataSource.count];
        return CGSizeMake(collectionView.width, model.cellHeight);
    }
   
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 10, 0);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section < self.dataSource.count) {
        CNLiveSearchHotsModel *model = self.dataSource[section];
        if ([model.title isEqualToString:@""]) {
            return CGSizeMake(0, 0);
        }
    }else {
        CNliveServicePageModel *model = self.pageListSource[section - self.dataSource.count];
        if ([model.blockName isEqualToString:@"轮播"] ||[model.blockName isEqualToString:@"疫情服务专区"]||[model.blockName isEqualToString:@"大家都在用"]) {
            return CGSizeMake(0, 0);
        }
    }
    
    return CGSizeMake(collectionView.width, 40);
}
#pragma mark - Setting&&Getting

- (UIImageView *)topBackView
{
    if (!_topBackView) {
        _topBackView = [[UIImageView alloc] init];
        [_topBackView setImage:[UIImage loadCServiceBundle:@"service_top"]];
        
    }
    return _topBackView;
}
- (CNLiveMainSearch *)searchView
{
    if (!_searchView) {
        _searchView = [[CNLiveMainSearch alloc] initWithType:CNSearchViewShowTypePush];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seachPushTap)];
        [_searchView addGestureRecognizer:tap];
    }
    return _searchView;
}
- (UICollectionView *)listCollectionView
{
    if (!_listCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _listCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _listCollectionView.backgroundColor = kWhiteColor;
        _listCollectionView.collectionViewLayout = layout;
        _listCollectionView.delegate = self;
        _listCollectionView.dataSource = self;
        _listCollectionView.backgroundColor = kClearColor;
        _listCollectionView.showsVerticalScrollIndicator = NO;
        [_listCollectionView registerClass:[CNLiveServiceChinasCell class] forCellWithReuseIdentifier:CNServiceChinaCellID];
        [_listCollectionView registerClass:[CNLiveServiceEpiCell class] forCellWithReuseIdentifier:CNServiceEpiCellID];
        [_listCollectionView registerClass:[CNLiveServiceLookCell class] forCellWithReuseIdentifier:CNServiceLookCellID];
        [_listCollectionView registerClass:[CNLiveCycleActiveCell class] forCellWithReuseIdentifier:CNCycleActiveCellID];
        [_listCollectionView registerClass:[CNLiveRecommendCell class] forCellWithReuseIdentifier:CNRecommendCellID];
        [_listCollectionView registerClass:[CNLiveSearchHeaderView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:CNServiceheaderID];
        [_listCollectionView registerClass:[CNLiveGoodsOrVideoHeaderView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:CNGoodsOrVideoheaderID];
    }
    return _listCollectionView;
}
/*
#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