CNNewsoundBaseViewController.m 12.3 KB
//
//  CNNewsoundBaseViewController.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/20.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNNewsoundBaseViewController.h"
#import "CNNewsoundBaseTableViewCell.h"
#import "CNNewsoundBaseRecommendTableViewCell.h"
#import "CNNewsoundBaseModel.h"

@interface CNNewsoundBaseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) NSMutableArray *topicData;

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

@end

@implementation CNNewsoundBaseViewController
static NSString *newsoundBaseTableViewCell = @"CNNewsoundBaseTableViewCell";
static NSString *newsoundBaseRecommendTableViewCell = @"CNNewsoundBaseRecommendTableViewCell";

#pragma mark - Data
- (void)getData{
    NSString *strUrl = REQUESTURL(@"gxb/api/page");
    NSLog(@"%@",strUrl);
    NSString *josnParameters = [NSString stringWithFormat:@"{\"plat\":\"%@\",\"version\":\"5.0\",\"appid\":\"gxb\",\"pageUUID\":\"%@\",\"pageType\":\"%@\"}",self.plat, self.pageUUID, self.recommendKey];
//    @"params":@"{\"plat\":\"i\",\"version\":\"5.0\",\"appid\":\"gxb\",\"pageUUID\":\"d6262244-b059-11e6-b220-c7d8a7a18cc4\",\"pageType\":\"index\"}"
//    @"params":@"{\"plat\":\"a\",\"version\":\"5.0\",\"appid\":\"gxb\",\"pageUUID\":\"bca5bdd0-259a-11e8-908c-c7d8a7a18cc4\",\"pageType\":\"channelList\"}"
    
    NSLog(@"%@",josnParameters);
    NSDictionary *parameters = @{@"params":josnParameters};
    
    [AlertViewTool showHUDView];
    [XWHTTPTool POST:strUrl params:parameters success:^(id json) {
        self->_isRefreshing = NO;
        [AlertViewTool hideHUDView];
        // 请求成功,解析数据
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:json];
        
        if ([dic[@"errorCode"] isEqualToString:@"0"]) {
            NSDictionary *gxzstj =  [self.latestKey isEqualToString:@"gxzs"]?dic[@"gxzstj"]:dic[@"gxzsnews"];
            if ([gxzstj[@"programs"] isKindOfClass:[NSArray class]]) {
                NSArray *lists = gxzstj[@"programs"];
                for (NSDictionary *dics in lists) {
                    [CNNewsoundBaseModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                        return @{ @"ID" : @"id" ,@"Description" : @"description" };
                    }];
                    NSLog(@"%@",dics);
                    [self.topicData addObject:[CNNewsoundBaseModel mj_objectWithKeyValues:dics]];
                }
            }
            
        } else {
            [AlertViewTool showHUDAddedToView:self.view text:dic[@"errorMessage"]];
            
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        [self.tableView reloadData];
        
        
    } failure:^(NSError *error) {
        [AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"ServerBusy")];
        self->_isRefreshing = NO;
        [AlertViewTool hideHUDView];

    }];
    
}
- (void)loadData{
    NSString *url = REQUESTURL(@"gxb/api/cataloglatest");
    NSLog(@"%@",url);
    NSString *josnParameters = [NSString stringWithFormat:@"{\"plat\":\"i\",\"version\":\"5.0\",\"appid\":\"gxb\",\"categoryId\":\"spzl/%@/\",\"page\":\"%ld\",\"pageSize\":\"%@\"}",self.latestKey,(long)self.pageNo,REQUEST_PAGE_SIZE];
    NSLog(@"%@",josnParameters);
    NSDictionary *parameters = @{@"params":josnParameters};
    
    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
        [AlertViewTool showHUDViewText:CustomStr(@"NetworkiInterruption")];
        return;
    }
    
    [AlertViewTool showHUDView];
    [XWHTTPTool POST:url params:parameters success:^(id json) {
        self->_isRefreshing = NO;
        [AlertViewTool hideHUDView];

        // 请求成功,解析数据
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:json];
        
        if ([dic[@"errorCode"] isEqualToString:@"0"]) {
            self->_pageNo++;
            if ([dic[@"programs"] isKindOfClass:[NSDictionary class]] && [dic[@"programs"] count]) {
                NSDictionary *dics = dic[@"programs"];
                [CNNewsoundBaseModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                    return @{ @"ID" : @"id" ,@"Description" : @"description" };
                }];
                [self.data addObject:[CNNewsoundBaseModel mj_objectWithKeyValues:dics]];
            }
            
            if ([dic[@"programs"] isKindOfClass:[NSArray class]]) {
                NSArray *lists = dic[@"programs"];
                for (NSDictionary *dics in lists) {
                    [CNNewsoundBaseModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                        return @{ @"ID" : @"id" ,@"Description" : @"description" };
                    }];
                    
                    NSLog(@"%@",dics);
                    [self.data addObject:[CNNewsoundBaseModel mj_objectWithKeyValues:dics]];
                    
                }
                
            }
            
            
        } else {
            
            [AlertViewTool showHUDAddedToView:self.view text:dic[@"errorMessage"]];
            
        }
        [self.tableView.mj_header endRefreshing];
        
        if ([dic[@"nextCursor"] isEqualToString:@"0"]) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            
        } else {
            [self.tableView.mj_footer endRefreshing];
            
        }
        [self.tableView reloadData];
        
        
    } failure:^(NSError *error) {
        [AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"ServerBusy")];
        self->_isRefreshing = NO;
        [AlertViewTool hideHUDView];
        
    }];
}

