CNHomeViewController.m 9.72 KB
//
//  CNLiveHomeViewController.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/17.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNHomeViewController.h"
#import "XLSlideMenu.h"
#import "CNHomeModel.h"
#import "ZXWKitConst.h"

#import "CNLiveHeraldedNewsViewController.h"
#import "CNLiveReporterAskedViewController.h"

#import "CNHomeBaseViewController.h"

@interface CNHomeViewController ()
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) UIButton *reFreshBtn;

@end

@implementation CNHomeViewController
#pragma mark - Data
- (void)getData{
    NSString *strUrl = REQUESTURL(@"gxb/api/page");

     NSDictionary *parameters = @{@"params":@"{\"plat\":\"i\",\"version\":\"5.0\",\"appid\":\"gxb\",\"pageUUID\":\"852cde69-ecd8-11e6-b52e-c7d8a7a18cc4\",\"pageType\":\"channelList\"}"};
    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
        [AlertViewTool showHUDViewText:CustomStr(@"NetworkiInterruption")];
        if (!_reFreshBtn &&self.data.count == 0) {
            [self.view addSubview:self.reFreshBtn];
        }
        return;
    }
    [AlertViewTool showHUDView];

    [XWHTTPTool POST:strUrl params:parameters success:^(id json) {
        
        if (self->_reFreshBtn) {
            [self->_reFreshBtn removeFromSuperview];
        }
        [AlertViewTool hideHUDView];

        // 请求成功,解析数据
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:json];
        
        if ([dic[@"errorCode"] isEqualToString:@"0"]) {
            
            NSDictionary *ininav = dic[@"ininav"];
            NSArray *ininavArray = ininav[@"programs"];
            
            if ([ininavArray isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dics in ininavArray) {
                    [CNHomeModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                        return @{ @"ID" : @"id" ,@"Description" : @"description" };
                    }];
                    
                    NSLog(@"%@",dics);
                    [self.data addObject:[CNHomeModel mj_objectWithKeyValues:dics]];
                    
                }
                
            }
            
            NSMutableArray *controllers = [NSMutableArray array];
            NSMutableArray *titles = [NSMutableArray array];
            
            for(int i = 0; i < self.data.count; i++){
                CNHomeModel *model = self.data[i];
                [titles addObject:model.title];
                if ([model.cid isEqualToString:@"wz/toutiao/"]) {
                    //外宣要闻
                    CNLiveHeraldedNewsViewController *vc = [[CNLiveHeraldedNewsViewController alloc] init];
                    vc.cid = model.cid;
                    [controllers addObject:vc];
                    
                }else if ([model.cid isEqualToString:@"spzl/djzw/"]) {
                    //答记者问
                    CNLiveReporterAskedViewController *vc = [[CNLiveReporterAskedViewController alloc] init];
                    vc.cid = model.cid;
                    [controllers addObject:vc];
                    
                }else{
                    //基本
                    CNHomeBaseViewController *vc = [[CNHomeBaseViewController alloc] init];
                    vc.cid = model.cid;
                    [controllers addObject:vc];
                    
                }
                
            }
            if(controllers.count > 0){
                self.segmentControl.titles = titles;
                if (HOMETITLECOLORSTR) {
                    self.segmentControl.titleNormalColor = segementColor_title_color;
                    self.segmentControl.titleSelectColor = [UIColor colorWithHexString:HOMETITLECOLORSTR];
                [self.segmentControl setBottomViewColor:[UIColor colorWithHexString:HOMETITLECOLORSTR]];
                }else{
                if (ininavArray[0][@"textColor"]&&![ininavArray[0][@"textColor"] isEqualToString:@""]) {

                    self.segmentControl.titleSelectColor = [UIColor colorWithHexString:ininavArray[0][@"textColor"]];
                    [self.segmentControl setBottomViewColor:[UIColor colorWithHexString:ininavArray[0][@"textColor"]]];
                    }
                }
                self.segmentControl.viewControllers = controllers;
            }
            NSDictionary *dic = @{@"titles":titles};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CNHomeRightViewController" object:nil userInfo:dic];
            
        } else {
            [AlertViewTool hideHUDView];
            [AlertViewTool showHUDViewText:dic[@"errorMessage"]];
            if (!self->_reFreshBtn &&self.data.count == 0) {
                [self.view addSubview:self.reFreshBtn];
            }
        }
        
        
    } failure:^(NSError *error) {
        [AlertViewTool hideHUDView];
        [AlertViewTool showHUDViewText:CustomStr(@"ServerBusy")];
        if (!self->_reFreshBtn &&self.data.count == 0) {
            [self.view addSubview:self.reFreshBtn];
        }
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
    [self setUpNavigationBar];
        
}

