CNHomeViewController.m 10.1 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"

@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]) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"homeList"];
        if (data) {
            [self dealDictionaryData:data];
        }
        [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"]) {
            NSData *data = [dic mj_JSONData];
            [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"homeList"];
            [self dealDictionaryData:data];
            
        } else {
            NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"homeList"];
            if (data) {
                [self dealDictionaryData:data];
            }
            [AlertViewTool hideHUDView];
            [AlertViewTool showHUDViewText:dic[@"errorMessage"]];
//            if (!self->_reFreshBtn &&self.data.count == 0) {
//                [self.view addSubview:self.reFreshBtn];
//            }
        }
        
        
    } failure:^(NSError *error) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"homeList"];
        if (data) {
            [self dealDictionaryData:data];
        }

        [AlertViewTool hideHUDView];
        [AlertViewTool showHUDViewText:CustomStr(@"ServerBusy")];
        if (!self->_reFreshBtn &&self.data.count == 0) {
            [self.view addSubview:self.reFreshBtn];
        }
    }];
}
-(void)dealDictionaryData:(NSData *)data
{
    NSDictionary *dic = [data mj_JSONObject];
    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];
    NSMutableArray *colors = [NSMutableArray array];
    for(int i = 0; i < self.data.count; i++){
        CNHomeModel *model = self.data[i];
        [titles addObject:model.title];
        if (!model.textColor) {
            model.textColor = @"";
        }
        [colors addObject:model.textColor];
        if ([model.cid isEqualToString:@"spzl/djzw/"]) {
            //答记者问
            CNLiveReporterAskedViewController *vc = [[CNLiveReporterAskedViewController alloc] init];
            vc.cid = model.cid;
            [controllers addObject:vc];
            
        }else{
            //其他
            CNLiveHeraldedNewsViewController *vc = [[CNLiveHeraldedNewsViewController alloc] init];
            vc.cid = model.cid;
            [controllers addObject:vc];
        }
        
    }
    if(controllers.count > 0){
        NSMutableArray *textArray = [NSMutableArray array];
        [textArray addObject:titles];
        [textArray addObject:colors];
        self.segmentControl.titles = textArray;
        self.segmentControl.viewControllers = controllers;
    }
    NSDictionary *dic1 = @{@"titles":titles};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CNHomeRightViewController" object:nil userInfo:dic1];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
    [self setUpNavigationBar];
        
}

#pragma mark - UI
- (void)setUpNavigationBar{
//    UIImage *backGroundImage = [UIImage imageNamed:@"cnlive_headerBack"];
//
//    backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
//    [self.navigationController.navigationBar setBackgroundImage:backGroundImage forBarMetrics:UIBarMetricsDefault];
    //左
    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];
    if (HOMETITLECOLORSTR) {
        [headerButton setTitleColor:[UIColor colorWithHexString:HOMETITLECOLORSTR] forState:UIControlStateNormal];
    }else{
    [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