- (void)loadUpData{
    _pageNo = 1;
    if(!_isRefreshing){
        _isRefreshing = YES;
        [self.data removeAllObjects];
        [self loadData];
    }
}
- (void)loadMoreData{
    if(!_isRefreshing){
        _isRefreshing = YES;
        [self loadData];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _pageNo = 1;
    _isRefreshing = NO;
    
    [self getData];
    [self loadData];
    
    [self.view addSubview:self.tableView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#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 - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return 60;
        
    }else if (indexPath.section == 1){
        return 80;

    }
    return 0;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00000001f;
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return self.topicData.count;
        
    }else if (section == 1){
        return self.data.count;

    }
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        CNNewsoundBaseRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsoundBaseRecommendTableViewCell];
        if (!cell) {
            cell = [[CNNewsoundBaseRecommendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newsoundBaseRecommendTableViewCell];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.row = indexPath.row;
        cell.model = self.topicData[indexPath.row];
        return cell;
    }
    CNNewsoundBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsoundBaseTableViewCell];
    if (!cell) {
        cell = [[CNNewsoundBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newsoundBaseTableViewCell];
    }
    cell.model = self.data[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;    
    return cell;

    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth, 40)];
    headerView.backgroundColor = HEXCOLOR(0xeeeeee);

    NSArray *name = @[CustomStr(@"EDITOR'SPICK"), CustomStr(@"Latest")];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, MainScreenWidth, 40)];
    title.numberOfLines = 1;
    title.textColor = HEXCOLOR(0xbbbbbb);
    title.font = [UIFont systemFontOfSize:14];
    title.text = name[section];
    [headerView addSubview:title];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 0.5)];
    line.backgroundColor = HEXCOLOR(0xbbbbff);
    [headerView addSubview:line];
    
    return headerView;
}
- (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 - Action

#pragma mark - Lazy Laoding
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 45 - NavBarHeight - TabBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = HEXCOLOR(0xeeeeee);
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        [_tableView registerClass:[CNNewsoundBaseTableViewCell class] forCellReuseIdentifier:newsoundBaseTableViewCell];
        [_tableView registerClass:[CNNewsoundBaseRecommendTableViewCell class] forCellReuseIdentifier:newsoundBaseRecommendTableViewCell];
        //  设置上拉加载
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadUpData)];
        NSMutableArray *refreshingImages = [NSMutableArray array];
        for (NSUInteger i = 0; i < 4; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"refresh_animation%lu", (unsigned long)i]];
            [refreshingImages addObject:image];
        }
        [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
        header.lastUpdatedTimeLabel.hidden = YES;
        header.stateLabel.hidden = YES;
        _tableView.mj_header = header;

        //  设置下拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        [footer setTitle:CustomStr(@"RefreshBackFooterIdleText") forState:MJRefreshStateIdle];
        [footer setTitle:CustomStr(@"RefreshBackFooterPullingText") forState:MJRefreshStatePulling];
        [footer setTitle:CustomStr(@"RefreshBackFooterRefreshingText") forState:MJRefreshStateRefreshing];
        [footer setTitle:CustomStr(@"RefreshBackFooterNoMoreDataText") forState:MJRefreshStateNoMoreData];
        _tableView.mj_footer = footer;
        
    }
    return _tableView;
}

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

@end