CNLiveAllServiceVC.m 10.6 KB
//
//  CNLiveAllServiceVC.m
//  CNLiveCServiceModule
//
//  Created by CNLive on 2021/11/29.
//

#import "CNLiveAllServiceVC.h"
#import "CNLiveServiceSearchVC.h"
#import "CNLiveSearchHotsCell.h"
#import "CNLiveSearchHeaderView.h"
#import "CNLiveServicePresent.h"
#import "CNLIveCategoryCell.h"

static NSString *HotListCellID       =   @"CNLiveSearchHotsCell";
static NSString *HotHeaderID         =   @"CNLiveSearchHeaderView";
static NSString *CNCategoryCellID    =   @"CNLIveCategoryCell";
static NSString *HistoryFootID       =   @"UICollectionFooterView";

@interface CNLiveAllServiceVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) CNLiveNavigationBar *navigationBar;
@property (nonatomic, strong) UICollectionView *listCollectionView;//热搜/搜索历史列表
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSString *serviceId;
@property (nonatomic, assign) NSInteger page;

@end

@implementation CNLiveAllServiceVC

+ (instancetype)allServiceVCWithId:(NSString *)serviceId title:(nonnull NSString *)title
{
    CNLiveAllServiceVC *vc = [[CNLiveAllServiceVC alloc] initWithServiceId:serviceId];
        vc.title = title;
    [[BaseAppDelegate sharedAppDelegate] pushViewController:vc withBackTitle:@""];
    return vc;
}
- (void)requestService{
    if (self.page == 1) {
        self.dataSource = @[];
        [self.listCollectionView reloadData];
    }
    if (![CNLiveNetworking isNetworking]) {
        self.listCollectionView.mj_footer.hidden = YES;
        if (self.dataSource.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;
    }
    [QMUITips showLoadingInView:self.view];
    weakself
    [CNLiveServicePresent loadAllServiceWithServiceId:_serviceId page:[NSString stringWithFormat:@"%ld",_page] result:^(NSArray * _Nonnull dataArray, BOOL isLastPage) {
        strongself
        [QMUITips hideAllTipsInView:self.view];
        [self.listCollectionView.mj_footer endRefreshing];
        self.listCollectionView.mj_footer.hidden = NO;
        
        if (isLastPage) {
            [self.listCollectionView.mj_footer endRefreshingWithNoMoreData];
        }
        [self removeEmptyView];
        NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.dataSource];
        for (id object in dataArray) {
            [array addObject:object];
        }
        
        self.dataSource = array.copy;
        [self.listCollectionView reloadData];
    }error:^{
        strongself
        [QMUITips hideAllTipsInView:self.view];
        if (self.dataSource.count > 0) {
            return;
        }
        [self.listCollectionView.mj_footer endRefreshing];
        self.listCollectionView.mj_footer.hidden = YES;
        [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];
    }];
}
-(void)removeEmptyView
{
    [self.emptyView removeFromSuperview];
    self.emptyView = nil;
}
- (instancetype)initWithServiceId:(NSString *)serviceId
{
    self = [super init];
    if (self) {
        _serviceId = serviceId;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self requestService];
    self.view.backgroundColor = kWhiteColor;
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.listCollectionView];
    // Do any additional setup after loading the view.
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self.listCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navigationBar.mas_bottom).offset(0);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
    CNLiveSearchModel *model1 = [CNLiveSearchModel mj_objectWithKeyValues:model.lists[indexPath.row]];
    [CNLiveServicePresent usedServiceWithServingId:model1.id];
    [CNLiveCServiceManagerBridge jumpServiceController:model1.id type:model1.type to:model1.to shopType:model1.shopType controller:self];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    CNLiveSearchHotsModel *model = self.dataSource[section];
    return model.lists.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.dataSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
    if (_serviceId &&![_serviceId isEqualToString:@""]&&indexPath.section != 0){
        CNLIveCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNCategoryCellID forIndexPath:indexPath];
        cell.model = [CNLiveSearchModel mj_objectWithKeyValues:model.lists[indexPath.row]];
        return cell;
    }else{
        CNLiveSearchHotsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HotListCellID forIndexPath:indexPath];
        cell.model = [CNLiveSearchModel mj_objectWithKeyValues:model.lists[indexPath.row]];
        return cell;
    }
   
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:@"UICollectionElementKindSectionHeader"]) {
        CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
        CNLiveSearchHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HotHeaderID forIndexPath:indexPath];
        if (_serviceId &&![_serviceId isEqualToString:@""]&&indexPath.section == 0) {
            header.type = CNServiceHeaderTypeCategoryId;
        }else{
            header.type = CNServiceHeaderTypeService;
        }
        header.model = model;
        return header;
    }else
    {
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HistoryFootID forIndexPath:indexPath];
        footer.backgroundColor = CNLiveColorWithHexString(@"#F5F5F5");
        return footer;
    }
    
    
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveSearchHotsModel *model = self.dataSource[indexPath.section];
    if (_serviceId &&![_serviceId isEqualToString:@""]&&indexPath.section != 0){
        return CGSizeMake(collectionView.width, model.layout.height);
    }
    return CGSizeMake(model.layout.width, model.layout.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(5, 15, 10, 15);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(collectionView.width, 60);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(self.view.width, 5);
}
#pragma mark - Setting&&Getting
- (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.showsVerticalScrollIndicator = NO;
        [_listCollectionView registerClass:[CNLiveSearchHotsCell class] forCellWithReuseIdentifier:HotListCellID];
        [_listCollectionView registerClass:[CNLiveSearchHeaderView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:HotHeaderID];
        [_listCollectionView registerClass:[CNLIveCategoryCell class] forCellWithReuseIdentifier:CNCategoryCellID];
        [_listCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionFooter" withReuseIdentifier:HistoryFootID];
        weakself
        MJRefreshAutoNormalFooter *loadFooter = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            strongself
            self.page ++;
            [self requestService];
        }];
        _listCollectionView.mj_footer = loadFooter;
    }
    return _listCollectionView;
}
#pragma mark - Navigation

- (CNLiveNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [CNLiveNavigationBar navigationBar];
        _navigationBar.title.text = self.title;
        weakself
        _navigationBar.onClickLeftButton = ^{
            strongself
            [self.navigationController popViewControllerAnimated:YES];
        };
        if ([self.title isEqualToString:@"全部服务"]) {
            [_navigationBar.right setTitleColor:CNLiveColorWithHexString(@"#F5A623") forState:UIControlStateNormal];
            _navigationBar.rightImage = [UIImage loadCServiceBundle:@"service_search"];
            _navigationBar.onClickRightButton = ^{
                [CNLiveServiceSearchVC serviceSearchVC:nil];
            };
        }
    }
    return _navigationBar;
}


@end