/
City.m
169 lines (121 loc) · 4.6 KB
/
City.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
//
// City.m
// Weather
//
// Created by Tom Williamson on 5/5/16.
// Copyright © 2016 Tom Williamson. All rights reserved.
//
#import "City.h"
@implementation City
@synthesize apidata;
@synthesize delegate;
@synthesize doSave;
static int getZip = 1;
static int getForecast = 2;
//
// validate a zip code
//
// build APIData request
//
-(void)validateZip:(NSString*) zipcode delegate:(id<CityDelegate>) delegatex{
self.apidata = [[APIData alloc] init];
self.delegate = delegatex;
NSString* req = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:%@&sensor=false", zipcode];
apidata.userType = getZip;
[self.apidata startRequest:req delegate:self];
}
//
// update the weather information for this city
//
// build APIData request and start the request
//
-(void)updateForecast:(id<CityDelegate>) delegatex{
self.apidata = [[APIData alloc] init];
self.delegate = delegatex;
NSString* req = [NSString stringWithFormat:@"https://api.forecast.io/forecast/b706a847fb62309a43d0c68ac494e4b4/%@", self.coordinates];
apidata.userType = getForecast;
[self.apidata startRequest:req delegate:self];
}
//
// we are back from the apiData request
//
// see if we got good zipcode info or good forecast info
//
-(void)gotAPIData:(APIData*)apidata{
if(!self.apidata.errorText) {
//
// process the zip code api call
//
if(self.apidata.userType == getZip) {
NSString* status = self.apidata.dictionary[@"status"];
if (![status isEqualToString:@"OK"]) {
self.apidata.errorText = status;
}
else {
NSDictionary *dict = self.apidata.dictionary[@"results"][0];
if(dict) {
//
// extract fields we want from the dictionary
//
self.name = dict[@"formatted_address"];
NSString *match = [NSString stringWithFormat:@", "];
NSScanner *scanner = [NSScanner scannerWithString:self.name];
NSString *cityName;
[scanner scanUpToString:match intoString:&cityName];
self.city = cityName;
NSDictionary *loc = dict[@"geometry"][@"location"];
self.latitude = loc[@"lat"];
self.longitude = loc[@"lng"];
self.coordinates = [NSString stringWithFormat:@"%@,%@", self.latitude, self.longitude];
self.updatedAt = [NSDate date];
}
}
}
//
// process the forecast api call
//
else if(self.apidata.userType == getForecast) {
NSString* status = self.apidata.dictionary[@"error"];
if (status) {
self.apidata.errorText = status;
}
else {
NSDictionary *dict = self.apidata.dictionary[@"currently"];
if(dict) {
//
// extract the fields we want from the data dictionary
// and update our attributes (fields names must match)
//
self.updatedAt = [NSDate date];
NSArray* fields = @[@"summary", @"icon", @"nearestStormDistance", @"nearestStormBearing", @"precipIntensity", @"precipProbability",
@"temperature", @"apparentTemperature", @"dewPoint", @"humidity",
@"windSpeed", @"windBearing", @"visibility", @"cloudCover",
@"pressure", @"ozone", @"timezone"];
for (NSString* key in fields) {
[self setValue:dict[key] forKey:key];
}
}
}
}
//
// see if we need to save the object
//
if(self.doSave && !self.apidata.errorText)
[self save];
}
//
// let caller know we are done
//
[self.delegate cityUpdated:self];
}
//
// save this object
//
-(void)save{
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
@end