CNLiveIntegralDetailsController.m 7.89 KB
//
//  CNLiveIntegralDetailsController.m
//  CNLiveIntegralModule
//
//  Created by CNLive-zxw on 2019/3/1.
//  Copyright © 2019年 cnlive. All rights reserved.
//

#import "CNLiveIntegralDetailsController.h"

#import "QMUIKit.h"
#import "MJRefresh.h"
#import "MJExtension.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>

#import "CNLiveNetworking.h"
#import "CNLiveEnvironment.h"
#import "CNUserInfoManager.h"
#import "CNLiveUserAgreementManager.h"

#import "CNLiveIntegralDetailsTableViewCell.h"
#import "CNLiveScoreModel.h"
#import "CNLiveIntegralNavigationBar.h"

@interface CNLiveIntegralDetailsController ()<UITableViewDelegate,UITableViewDataSource,CNLiveIntegralNavigationBarDelegate>

@property (nonatomic, strong) CNLiveIntegralNavigationBar *navigationBar;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) BOOL isRefreshing;

@end

@implementation CNLiveIntegralDetailsController

#pragma mark - Data
- (void)loadData{
//    NSDictionary *params = @{@"pageSize":@"10",
//                             @"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],
//                             @"sid":CNUserShareModel.uid};
    NSDictionary *params = @{@"pageSize":@"15",
                                @"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],
                                @"sid":@"10514405",@"appId":AppId};
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking setupShowDataResult:NO];
    [QMUITips showLoadingInView:[UIApplication sharedApplication].keyWindow];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:CNIntegralDetailsUrl Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        [QMUITips hideAllTipsInView:[UIApplication sharedApplication].keyWindow];
        [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;
        }
        if ([responseObject isKindOfClass:[NSDictionary class]] && [responseObject[@"errorCode"] isEqualToString:@"0"]) {
            _isRefreshing = NO;
            _pageNo++;
            NSArray *arr = [CNLiveScoreModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            [self.data addObjectsFromArray:arr];
            if (arr.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                [self.tableView.mj_footer endRefreshing];
                
            }
        }
        self.tableView.mj_footer.hidden = self.data.count == 0;
        [self.tableView reloadData];
        
        if (self.data.count == 0) {//显示empty
//            [self showEmptyViewWithImage:[UIImage imageNamed:@"zhuangtai_kong"] text:@"您现在没有相关内容哦" detailText:@"" buttonTitle:@"" buttonAction:nil];
//            [self setCNNoDataEmptyView];
            self.tableView.tableFooterView = nil;
        }
    }];
    
}

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

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

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _pageNo = 1;
    _isRefreshing = NO;
    
    [self loadData];
    [self createUI];
    
}

#pragma mark - UI
- (void)createUI {
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.tableView];
    
}

/*
 #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.
 }
 */
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CNLiveIntegralDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCNLiveIntegralDetailsTableViewCell forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.data[indexPath.row];
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30+40;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
}

/*
 #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.
 }
 */

#pragma mark - CNLiveIntegralNavigationBarDelegate
- (void)navigationBar:(CNLiveIntegralNavigationBar *)navigationBar actionEvents:(NSString *)actionEvents{
    if([actionEvents isEqualToString:@"1"]){
        [self.navigationController popViewControllerAnimated:YES];
    }else if([actionEvents isEqualToString:@"2"]){
        [CNLiveUserAgreementManager jumpToAgreementH5WithType:CNLiveUserAgreementIntegralRules];
    }
}

#pragma mark - Lazy loading
- (CNLiveIntegralNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [[CNLiveIntegralNavigationBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationContentTop)];
        _navigationBar.backgroundColor = [UIColor whiteColor];
        _navigationBar.delegate = self;
        _navigationBar.title.text = @"积分明细";
        [_navigationBar.right setTitle:@"积分规则" forState:UIControlStateNormal];
        [_navigationBar.right setTitleColor:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] forState:UIControlStateNormal];
        _navigationBar.right.titleLabel.font = [UIFont systemFontOfSize:16.0];
        _navigationBar.right.frame = CGRectMake(SCREEN_WIDTH-80-15, StatusBarHeight, 80, NavigationBarHeight);
    }
    return _navigationBar;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[CNLiveIntegralDetailsTableViewCell class] forCellReuseIdentifier:kCNLiveIntegralDetailsTableViewCell];
        __weak __typeof(self)weakSelf = self;
        MJRefreshAutoNormalFooter *refreshView = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf loadMoreData];
        }];
        _tableView.mj_footer = refreshView;
    }
    return _tableView;
}

- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
    }
    return _data;
}

- (void)dealloc{
    
}

@end