-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlacesTableViewController.m
More file actions
251 lines (149 loc) · 6.73 KB
/
PlacesTableViewController.m
File metadata and controls
251 lines (149 loc) · 6.73 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
//
// PlacesTableViewController.m
// FSDash
//
// Created by Jose Ramos on 3/20/14.
// Copyright (c) 2014 Jose Ramos. All rights reserved.
//
#import "PlacesTableViewController.h"
#import "VenueDetailsViewController.h"
#import "PlacesCell.h"
@interface PlacesTableViewController ()
@end
@implementation PlacesTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.images = [[NSMutableArray alloc] init];
requestingHTTP = NO;
// self.request = [[RequestModel alloc] init];
// [self.request httpRequest];
[self getUserLocation];
//[self httpRequest:location];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.venuesResults.count; //total count of objects in the "request"
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
PlacesCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[PlacesCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.venueName.text = [NSString stringWithFormat:@"%@", [[self.venuesResults objectAtIndex:indexPath.row] objectForKey:@"name"]];
cell.venueImage.image = [self.images objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.venueID= [NSString stringWithFormat:@"%@",[[self.venuesResults objectAtIndex:indexPath.row] objectForKey:@"id"]];
self.venueName = [NSString stringWithFormat:@"%@",[[self.venuesResults objectAtIndex:indexPath.row] objectForKey:@"name"]];
[self performSegueWithIdentifier:@"showVenueDetails" sender:self];
}
#pragma mark -HTTP Requests
-(void)httpRequest:(NSString *)location
{
NSLog(@"in http request");
requestingHTTP = YES;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSString *post = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%@&oauth_token=QCDYZZ2YUFAUHTXTW0SXNGDZCA45OBDADIHFX2WB2NKZYGYD&v=20140320", location];
[manager POST:post parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
self.results = (NSDictionary *)responseObject;
self.venuesResults = [[self.results objectForKey:@"response"] objectForKey:@"venues"];
[self initializeImages];
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
}
-(void)initializeImages
{
count = 0;
NSString *theID= [NSString stringWithFormat:@"%@",[[self.venuesResults objectAtIndex:0] objectForKey:@"id"]];
[self theVenueThumbnail:theID atIndex:count];
}
-(void)theVenueThumbnail:(NSString *)venueId atIndex:(int)index
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSString *post = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/%@/photos?limit=1&oauth_token=QCDYZZ2YUFAUHTXTW0SXNGDZCA45OBDADIHFX2WB2NKZYGYD&v=20140320", venueId];
[manager GET:post parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary *imageThumbnail = (NSDictionary *)responseObject;
NSArray *thumbnails = [[[imageThumbnail objectForKey:@"response"] objectForKey:@"photos"] objectForKey:@"items"];
if(thumbnails.count > 0)
{
NSString *urlPart1 = [[thumbnails objectAtIndex:0] objectForKey:@"prefix"];
NSString *urlPart2 = [[thumbnails objectAtIndex:0] objectForKey:@"suffix"];
NSString *imageURL = [NSString stringWithFormat:@"%@50x50%@", urlPart1, urlPart2];
UIImage *theImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
[self.images insertObject:theImage atIndex:index];
}
else
{
UIImage *noImage = [UIImage imageNamed:@"no-available-image"];
[self.images insertObject:noImage atIndex:index];
}
count++;
if(count == self.venuesResults.count || count > self.venuesResults.count)
{
NSLog(@"TOTAL");
[self.tableView reloadData];
}
else
{
NSString *theID= [NSString stringWithFormat:@"%@",[[self.venuesResults objectAtIndex:count] objectForKey:@"id"]];
[self theVenueThumbnail:theID atIndex:count];
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
}
#pragma mark - Location Methods
-(void)getUserLocation
{
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager startUpdatingLocation];
self.locationManager.delegate = self;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"stopping location");
[self.locationManager stopUpdatingLocation];
latitude = self.locationManager.location.coordinate.latitude;
longitude = self.locationManager.location.coordinate.longitude;
NSString *location = [NSString stringWithFormat:@"%f,%f", latitude, longitude];
if(!requestingHTTP) //verification so we don't call the httprequest more than once by mistake
[self httpRequest:location];
}
#pragma mark - Segue Methods
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"showVenueDetails"])
{
NSLog(@"show venue details %@", self.venueID);
VenueDetailsViewController *vc = (VenueDetailsViewController*)segue.destinationViewController;
vc.venueID = self.venueID;
vc.venueName = self.venueName;
}
}
@end