-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShopsTableViewController.m
429 lines (360 loc) · 14.3 KB
/
ShopsTableViewController.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* Copyright (c) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <CoreLocation/CoreLocation.h>
#import "ExtraPropertyUITableViewCell.h"
#import "GTLShoppingassistant.h"
#import "GTLService.h"
#import "GTMHTTPFetcher.h"
#import "GTMHTTPFetcherLogging.h"
#import "GTMOAuth2Authentication.h"
#import "OffersTableViewController.h"
#import "ShopsTableViewController.h"
#import "ViewHelper.h"
@interface ShopsTableViewController()
@property(nonatomic, strong) GTLServiceShoppingassistant *service;
@property(nonatomic, strong) CLLocationManager *locationManager;
@property(nonatomic, strong) CLLocation *currentLocation;
@end
@implementation ShopsTableViewController
NSString *const kKeyChainName = @"mobileAssistant";
NSString *const kKeyClientID = @"{{{ INSERT ID }}}";
NSString *const kKeyClientSecret = @"{{{ INSERT SECREET }}}";
NSString *const kShopToOfferSequeID = @"Show Discount List";
NSString *const kShopTableCellID = @"Shop with Address";
@synthesize shops = _shops;
@synthesize service = _service;
@synthesize auth = _auth;
@synthesize locationManager = _locationManager;
@synthesize currentLocation = _currentLocation;
- (IBAction)pressSignout {
[self unAuthenticateUser];
[ViewHelper showSigninToolBarButtonForViewController:self];
}
- (IBAction)pressSignin {
[self authenticateUser];
[ViewHelper showSignoutToolBarButtonForViewController:self];
}
#pragma mark - Customized setters and getters
- (void)setShops:(NSArray *)list {
if (_shops != list) {
_shops = list;
if (self.tableView.window) {
[self.tableView reloadData];
if (self.navigationItem.rightBarButtonItem) {
self.navigationItem.rightBarButtonItem = nil;
}
}
}
}
- (GTLServiceShoppingassistant *)service {
if (!_service) {
_service = [[GTLServiceShoppingassistant alloc] init];
_service.shouldFetchNextPages = YES;
_service.retryEnabled = YES;
}
return _service;
}
#pragma mark - Authentication model
// Show user login view
- (void)showUserLoginView {
NSUInteger id = [kKeyClientID rangeOfString:@"{{{ INSERT ID }}}"].location;
assert(id == NSNotFound);
NSUInteger secret =
[kKeyClientSecret rangeOfString:@"{{{ INSERT SECREET }}}"].location;
assert(secret == NSNotFound);
GTMOAuth2ViewControllerTouch *oauthViewController;
oauthViewController =
[[GTMOAuth2ViewControllerTouch alloc]
initWithScope:@""
clientID:kKeyClientID
clientSecret:kKeyClientSecret
keychainItemName:kKeyChainName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[self presentViewController:oauthViewController animated:YES completion:nil];
}
- (void)authenticateUser {
if (!self.auth) {
// Instance doesn't have an authentication object, attempt to fetch from
// keychain. This method call always returns an authentication object.
// If nothing is returned from keychain, this will return an invalid
// authentication
self.auth = [GTMOAuth2ViewControllerTouch
authForGoogleFromKeychainForName:kKeyChainName
clientID:kKeyClientID
clientSecret:kKeyClientSecret];
}
// Now instance has an authentication object, check if it's valid
if ([self.auth canAuthorize]) {
// Looks like token is good, reset instance authentication object
[self resetAccessTokenForCloudEndpoint];
NSLog(@"%@", self.auth);
} else {
// If there is some sort of error when validating the previous
// authentication, reset the authentication and force user to login
self.auth = nil;
[self showUserLoginView];
}
}
// Reset access token value for authentication object for Cloud Endpoint.
- (void)resetAccessTokenForCloudEndpoint {
GTMOAuth2Authentication *auth = self.auth;
if (auth) {
self.auth.authorizationTokenKey = @"id_token";
[self.service setAuthorizer:auth];
// Add a sign out button
[ViewHelper showSignoutToolBarButtonForViewController:self];
// Reload the table if it's on screen
if (self.tableView.window) {
[self.tableView reloadData];
if (self.navigationItem.rightBarButtonItem && self.shops) {
self.navigationItem.rightBarButtonItem = nil;
}
}
}
}
// Callback method after user finished the login.
- (void)viewController:(GTMOAuth2ViewControllerTouch *)oauthViewController
finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];
if (error) {
[ViewHelper showPopup:@"Error"
message:@"Failed to authenticate user"
button:@"OK"];
NSLog(@"%@", error);
} else {
self.auth = auth;
[self resetAccessTokenForCloudEndpoint];
NSLog(@"%@",self.auth);
}
}
// Signing user out and revoke token
- (void)unAuthenticateUser {
[GTMOAuth2ViewControllerTouch removeAuthFromKeychainForName:kKeyChainName];
[GTMOAuth2ViewControllerTouch revokeTokenForGoogleAuthentication:self.auth];
}
#pragma mark - View first loaded
- (void)viewDidLoad {
// Turn on logging
[GTMHTTPFetcher setLoggingEnabled:YES];
// Show the spinner
[ViewHelper showToolbarSpinnerForViewController:self];
// Authenticate user if needed
[self authenticateUser];
// Get current location
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
#pragma mark - Prepare Segue from "Shops" screen to "Goodies" screen
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier
sender:(ExtraPropertyUITableViewCell *)sender {
if ([identifier isEqual:kShopToOfferSequeID]) {
if ([self.navigationItem.leftBarButtonItem.title isEqual:@"Sign out"]) {
return YES;
} else {
[ViewHelper showPopup:@"Warning"
message:@"Please sign in first before checkin"
button:@"OK"];
}
}
return NO;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(ExtraPropertyUITableViewCell *)sender {
// Prepare the offers/recommendations list for display from "Shops" screen
// to "Goodies" screen
if ([segue.identifier isEqual:kShopToOfferSequeID]) {
OffersTableViewController *nextController =
segue.destinationViewController;
// Set the store name
nextController.storeName = sender.textLabel.text;
// Check in first, which will then pull all offers and recommendations
[self checkIn:sender.databaseID nextController:nextController];
// Set a spinner for the in the goodies controller
[ViewHelper showToolbarSpinnerForViewController:nextController];
}
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@"locationManager:didFailWithError: %@", error);
[ViewHelper showPopup:@"Error"
message:@"Failed to get current location"
button:@"OK"];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
// Update location once receive the GPS update from the phone
self.currentLocation = newLocation;
NSLog(@"current location: %@", self.currentLocation);
[self.locationManager stopUpdatingLocation];
// Go get all the shops from backend only when the current location is set
[self getAllShops];
}
#pragma mark - UITableView data source for shops
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.shops count];
}
- (ExtraPropertyUITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = kShopTableCellID;
ExtraPropertyUITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
if (!cell) {
cell = [[ExtraPropertyUITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:kShopTableCellID];
}
GTLShoppingassistantPlaceInfo *place =
[self.shops objectAtIndex:indexPath.row];
cell.textLabel.text = place.name;
cell.textLabel.textColor =
[UIColor colorWithRed:0.8 green:0.22 blue:0.68 alpha:1.0];
cell.detailTextLabel.numberOfLines = @2;
float distance = [place.distanceInKilometers floatValue];
NSString *unit = @"km";
if (! [NSLocaleMeasurementSystem isEqual:@"Metric"]) {
static float kmToMileRatio = 0.621371;
distance = distance * kmToMileRatio;
unit = @"mi";
}
cell.detailTextLabel.text =
[NSString stringWithFormat:@"Distance: %.1f%@\n%@", distance, unit,
place.address];
cell.databaseID = place.placeId;
UIImage *image = [UIImage imageNamed:@"logo.png"];
CGFloat height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
CGSize desiredSize = CGSizeMake(kDefaultCellImageWidth, height);
cell.imageView.image = [ViewHelper resizedImageWithImage:image
toSize:desiredSize];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableview
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kDefaultCellHeight + kDetailTitleHeightPerLine;
}
#pragma mark - Shops Model
- (void)getAllShops {
CLLocationCoordinate2D location = self.currentLocation.coordinate;
if (CLLocationCoordinate2DIsValid(location)) {
NSString *latitude = [NSString stringWithFormat:@"%.8f", location.latitude];
NSString *longitude = [NSString stringWithFormat:@"%.8f",
location.longitude];
GTLQueryShoppingassistant *query =
[GTLQueryShoppingassistant queryForPlaceEndpointListWithCount:100
distanceInKm:100
latitude:latitude
longitude:longitude];
[self.service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLShoppingassistantPlaceInfoCollection *object,
NSError *error) {
if (error) {
[ViewHelper showPopup:@"Error"
message:@"Unable to query a list of shops"
button:@"OK"];
NSLog(@"%@", error);
} else {
self.shops = [object items];
NSLog(@"%@", self.shops);
}
}];
} else {
NSLog(@"Current location is invalid: %.8f, %.8f",
location.latitude, location.longitude);
}
}
#pragma mark - Checkin Model
- (void)checkIn:(NSString *)placeId
nextController:(OffersTableViewController *)nextController {
GTLShoppingassistantCheckIn *checkIn =
[[GTLShoppingassistantCheckIn alloc] init];
checkIn.placeId = placeId;
GTLQueryShoppingassistant *checkinQuery =
[GTLQueryShoppingassistant queryForCheckInEndpointInsertWithObject:checkIn];
[self.service executeQuery:checkinQuery
completionHandler:^(GTLServiceTicket *ticket,
id object,
NSError *error) {
if (error) {
[ViewHelper showPopup:@"Error"
message:@"Unable to check in, try again."
button:@"OK"];
NSLog(@"%@", error);
// Reauthenticate the user
[self authenticateUser];
} else {
// Get a list of offers if it's not pulled before
if (!nextController.offers) {
[self getAllOffers:placeId
nextController:nextController];
}
// Get a list of recommendations if it's not pulled before
if (!nextController.recommendations) {
[self getAllRecommendations:placeId
nextController:nextController];
}
}
}];
}
#pragma mark - Offer Model
- (void)getAllOffers:(NSString *)placeId
nextController:(OffersTableViewController *)nextController {
GTLQueryShoppingassistant *query =
[GTLQueryShoppingassistant queryForOfferEndpointListWithPlaceId:placeId];
[self.service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLShoppingassistantOfferCollection *object,
NSError *error) {
if (error) {
[ViewHelper showPopup:@"Error"
message:@"Retrieving offers failed."
button:@"OK"];
NSLog(@"%@", error);
} else {
nextController.offers = [object items];
NSLog(@"%@", nextController.offers);
}
}];
}
#pragma mark - Recommendation Model
- (void)getAllRecommendations:(NSString *)placeId
nextController:(OffersTableViewController *)nextController {
GTLQueryShoppingassistant *query =
[GTLQueryShoppingassistant
queryForRecommendationEndpointListWithPlaceId:placeId];
[self.service executeQuery:query
completionHandler:
^(GTLServiceTicket *ticket,
GTLShoppingassistantRecommendationCollection *object,
NSError *error) {
if (error) {
[ViewHelper showPopup:@"Error"
message:@"Retrieving recommendations failed."
button:@"OK"];
NSLog(@"%@", error);
} else {
nextController.recommendations = [object items];
NSLog(@"%@", nextController.recommendations);
}
}];
}
@end