CNHomeViewController.m
9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// 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{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"cnlive_headerBack"] 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