-
Notifications
You must be signed in to change notification settings - Fork 0
/
WAHourlyForecastTVC.m
300 lines (227 loc) · 10.6 KB
/
WAHourlyForecastTVC.m
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// WADailyForecastTVC.m
// Weather
//
// Created by Mitchell Cooper on 3/22/14.
// Copyright (c) 2014 Mitchell Cooper. All rights reserved.
//
#import "WAHourlyForecastTVC.h"
#import "WALocation.h"
#import "WALocationListTVC.h"
#import "UITableView+Reload.h"
#import "WAPageViewController.h"
@implementation WAHourlyForecastTVC
// initialize with a location.
- (instancetype)initWithLocation:(WALocation *)location {
self = [super initWithStyle:UITableViewStylePlain];
if (self) self.location = location;
return self;
}
#pragma mark - Table view controller
- (void)viewDidLoad {
[super viewDidLoad];
// forecast not yet obtained.
if (!self.location.hourlyForecastResponse) {
[self.location fetchHourlyForecast:NO];
[self.location commitRequest];
}
self.navigationItem.titleView = [appDelegate.pageViewController menuLabelWithTitle:@"Hourly forecast"];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.separatorInset = UIEdgeInsetsZero;
// refresh button.
refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshButtonTapped)];
self.navigationItem.rightBarButtonItem = refreshButton;
// register for table headers.
[self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"day"];
}
// update if settings have been changed.
- (void)viewWillAppear:(BOOL)animated {
if (!lastUpdate || [appDelegate.lastSettingsChange timeIntervalSinceDate:lastUpdate] > 0)
[self update:NO];
}
#pragma mark - Weather info
- (void)update {
[self update:YES];
}
// update anything that has changed.
- (void)update:(BOOL)animated {
lastUpdate = [NSDate date];
// generate new cell information.
[self.location updateHourlyForecast];
// update the background if necessary.
if (background != self.location.background)
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:self.location.background];
background = self.location.background;
// update table.
[self.tableView reloadData:animated];
// loading.
if (self.location.loading) [self showIndicator];
else [self hideIndicator];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSUInteger count = [self.location.hourlyForecast count];
return count > 5 ? count : count + 1; // 1 extra for more button
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == [self.location.hourlyForecast count]) return 1;
return [self.location.hourlyForecast[section] count] - 1; // excluding header
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// more button.
if (indexPath.section == [self.location.hourlyForecast count]) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"more"];
// create cell.
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"more"];
// bold label.
cell.textLabel.text = @"See further into the future";
cell.textLabel.font = [UIFont boldSystemFontOfSize:cell.textLabel.font.pointSize];
cell.textLabel.textColor = [UIColor whiteColor];
// white tint when select.
cell.backgroundColor = BLUE_COLOR;
cell.selectedBackgroundView = [UIView new];
cell.selectedBackgroundView.backgroundColor = L_CELL_SEL_COLOR;
// hourly menu icon.
UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icons/menu/hourly"]];
icon.frame = CGRectMake(cell.contentView.bounds.size.width - 40, 10, 30, 30);
icon.tag = 1;
[cell.contentView addSubview:icon];
}
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
UILabel *conditionLabel, *timeLabel;
// new cell.
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
// create condition label.
conditionLabel = [[UILabel alloc] initWithFrame:CGRectMake(115, 0, 155, 50)];
conditionLabel.tag = 17;
conditionLabel.adjustsFontSizeToFitWidth = YES;
[cell.contentView addSubview:conditionLabel];
// create time label.
timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];
timeLabel.tag = 16;
timeLabel.font = [UIFont systemFontOfSize:20];
timeLabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview:timeLabel];
cell.backgroundColor = TABLE_COLOR_T;
cell.detailTextLabel.textColor = [UIColor blackColor];
// shadow on icon.
cell.imageView.layer.shadowColor = [UIColor blackColor].CGColor;
cell.imageView.layer.shadowOffset = CGSizeMake(0, 0);
cell.imageView.layer.shadowRadius = 0.3;
cell.imageView.layer.shadowOpacity = 1.0;
cell.imageView.layer.masksToBounds = NO;
cell.imageView.layer.shouldRasterize = YES;
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:20];
}
// reusing cell.
else {
timeLabel = (UILabel *)[cell.contentView viewWithTag:16];
conditionLabel = (UILabel *)[cell.contentView viewWithTag:17];
}
// detail label on a forecast.
NSDictionary *dict = self.location.hourlyForecast[indexPath.section][indexPath.row + 1];
// 30x30 icon with a slight shadow to increase visibility.
cell.imageView.image = dict[@"iconImage"];
// labels.
timeLabel.attributedText = dict[@"prettyHour"];
cell.detailTextLabel.text = dict[@"temperature"];
conditionLabel.text = dict[@"condition"];
return cell;
}
// disable selection of cells except for more button.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == [self.location.hourlyForecast count]) return YES;
return NO;
}
// more button selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section != [self.location.hourlyForecast count]) return;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
// fetch the forecast.
tenDay = YES; // remember for refresh
[self.location fetchHourlyForecast:YES];
[self.location commitRequestThen:^(NSURLResponse *res, NSDictionary *data) {
if (self.view.window) [self hideIndicator];
}];
// show other loading indicators.
[self showIndicator];
}
// header views. these are reused.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == [self.location.hourlyForecast count]) return nil;
UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"day"];
// fetch labels with tags.
UILabel *dayNameLabel = (UILabel *)[view.contentView viewWithTag:1];
UILabel *dateNameLabel = (UILabel *)[view.contentView viewWithTag:2];
// if there is no label, view hasn't been set up.
if (!dayNameLabel) {
/* The backgroundColor property changes nothing, and the tintColor property
seems to be completely ineffective on iOS 7. I have resorted to using a
custom background view instead.
http://stackoverflow.com/questions/15604900/ios-uitableviewheaderfooterview-unable-to-change-background-color
*/
view.backgroundView = [[UIView alloc] initWithFrame:view.frame];
view.backgroundView.backgroundColor = TABLE_HEADER_COLOR;
// day, e.g. Friday
dayNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 40)];
dayNameLabel.textColor = [UIColor whiteColor];
dayNameLabel.tag = 1;
dayNameLabel.font = [UIFont boldSystemFontOfSize:25];
[view.contentView addSubview:dayNameLabel];
// date, e.g. March 28
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
dateNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenWidth - 165, 0, 150, 40)];
dateNameLabel.textAlignment = NSTextAlignmentRight;
dateNameLabel.textColor = [UIColor whiteColor];
dateNameLabel.tag = 2;
[view.contentView addSubview:dateNameLabel];
}
// update text.
dayNameLabel.text = self.location.hourlyForecast[section][0][0];
dateNameLabel.text = self.location.hourlyForecast[section][0][1];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == [self.location.hourlyForecast count]) return 0;
return 40;
}
#pragma mark - Interface actions
// refresh button was tapped.
- (void)refreshButtonTapped {
// fetch most recent data.
[self.location fetchHourlyForecast:tenDay];
[self.location commitRequest];
// loading and refresh button is visible.
if (self.location.loading) [self showIndicator];
}
#pragma mark - Indicators
// show the loading indicators.
- (void)showIndicator {
if (indicator) return;
// bar button indicator.
UIActivityIndicatorView *ind = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[ind startAnimating];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:ind];
[self.navigationItem setRightBarButtonItem:item animated:YES];
// big indicator.
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.tableView addSubview:indicator];
indicator.center = [self.tableView convertPoint:self.tableView.center fromView:self.tableView.superview];
[indicator startAnimating];
}
// hide the loading indicators.
- (void)hideIndicator {
if (!indicator) return;
[self.navigationItem setRightBarButtonItem:refreshButton animated:YES];
[indicator removeFromSuperview];
indicator = nil;
}
@end