CNLiveMyServicesVC.m 8.02 KB
//
//  CNLiveMyServicesVC.m
//  CNLiveCServiceModule
//
//  Created by CNLive on 2021/11/26.
//

#import "CNLiveMyServicesVC.h"
#import "CNLiveServicePresent.h"
#import "CNLiveSearchModel.h"
#import "CNLiveSearchHotsCell.h"

@interface CNLiveMyServicesVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) CNLiveNavigationBar *navigationBar;
@property (nonatomic ,strong) NSArray *dataSource;
@property (nonatomic, strong) UICollectionView *listCollectionView;
@property (nonatomic, assign) NSInteger page;
@end

static NSString *HotListCellID = @"HotListCellID";

@implementation CNLiveMyServicesVC
+ (instancetype)myServiceVC
{
    CNLiveMyServicesVC *myService = [[CNLiveMyServicesVC alloc] init];
    [[BaseAppDelegate sharedAppDelegate] pushViewController:myService withBackTitle:@""];
    return myService;
}
- (void)myServiceData{
    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(myServiceData)];
        self.emptyView.frame = CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop);
        [self setCNAPIErrorEmptyView];
        return;
    }
    [QMUITips showLoadingInView:self.view];
    weakself
    [CNLiveServicePresent loadMyServicepage:[NSString stringWithFormat:@"%ld",_page] result:^(NSArray * _Nonnull dataArray, BOOL isLastPage) {
        strongself
        [self.listCollectionView.mj_footer endRefreshing];
        [QMUITips hideAllTipsInView:self.view];
        if (!dataArray||dataArray.count <= 0) {
            if (self.dataSource.count > 0) {
                return;
            }
            self.listCollectionView.mj_footer.hidden = YES;
            self.navigationBar.right.hidden = YES;
            [self showEmptyViewWithImage:[UIImage loadCServiceBundle:@"service_noMsg"] text:@"暂无内容" detailText:nil buttonTitle:NULL buttonAction:NULL];
            self.emptyView.frame = CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop);
            [self setCNAPIErrorEmptyView];
        }else{
            self.listCollectionView.mj_footer.hidden = NO;
            [self removeEmptyView];
            if (isLastPage) {
                [self.listCollectionView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.listCollectionView.mj_footer endRefreshing];
            }
            self.navigationBar.right.hidden = NO;
            NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.dataSource];
            for (id object in dataArray) {
                [array addObject:object];
            }
            self.dataSource = array.copy;
            [self.listCollectionView reloadData];
        }
    } error:^{
        [QMUITips hideAllTipsInView:self.view];
        [self.listCollectionView.mj_footer endRefreshing];
        self.listCollectionView.mj_footer.hidden = YES;
        strongself
        self.navigationBar.right.hidden = YES;
        [self showEmptyViewWithImage:[UIImage loadCServiceBundle:@"service_failure"] text:@"加载失败,请点击重试" detailText:nil buttonTitle:@"     重试     " buttonAction:@selector(myServiceData)];
        self.emptyView.frame = CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop);
        [self setCNAPIErrorEmptyView];
    }];
}
-(void)removeEmptyView
{
    [self.emptyView removeFromSuperview];
    self.emptyView = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.listCollectionView];
    _page = 1;
    [self myServiceData];
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self.listCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navigationBar.mas_bottom);
        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{
    
    CNLiveSearchModel *model1 = self.dataSource[indexPath.row];
    [CNLiveCServiceManagerBridge jumpServiceController:model1.id showAgreement:model1.showAgreement agreementDesc:model1.agreementDesc  type:model1.type to:model1.to shopType:model1.shopType controller:self];
        
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   
        CNLiveSearchHotsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HotListCellID forIndexPath:indexPath];
        cell.model = self.dataSource[indexPath.row];
        return cell;
}
#pragma mark - Getting
- (UICollectionView *)listCollectionView
{
    if (!_listCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((SCREEN_WIDTH-2*15)/4.0, 80);
        layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
        layout.minimumInteritemSpacing = 0;
        _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];
        weakself
        MJRefreshAutoNormalFooter *loadFooter = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            strongself
            self.page ++;
            [self myServiceData];
        }];
        _listCollectionView.mj_footer = loadFooter;
    }
    return _listCollectionView;
}

#pragma mark - Navigation

- (CNLiveNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [CNLiveNavigationBar navigationBar];
        _navigationBar.title.text = @"我的服务";
        weakself
        _navigationBar.onClickLeftButton = ^{
            strongself
            [self.navigationController popViewControllerAnimated:YES];
        };
        [_navigationBar.right setTitleColor:CNLiveColorWithHexString(@"#F5A623") forState:UIControlStateNormal];
        _navigationBar.rightTitle = @"清空";
        _navigationBar.onClickRightButton = ^{
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确认清空所有服务吗?" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [CNLiveServicePresent removeMyServiceResult:^{
                    strongself
                    [self myServiceData];
                }];
            }];
            [alertVC addAction:cancel];
            [alertVC addAction:sure];
            [weakSelf presentViewController:alertVC animated:YES completion:NULL];
        };
        
    }
    return _navigationBar;
}

@end