CNHomeENViewController.m 16.1 KB
//
//  CNHomeENViewController.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/27.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNHomeENViewController.h"
#import "CNHomeModel.h"

#import "CNLiveNewsNoPicTableViewCell.h"
#import "CNLiveNewsThreePicTableViewCell.h"
#import "CNLiveNewsLeftPicTableViewCell.h"
#import "CNLiveNewsUpPicTableViewCell.h"
#import "NoDataView.h"

@interface CNHomeENViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) BOOL isRefreshing;
@property (nonatomic, strong) NoDataView *noDataView;
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) NSMutableArray *banner;

@end

@implementation CNHomeENViewController
static NSString *newsNoPicTableViewCell = @"CNLiveNewsNoPicTableViewCell";
static NSString *newsThreePicTableViewCell = @"CNLiveNewsThreePicTableViewCell";
static NSString *newsLeftPicTableViewCell = @"CNLiveNewsLeftPicTableViewCell";
static NSString *newsUpPicTableViewCell = @"CNLiveNewsUpPicTableViewCell";

#pragma mark - Data
- (void)loadData{
    NSString *strUrl = REQUESTURL(@"gxb/api/cataloglatest");
    NSString *categoryId = [NSString stringWithFormat:@"{\"plat\":\"i\",\"version\":\"5.0\",\"appid\":\"38_ivkou3xi05\",\"categoryId\":\"%@\",\"page\":\"%ld\",\"pageSize\":\"10\"}",@"wz/scio/",(long)self.pageNo];

    NSDictionary *parameters = @{@"params":categoryId};
    NSLog(@"%@",parameters);

    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
        [AlertViewTool showHUDViewText:CustomStr(@"NetworkiInterruption")];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        self->_isRefreshing = NO;
        return;
    }
    
    [AlertViewTool showHUDView];
    [XWHTTPTool POST:strUrl params:parameters success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [AlertViewTool hideHUDView];
        
        // 请求成功,解析数据
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:json];
        
        if ([dic[@"errorCode"] isEqualToString:@"0"]) {
            if ([dic[@"programs"] isKindOfClass:[NSArray class]]) {
                
                NSArray *lists = dic[@"programs"];
                if(self->_pageNo == 1&&lists.count > 0){
                    [self.data removeAllObjects];
                    
                }
                self->_pageNo++;
                for (NSDictionary *dics in lists) {
                    [CNHomeModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                        return @{ @"ID" : @"id" ,@"Description" : @"description" };
                    }];
                    NSLog(@"%@",dics);
                    [self.data addObject:[CNHomeModel mj_objectWithKeyValues:dics]];
                    
                }
            
            }
            NSDictionary *ttName = dic[@"ttName"];
            NSArray *programs = ttName[@"programs"];

            if ([programs isKindOfClass:[NSArray class]]) {
                [self.banner removeAllObjects];
                for (NSDictionary *dics in programs) {
                    [CNHomeModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                        return @{ @"ID" : @"id" ,@"Description" : @"description" };
                    }];
                    
                    NSLog(@"%@",dics);
                    [self.banner addObject:[CNHomeModel mj_objectWithKeyValues:dics]];
                }
                
                NSMutableArray *titles = [NSMutableArray array];
                NSMutableArray *imagesURLStrings = [NSMutableArray array];
                for(int i = 0; i < self.banner.count; i++){
                    CNHomeModel *model = self.banner[i];
                    [titles addObject:model.title];
                    [imagesURLStrings addObject:model.img];

                }
                self.cycleScrollView.titlesGroup = titles;
                self.cycleScrollView.imageURLStringsGroup = imagesURLStrings;
                
            }
            
        } else {
            [AlertViewTool showHUDViewText: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];
        self->_isRefreshing = NO;

        
    } failure:^(NSError *error) {
        [AlertViewTool hideHUDView];
        self->_isRefreshing = NO;
        [AlertViewTool showHUDViewText:CustomStr(@"ServerBusy")];
        
    }];
}

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

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    
    self.cid = @"gxsj/";
    self.name = @"NEWS";
    _pageNo = 1;
    _isRefreshing = NO;
    
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.cycleScrollView;

    [self loadData];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CNHomeModel *model = self.data[indexPath.row];
    if([model.showType isEqualToString:@"0"]){
        return 200*MainHScale+85;//一张图 上边
        
    }
    else if([model.showType isEqualToString:@"1"]){
        if(model.img && ![model.img isEqualToString:@""]){
            return 80*MainScale+20;//一张图 左边
        }
//        return model.titleHeight+40;//无图
        return 80*MainScale+20;//一张图 左边

    }
    else if([model.showType isEqualToString:@"2"]){
        return (MainScreenWidth-20-20)/3.0+85;//三张图
        
    }
    else if([model.showType isEqualToString:@"3"]){
        if(model.img && ![model.img isEqualToString:@""]){
            return 80*MainScale+20;//一张图 左边
        }
//        return model.titleHeight+40;//无图
        return 80*MainScale+20;//一张图 左边
        
    }else{
//        return model.titleHeight+40;//无图
        return 80*MainScale+20;//一张图 左边
        
    }
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CNHomeModel *model = self.data[indexPath.row];
    if([model.showType isEqualToString:@"0"]){
        //一张图 上边
        CNLiveNewsUpPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsUpPicTableViewCell];
        if (!cell) {
            cell = [[CNLiveNewsUpPicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newsUpPicTableViewCell];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.data[indexPath.row];
        return cell;
        
    }
    else if([model.showType isEqualToString:@"2"]){
        //三张图
        CNLiveNewsThreePicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsThreePicTableViewCell];
        if (!cell) {
            cell = [[CNLiveNewsThreePicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newsThreePicTableViewCell];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.data[indexPath.row];
        return cell;
        
    }
    else{
        if(model.img){
            //一张图 左边
            CNLiveNewsLeftPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsLeftPicTableViewCell];
            if (!cell) {
                cell = [[CNLiveNewsLeftPicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newsLeftPicTableViewCell];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.data[indexPath.row];
            return cell;
        }
        //无图
        CNLiveNewsNoPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsNoPicTableViewCell];
        if (!cell) {
            cell = [[CNLiveNewsNoPicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newsNoPicTableViewCell];
        }
//        CNLiveNewsLeftPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsLeftPicTableViewCell];
//        if (!cell) {
//            cell = [[CNLiveNewsLeftPicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newsLeftPicTableViewCell];
//        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.data[indexPath.row];
        return cell;
        
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CNHomeModel *model = self.data[indexPath.row];
    [self pushViewControllerWithModel:model];
}
#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    CNHomeModel *model = self.banner[index];
    [self pushViewControllerWithModel:model];
}
/*
 #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
- (void)pushViewControllerWithModel:(CNHomeModel *)model {
    NSDictionary *_dic = [model toDictionary];
    if ([@"program" isEqualToString:_dic[@"type"]]) {
        //视频
        OldVideoDetailViewController *vc = [[OldVideoDetailViewController alloc] initWithDictionary:_dic];
        vc.title = _dic[@"cmsColumnName"];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    } else if ([@"article" isEqualToString:_dic[@"type"]]) {
        //文章
        OldArticleDetailViewController *vc = [[OldArticleDetailViewController alloc] initWithDictionary:_dic];
        vc.title = _dic[@"cmsColumnName"];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    } else if ([@"subject" isEqualToString:_dic[@"type"]]) {
        SCIOSpecialViewController *vc = [[SCIOSpecialViewController alloc] initWithDictionary:_dic];
        vc.title = @"国新专题";
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    } else if ([@"LK" isEqualToString:_dic[@"type"]]) {
        OldArticleDetailViewController *vc = [[OldArticleDetailViewController alloc] initWithDictionary:_dic];
        vc.title = _dic[@"cmsColumnName"];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }

}
#pragma mark - Lazy Laoding
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight-NavBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = HEXCOLOR(0xebebeb);
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        [_tableView registerClass:[CNLiveNewsLeftPicTableViewCell class] forCellReuseIdentifier:newsLeftPicTableViewCell];
        [_tableView registerClass:[CNLiveNewsNoPicTableViewCell class] forCellReuseIdentifier:newsNoPicTableViewCell];
        [_tableView registerClass:[CNLiveNewsUpPicTableViewCell class] forCellReuseIdentifier:newsUpPicTableViewCell];
        [_tableView registerClass:[CNLiveNewsThreePicTableViewCell class] forCellReuseIdentifier:newsThreePicTableViewCell];
        
        //  设置上拉加载
        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];
//        "MJRefreshHeaderIdleText" = "下拉可以刷新";
//        "MJRefreshHeaderPullingText" = "松开立即刷新";
//        "MJRefreshHeaderRefreshingText" = "正在刷新数据中...";
        
//        "MJRefreshAutoFooterIdleText" = "点击或上拉加载更多";
//        "MJRefreshAutoFooterRefreshingText" = "正在加载更多的数据...";
//        "MJRefreshAutoFooterNoMoreDataText" = "已经全部加载完毕";
//
//        "MJRefreshBackFooterIdleText" = "上拉可以加载更多";
//        "MJRefreshBackFooterPullingText" = "松开立即加载更多";
//        "MJRefreshBackFooterRefreshingText" = "正在加载更多的数据...";
//        "MJRefreshBackFooterNoMoreDataText" = "已经全部加载完毕";
        _tableView.mj_footer = footer;
        
    }
    return _tableView;
}

- (NSMutableArray *)data{
    if(!_data){
        _data = [[NSMutableArray alloc] init];
    }
    return _data;
}
- (NoDataView *)noDataView{
    if (!_noDataView) {
        _noDataView = [[NoDataView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight-NavBarHeight-TabBarHeight)];
        _noDataView.title = CustomStr(@"NoData");
        _noDataView.btnTitle = @"重新加载";
        _noDataView.backgroundColor = [UIColor whiteColor];
    }
    return _noDataView;
}

- (SDCycleScrollView *)cycleScrollView{
    
    if(!_cycleScrollView){

        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenWidth*9/16.0) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
        _cycleScrollView.titleLabelTextFont = [UIFont systemFontOfSize:15];
        _cycleScrollView.titleLabelHeight = 40;
        _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _cycleScrollView.titleLabelTextColor = [UIColor whiteColor];
        _cycleScrollView.titleLabelBackgroundColor = [UIColor clearColor];
        _cycleScrollView.titleLabelTextAlignment = NSTextAlignmentLeft;
        _cycleScrollView.delegate = self;
    }
    return _cycleScrollView;
    
}
- (NSMutableArray *)banner{
    if(!_banner){
        _banner = [[NSMutableArray alloc] init];
    }
    return _banner;
}
@end