#pragma mark - UI
- (void)setUpNavigationBar{

    //左
    UIButton *headerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    headerButton.userInteractionEnabled = NO;
    headerButton.layer.cornerRadius = 2.0;
    headerButton.layer.masksToBounds = true;
    [headerButton setImage:[UIImage imageNamed:@"cnlive_icon"] forState:UIControlStateNormal];
    [headerButton setTitle:@"国新" forState:UIControlStateNormal];
    [headerButton setTitleColor:Color_Blue forState:UIControlStateNormal];
    headerButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    headerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//居左显示
    headerButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headerButton];
    
    //右
    UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 21, 44)];
    [moreButton setImage:[UIImage imageNamed:@"cnlive_classification"] forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(showRight) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:moreButton];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = 15;

    UIButton *languageButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 23, 44)];
    if(IS_CHINESE){
        [languageButton setImage:[UIImage imageNamed:@"cnlive_EN"] forState:UIControlStateNormal];
        [languageButton setImage:[UIImage imageNamed:@"cnlive_CN"] forState:UIControlStateSelected];
    }else{
        [languageButton setImage:[UIImage imageNamed:@"cnlive_CN"] forState:UIControlStateNormal];
        [languageButton setImage:[UIImage imageNamed:@"cnlive_EN"] forState:UIControlStateSelected];
    }
    
    [languageButton addTarget:self action:@selector(chinaENLanguageSwitch:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:languageButton];
    self.navigationItem.rightBarButtonItems = @[item1, space, item2];
}
- (void)chinaENLanguageSwitch:(UIButton *)btn{
    [super chinaENLanguageSwitch:btn];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Action
//右边抽屉
- (void)showRight{
    [self.xl_sldeMenu showRightViewControllerAnimated:true];
}

/*
#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 - Lazy loading
- (UIButton *)reFreshBtn
{
    if (!_reFreshBtn) {
        _reFreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _reFreshBtn.frame = CGRectMake(0, 0, 200, 44);
        _reFreshBtn.center = self.view.center;
        [_reFreshBtn setTitle:@"网络中断了,刷一刷吧~" forState:UIControlStateNormal];
        [_reFreshBtn setTitleColor:Color_Blue forState:UIControlStateNormal];
        _reFreshBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC" size:13];
        [_reFreshBtn addTarget:self action:@selector(getData) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reFreshBtn;
    
}
- (ZXWSegmentControl *)segmentControl{
    if (!_segmentControl) {
        //initStaticTitlesWithFrame
        //initScrollTitlesWithFrame
        if(self.data.count > 4){
            _segmentControl = [[ZXWSegmentControl alloc] initScrollTitlesWithFrame:CGRectMake(0, 0, MainScreenWidth, 45)];

        }else{
            _segmentControl = [[ZXWSegmentControl alloc] initStaticTitlesWithFrame:CGRectMake(0, 0, MainScreenWidth, 45)];

        }
        _segmentControl.backgroundColor = [UIColor whiteColor];
        _segmentControl.titleNormalColor = HEXCOLOR(0x333333);
        _segmentControl.titleSelectColor = Color_Blue;
        [_segmentControl setBottomViewColor:Color_Blue];
        _segmentControl.isTitleScale = YES;
        [self.view addSubview:_segmentControl];
    }
    return _segmentControl;
}
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
    }
    return _data;
}

@end