CNLiveCollectionController.m 7.18 KB
//
//  CNLiveCollectionController.m
//  CNLiveCollectionModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright (c) 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveCollectionController.h"
#import "QMUIKit.h"
#import "MJExtension.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import "CNLiveCategory.h"
#import "CNLiveNetworking.h"
#import "CNLiveEnvironment.h"
#import "CNUserInfoManager.h"

#import "CNLiveRefreshHeader.h"
#import "CNLiveRrfreshFooter.h"

#import "CNLiveCollectionNavigationBar.h"
#import "CNLiveCollectionCell.h"
#import "CNLiveCollectionModel.h"


@interface CNLiveCollectionController ()<UITableViewDelegate, UITableViewDataSource,CNLiveCollectionNavigationBarDelegate>
@property (nonatomic, strong) CNLiveCollectionNavigationBar *navigationBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;

@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) BOOL isRefreshing;

@end

@implementation CNLiveCollectionController
#pragma mark - 数据
- (void)loadData{
    NSDictionary *params = @{
                             @"pageSize":@"10",
                             @"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],
                             @"appId":AppId,
                             @"plat":@"i",
                             @"sid":CNUserShareModel.uid};
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [QMUITips showLoadingInView:self.view];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:CNDarenCollectListDataSourceUrl Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        if(_pageNo == 1 && self.data.count > 0){
            [self.data removeAllObjects];
        }
        [QMUITips hideAllTipsInView:self.view];
        [self hideEmptyView];
        
        if (error) {
            if (self.data.count == 0) {
                if (error.code == -1001||error.code == -1003||error.code == -1009) {
                    [self showEmptyViewWithImage:[UIImage imageNamed:@"dd_wlcw"] text:@"无法连接网络" detailText:@"别紧张,试试看刷新页面~" buttonTitle:@"     重试     " buttonAction:@selector(loadData)];
                    [self setCNAPIErrorEmptyView];
                    self.tableView.mj_footer.hidden = self.data.count == 0;
                }
            }
            return;
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject isKindOfClass:[NSDictionary class]] && [responseObject[@"errorCode"] isEqualToString:@"0"]) {
            _isRefreshing = NO;
            _pageNo++;
            NSArray *arr = [CNLiveCollectionModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            [self.data addObjectsFromArray:arr];
            if (arr.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                [self.tableView.mj_footer endRefreshing];
                
            }
        }

        if (self.data.count == 0) {//显示empty
//            [self showEmptyViewWithImage:[UIImage imageNamed:@"zhuangtai_kong"] text:@"您现在没有相关内容哦" detailText:@"" buttonTitle:@"" buttonAction:nil];
            [self setCNNoDataEmptyView];
            self.tableView.tableFooterView = nil;
        }
        self.tableView.mj_footer.hidden = self.data.count == 0;
        [self.tableView reloadData];

    }];
    
}

- (void)loadMoreData{
    if(!_isRefreshing){
        _isRefreshing = YES;
        [self loadData];
    }
}

#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11, *)) {
    }
    else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _pageNo = 1;
    _isRefreshing = NO;
    [self loadData];
    [self createSubViews];    
}

- (void)dealloc {
    NSLog(@"CNLiveCollectionController -- dealloc");
    
}

- (void)createSubViews {
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.tableView];
}

#pragma mark - 响应方法
- (void)goBack {
    if (self.presentingViewController != nil) {
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveCollectionModel *model = self.data[indexPath.row];
    return model.height;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CNLiveCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:kCNLiveCollectionCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CNLiveCollectionModel *model = self.data[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)navigationBar:(CNLiveCollectionNavigationBar *)navigationBar actionEvents:(NSString *)actionEvents{
    if ([actionEvents isEqualToString:@"1"]) {
        [self goBack];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)data {
    if (!_data) {
        _data = [NSMutableArray array];
    }
    return _data;
}
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[CNLiveCollectionCell class] forCellReuseIdentifier:kCNLiveCollectionCell];
        _tableView.mj_header = [CNLiveRefreshHeader headerWithRefreshingBlock:^{
            _pageNo = 1;
            [self loadData];

        }];
        CNLiveRrfreshFooter *footer = [CNLiveRrfreshFooter footerWithRefreshingBlock:^{
            [self loadMoreData];
        }];
        footer.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
        _tableView.mj_footer = footer;
    }
    return _tableView;
}
- (CNLiveCollectionNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [[CNLiveCollectionNavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationContentTop)];
        _navigationBar.delegate = self;
        _navigationBar.title.text = @"我的收藏";
    }
    return _navigationBar;
}

@end