-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWANewLocationTVC.m
328 lines (252 loc) · 11.4 KB
/
WANewLocationTVC.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// WANewLocationVC.m
// WeatherApp
//
// Created by Mitchell Cooper on 11/1/13.
// Copyright (c) 2013-14 Mitchell Cooper. All rights reserved.
//
#import "WANewLocationTVC.h"
#import "WANavigationController.h"
#import "WALocationManager.h"
#import "WALocation.h"
#import "WALocationListTVC.h"
@implementation WANewLocationTVC
#pragma mark - Table view controller
- (void)viewDidLoad {
[super viewDidLoad];
results = [NSMutableArray array];
// keyboard notifications.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
self.navigationItem.title = @"New favorite";
// background.
self.tableView.backgroundColor = TABLE_COLOR;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgrounds/clear.jpg"]];
}
- (void)viewDidAppear:(BOOL)animated {
// scroll back to the top with this silly rect.
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
// select the text field and bring up the keyboard.
[textField becomeFirstResponder];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2; // one for text field, one for results
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) return 1; // text field
return [results count]; // results
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
// input section.
if (indexPath.section == 0) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// text field not yet created.
if (!textField) {
textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 10, 280, 30)];
textField.placeholder = @"Search locations...";
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.adjustsFontSizeToFitWidth = YES;
textField.delegate = self;
}
[cell.contentView addSubview:textField];
cell.backgroundColor = TABLE_COLOR;
return cell;
}
// result section.
UIImageView *imageView;
cell = [self.tableView dequeueReusableCellWithIdentifier:@"result"];
if (!cell) {
// new cell.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"result"];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor =
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.selectedBackgroundView = [UIView new];
cell.selectedBackgroundView.backgroundColor = L_CELL_SEL_COLOR;
cell.textLabel.adjustsFontSizeToFitWidth = YES;
// image for flag.
imageView = [UIImageView new];
imageView.tag = 1;
[cell.contentView addSubview:imageView];
} else imageView = (UIImageView *)[cell.contentView viewWithTag:1];
// location name.
cell.textLabel.text = results[indexPath.row][@"longName"];
// country flag.
UIImage *image = [UIImage imageNamed:FMT(@"flags/%@", [results[indexPath.row][@"countryShort"] lowercaseString])];
if (!image) image = [UIImage imageNamed:@"flags/unknown"];
imageView.image = image;
imageView.frame = CGRectMake(
[UIScreen mainScreen].bounds.size.width - image.size.width - 15,
(cell.frame.size.height - image.size.height) / 2.,
image.size.width,
image.size.height
);
return cell;
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) return NO;
if ([results count] == 0) return NO;
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
// a result was selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// create a location using the data we generated in lookupSuggestion:
WALocation *location = [locationManager createLocationFromDictionary:results[indexPath.row]];
// fetch the conditions; then, return to the location list.
[location fetchCurrentConditions];
[location commitRequest];
[self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - Suggestion lookups
// look up something (firstPart = the query so far)
- (void)lookupSuggestion:(NSString *)firstPart {
// empty. just clear it.
if (![firstPart length]) {
[results removeAllObjects];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
return;
}
NSLog(@"Looking up suggestions for %@", firstPart);
// figure the API URL and create a request.
NSString *str = FMT(@"http://autocomplete.wunderground.com/aq?query=%@&h=1&ski=1&features=1", URL_ESC(firstPart));
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
// send the request asynchronously.
NSDate *date = [NSDate date];
[appDelegate beginActivity];
[self showIndicator];
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
[appDelegate endActivity];
[self hideIndicator];
// the user already selected something, so just forget about this request.
// or if the user has typed since this request started, forget it.
if (selectedOne || [lastTypeDate laterDate:date] == lastTypeDate) return;
// a connection error occurred.
if (connectionError) {
NSLog(@"Location lookup connection error: %@", connectionError);
[appDelegate displayAlert:@"Lookup error" message:[connectionError localizedDescription]];
return;
}
// decode the JSON.
NSError *jsonError;
NSDictionary *obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
// a JSON error occured.
if (jsonError) {
NSLog(@"JSON error: %@", jsonError);
return;
}
// clear the results from the last request.
[results removeAllObjects];
// add the new results.
for (NSDictionary *place in obj[@"RESULTS"]) {
// name type city c zmw tz tzs l(/q)
// this must be a city.
BOOL skipPlace = NO;
// ensure that this place has the information we need.
for (NSString *key in @[@"c", @"l", @"name"]) {
if (place[key]) continue;
NSLog(@"No %@ found for %@; skipping", key, place[@"name"]);
skipPlace = YES;
}
if (![place[@"type"] isEqualToString:@"city"]) {
NSLog(@"%@ (%@) appears to not be a city; skipping", place[@"name"], place[@"type"]);
skipPlace = YES;
}
if (skipPlace) continue;
// used for all types of cities.
NSMutableDictionary *loc = [@{
@"longName": place[@"name"],
@"l": place[@"l"],
@"countryShort": place[@"c"]
} mutableCopy];
[results addObject:loc];
}
// reload section 1 of the table.
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
}
#pragma mark - Text field delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
lastTypeDate = [NSDate date];
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
[self performSelector:@selector(checkIfTypedSince:) withObject:[NSDate date] afterDelay:0.3];
return YES;
}
// check if the user has typed since a half-second ago.
- (void)checkIfTypedSince:(NSDate *)date {
// user has typed since.
if ([date laterDate:lastTypeDate] == lastTypeDate) return;
NSLog(@"User hasn't typed for 0.3 seconds; looking up %@", textField.text);
[self lookupSuggestion:textField.text];
}
#pragma mark - Keyboard notifications
// the keyboard WAS shown.
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// already adjusted this way.
if (self.tableView.scrollIndicatorInsets.bottom >= kbSize.height) return;
// adjust the scrollview insets.
UIEdgeInsets currentInsets = self.tableView.contentInset;
self.tableView.contentInset =
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(
currentInsets.top,
currentInsets.left,
currentInsets.bottom + kbSize.height,
currentInsets.right
);
// make sure the text input field is visible.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, textField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, textField.frame.origin.y - kbSize.height);
[self.tableView setContentOffset:scrollPoint animated:YES];
}
}
// the keyboard WILL hide.
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// not adjusted.
if (self.tableView.scrollIndicatorInsets.bottom < kbSize.height) return;
// reset to former insets.
UIEdgeInsets currentInsets = self.tableView.contentInset;
self.tableView.contentInset =
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(
currentInsets.top,
currentInsets.left,
currentInsets.bottom - kbSize.height,
currentInsets.right
);
}
#pragma mark - Indicator
// show the loading indicators.
- (void)showIndicator {
if ([indicator isAnimating]) return;
if (!indicator) {
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.alpha = 0;
[self.tableView addSubview:indicator];
indicator.center = [self.tableView convertPoint:self.tableView.center fromView:self.tableView.superview];
}
[indicator startAnimating];
[UIView animateWithDuration:0.3 animations:^{
indicator.alpha = 1;
}];
}
// hide the loading indicators.
- (void)hideIndicator {
if (!indicator) return;
[UIView animateWithDuration:0.3 animations:^{
indicator.alpha = 0;
} completion:^(BOOL finished) {
if (finished) [indicator stopAnimating];
}];
}
@end