-
Notifications
You must be signed in to change notification settings - Fork 0
/
MasterTableViewController.m
301 lines (242 loc) · 10.9 KB
/
MasterTableViewController.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
//
// MasterTableViewController.m
// kenspeckle
//
// Created by karthik kadapa on 4/6/14.
// Copyright (c) 2014 karthik kadapa. All rights reserved.
//
#import "MasterTableViewController.h"
#import "DetailViewController.h"
#import "AFNetworking.h"
@interface MasterTableViewController ()
@property(nonatomic,strong) NSMutableArray *hotelNames;
@property(nonatomic,strong) CLLocation* currentLocation;
@end
@implementation MasterTableViewController
int latitude;
int longitude;
NSString* preamble = @"";
- (NSMutableArray *)objects
{
if(!_objects){
_objects = [[NSMutableArray alloc]init];
}
return _objects;
}
- (NSMutableArray *)results
{
if(!_results){
_results = [[NSMutableArray alloc]init];
}
return _results;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
_hotelNames = [[NSMutableArray alloc]init];
self.objects = [[NSMutableArray alloc]init];
NSLog(@"flag is %@",self.flagText);
if([self.flagText isEqualToString:@"rootMaster"]){
[self makeRestaurantRequests];
}
if ([self.flagText isEqualToString:@"textSearch"]) {
[self textSearchRequest];
}
self.navigationItem.title = self.cellText;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
_currentLocation = [locations lastObject];
NSLog(@"current location is %@",_currentLocation);
if(_currentLocation!=nil){
NSLog(@"longitude in diduploadlocation %8f",_currentLocation.coordinate.longitude);
NSLog(@"latitude in diduploadlocation %8f",_currentLocation.coordinate.latitude);
}
}
- (void) viewWillAppear:(BOOL)animated {
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];
[super viewWillAppear:animated];
}
-(void) textSearchRequest {
NSLog(@"value is %@",self.searchText);
NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=%@&sensor=false&key=AIzaSyDORbU0ge1NTSk2odGUCMCofZYxp_2GY7c",self.searchText];
NSURL *txturl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"txt url is %@",txturl);
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager GET:[txturl absoluteString] parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
// NSLog(@"JSON Result %@",responseObject);
self.objects = [responseObject objectForKey:@"results"];
[self.tableView reloadData];
} failure:^(NSURLSessionDataTask *task, NSError *error){
NSLog(@"Request Failed: %@, %@",error,error.userInfo);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) searchThroughData {
self.results = nil;
NSLog(@"self.searchBar.text--- %@",self.searchBar.text);
NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text];
for(int j = 0; j < [_hotelNames count]; j++){
for(int k = j+1;k < [_hotelNames count];k++){
NSString *str1 = [_hotelNames objectAtIndex:j];
NSString *str2 = [_hotelNames objectAtIndex:k];
if([str1 isEqualToString:str2])
[_hotelNames removeObjectAtIndex:k];
}
}
NSLog(@"resultsPredicate-- %@",resultsPredicate);
NSLog(@"-hotel names %@",_hotelNames);
self.results = [[_hotelNames filteredArrayUsingPredicate:resultsPredicate] mutableCopy];
NSLog(@"self.results-- %@",self.results);
}
- (void) searchBar:(UISearchBar *)searchBar co:(NSString *)searchText{
[self searchThroughData];
}
- (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.
if(tableView == self.tableView){
return self.objects.count;
}else {
[self searchThroughData];
return self.results.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
// Configure the cell...
NSDictionary *tempDictionary = [self.objects objectAtIndex:indexPath.row];
[_hotelNames addObject:[tempDictionary objectForKey:@"name"]];
NSURL *url = [NSURL URLWithString:[tempDictionary objectForKey:@"icon"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
cell.imageView.image = image;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(tableView == self.tableView){
NSString* ratingStr;
NSString* ratingVal;
NSString *totalRating;
ratingStr = @"Rating - ";
NSLog(@"self.objects[indexPath.row] %@",[self.objects[indexPath.row] objectForKey:@"name"]);
ratingVal = [self.objects[indexPath.row] objectForKey:@"rating"];
if(ratingVal!=NULL){
totalRating = [NSString stringWithFormat:@"%@ %@", ratingStr, ratingVal];
}else {
totalRating = [NSString stringWithFormat:@"%@ %@", ratingStr, @""];
}
NSLog(@"str %@",totalRating);
cell.detailTextLabel.text = totalRating;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
cell.textLabel.text = [self.objects[indexPath.row] objectForKey:@"name"];
}
//Search Values
else{
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
cell.textLabel.text = self.results[indexPath.row];
}
return cell;
}
-(void) makeRestaurantRequests {
NSLog(@"make latitude %f",self.locationManager.location.coordinate.latitude);
NSLog(@"make longitude %f",self.locationManager.location.coordinate.longitude);
NSString *searchString;
if([self.cellText isEqualToString:@"Museum"]){
searchString = @"museum";
}if ([self.cellText isEqualToString:@"Food"]) {
searchString = @"food";
}if ([self.cellText isEqualToString:@"Pizza"]) {
searchString = @"food";
}if ([self.cellText isEqualToString:@"Casino"]) {
searchString = @"casino";
}if ([self.cellText isEqualToString:@"Gym"]) {
searchString = @"gym";
}if ([self.cellText isEqualToString:@"Nightclub"]) {
searchString = @"night_club";
}if ([self.cellText isEqualToString:@"Skateboarding"]) {
searchString = @"amusement_park";
}if ([self.cellText isEqualToString:@"Bowling"]) {
searchString = @"bowling_alley";
}if ([self.cellText isEqualToString:@"Acting"]) {
searchString = @"movie_rental";
}if ([self.cellText isEqualToString:@"Photography"]) {
searchString = @"art_gallery";
}if ([self.cellText isEqualToString:@"Movies"]) {
searchString = @"movie_theater|movie_rental";
}if ([self.cellText isEqualToString:@"Bank of America"]) {
searchString = @"bank";
}if ([self.cellText isEqualToString:@"Animals"]) {
searchString = @"zoo";
}if ([self.cellText isEqualToString:@"General"]) {
searchString = @"";
}if ([self.cellText isEqualToString:@"Cars"]) {
searchString = @"car_dealer|car_rental";
}
NSLog(@"searchString is %@",searchString);
NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=%f,%f&radius=50000&types=%@&sensor=false&key=AIzaSyDORbU0ge1NTSk2odGUCMCofZYxp_2GY7c",self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude,searchString];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"urlString is %@",url);
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager GET:[url absoluteString] parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
self.objects = [responseObject objectForKey:@"results"];
[self.tableView reloadData];
} failure:^(NSURLSessionDataTask *task, NSError *error){
NSLog(@"Request Failed: %@, %@",error,error.userInfo);
}];
}
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row %2){
cell.backgroundColor = [UIColor colorWithRed: 255.0/255.0 green: 130.0/255.0 blue:171.0/255.0 alpha: 1.0];
}else {
cell.backgroundColor = [UIColor whiteColor];
}
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.searchDisplayController.isActive){
[self performSegueWithIdentifier:@"ShowDetail" sender:self];
}
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if([[segue identifier] isEqualToString : @"ShowDetail"]){
// NSString *object = nil;
NSIndexPath *indexPath; //= [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = (DetailViewController*)segue.destinationViewController;
if(self.searchDisplayController.isActive){
indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
// self.objects = self.results[indexPath.row];
}else {
indexPath = [self.tableView indexPathForSelectedRow];
// self.objects = self.objects[indexPath.row];
}
detailViewController.restaurantDetail = [self.objects objectAtIndex:indexPath.row];
// [[segue destinationViewController] setDetailLabelContents:object];
}
}
@end