-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLikedBeersViewController.m
423 lines (339 loc) · 19.2 KB
/
LikedBeersViewController.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
//
// LikedBeersViewController.m
// Beer Hopper
//
// Created by Justin Goulet on 6/24/16.
// Copyright © 2016 The Crony Project. All rights reserved.
//
#import "LikedBeersViewController.h"
@interface LikedBeersViewController ()
@end
@implementation LikedBeersViewController
- (void)viewDidLoad {
[super viewDidLoad];
//Load beer list
myData = [[HopperData alloc] init];
self.mainTable.delegate = self;
self.mainTable.dataSource = self;
self.mainTable.allowsSelection = YES;
self.mainTable.backgroundColor = [UIColor clearColor];
self.likedBeers = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"likedBeers"]];
//Add the frame for the liked beers image
float tabbarHeight = self.navigationController.navigationBar.frame.size.height + self.tabBarController.tabBar.frame.size.height;
noBeersLikedImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, tabbarHeight, self.view.frame.size.width, self.view.frame.size.height - tabbarHeight)];
noBeersLikedImage.image = [UIImage imageNamed:@"HowToLikeBeers.png"];
[self.view addSubview:noBeersLikedImage];
noBeersLikedImage.contentMode = UIViewContentModeScaleAspectFit;
noBeersLikedImage.hidden = YES;
noBeersLikedImage.backgroundColor = self.view.backgroundColor;
noBeersLikedImage.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 + tabbarHeight/4);
activity = [[ImageActivityView alloc] init];
[self.view addSubview:activity];
activity.tintColor = [UIColor colorWithRed:0.380 green:0.490 blue:0.541 alpha:1.00];
[activity useImage:[UIImage imageNamed:@"BeerHopperLogo.png"]];
[activity startAnimating];
[self loadBeerList];
if(self.beerListCount > 0){
NSLog(@"Beers Found: %i", self.beerListCount);
[self loadBeersFromList];
}
else{
NSLog(@"No Beers Found!");
//Display the "Blank view" here
[activity stopAnimating];
noBeersLikedImage.hidden = NO;
}
}
//Load the beer list
-(void)loadBeerList{
self.beerList = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"likedBeers"]];
self.beerListCount = (int)self.beerList.count;
}
-(void)loadBeersFromList{
//Create the query to get all beers that match the record ID within the beer list
NSURL *url = [self BeerQuery];
NSURLRequest *res = [NSURLRequest requestWithURL:url];
NSOperationQueue*que=[NSOperationQueue new];
//NSMutableArray *foundRecords = [[NSMutableArray alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reDoBeers) name:@"beersLoaded" object:nil];
[NSURLConnection sendAsynchronousRequest:res queue:que completionHandler:^(NSURLResponse*rep,NSData*data,NSError*err){
if ([data length]> 0 && err == nil) {
NSString* rel=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&err];
int iterator = 0;
//NSLog(@"Rel: %@", rel);
//NSLog(@"%@",json[@"records"]);
if(rel.length > 0){
//NSLog(@"Records: %@", json);
[myData createBeersWithDictionary:json[@"records"]];
/*
//Now, filter
for (NSDictionary *tem in json[@"records"]) {
NSLog(@"tem: %@", tem);
}*/
self.beers = [[NSMutableArray alloc] init];
for(NSDictionary *type in json[@"records"]){
//NSLog(@"Beer Found: %@", type[@"fields"]);
Beer *newBeer = [[Beer alloc] initWithName:type[@"fields"][@"Beer Name"] type:@"" description:type[@"fields"][@"Beer Description"]];
newBeer.iden = type[@"id"];
newBeer.reviews = [NSArray arrayWithArray:type[@"fields"][@"Reviews"]];
newBeer.ABV = [type[@"fields"][@"ABV"] floatValue];
newBeer.beerType = [type[@"fields"][@"Beer Type"] objectAtIndex:0];
newBeer.fromTheBrewer = type[@"fields"][@"From the Brewmaster"];
newBeer.IBU = [type[@"fields"][@"IBU"] floatValue];
newBeer.servingGlass = type[@"fields"][@"Recommended Serving Glass"];
newBeer.isAvail = [type[@"fields"][@"isAvailable"] boolValue];
newBeer.votes = [type[@"fields"][@"TotalVotes"] intValue];
newBeer.style = type[@"fields"][@"ServingStyle"];
newBeer.rating = [type[@"fields"][@"Average Rating"] floatValue];
newBeer.breweryName = [type[@"fields"][@"BreweryName"] objectAtIndex:0];
newBeer.pairings = type[@"fields"][@"Pairings"];
newBeer.awards = type[@"fields"][@"Awards"];
//NSLog(@"New Beer Name: %@", newBeer.beerName);
//Get the image
NSDictionary *coverImageURL = type[@"fields"][@"Picture"];
for (NSDictionary *keys in coverImageURL) {
//NSLog(@"Keys: %@", keys);
//newBeer.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:keys[@"url"]]]];
newBeer.imageURL = keys[@"url"];
//Get other info
}
iterator++;
//if([newBeer.style isEqualToString:@"CO2"]) self.brewery.beersOnCO2++;
//else if([newBeer.style isEqualToString:@"Nitro"]) self.brewery.beersOnNitro++;
[self.beers addObject:newBeer];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
UIImage *pic = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:newBeer.imageURL]]];
// update UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
newBeer.image = pic;
//NSLog(@"Beers Count: %i", (int)self.beers.count);
self.loadedBeerList = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"beersLoaded" object:self];
[self.mainTable performSelectorOnMainThread:@selector(reloadData) withObject:NULL waitUntilDone:NO];
});
});
//Run through the brewery list
//NSArray *breweries = type[@"fields"][@"Brewery"];
/*
for (Brewery *temp in self.breweries) {
if ([breweries containsObject:temp.iden]) {
[temp.beerList addObject:newBeer];
//Check the serving style and add to the total count
//if(newBeer.isAvail){
if([type[@"fields"][@"Serving Style"] isEqualToString:@"CO2"]) temp.beersOnCO2++;
else if([type[@"fields"][@"Serving Style"] isEqualToString:@"Nitro"]) temp.beersOnCO2++;
//}
//NSLog(@"Should be adding: %@ to %@", newBeer.beerName, temp.name);
}
//NSLog(@"Brewery: %@", breweries);
}*/
}
//[self.beerTable performSelectorOnMainThread:@selector(reloadData) withObject:NULL waitUntilDone:NO];
if (iterator ==0 ) {
NSLog(@"No Beers found!");
}
}
else{
NSLog(@"Error: %@", err.localizedDescription);
}
}
}];
}
-(void)reDoBeers{
//NSLog(@"Refreshing Beer List");
[activity stopAnimating];
[activity removeFromSuperview];
//self.brewery.beerList = self.beers;
[self.mainTable reloadData];
}
/**
* Filter by formula creation. Note, this is assuming the query already has the initial phrase setup. Tis area will only add the AND or the ORs in the correct order
*/
-(NSString *)compoundFormulaWithSingleOperator:(NSString *)op andBeers:(NSArray *)operations{
//NSLog(@"Creating compound formula with breweries (count): %i", (int)operations.count);
NSString *query = [NSString stringWithFormat:@"%@(", op];
for (int i = 0; i < operations.count; i++) {
if(i >= 0 && i < operations.count -1){
//Middle
query = [query stringByAppendingString:[NSString stringWithFormat:@"RECORD_ID()=\"%@\",%@(", [operations objectAtIndex:i], op]];
}
else{
query = [query stringByAppendingString:[NSString stringWithFormat:@"RECORD_ID()=\"%@\"", [operations objectAtIndex:i]]];
}
}
//Add the remaining parenthesis
for (int i = 0; i < operations.count; i++) {
query = [query stringByAppendingString:@")"];
}
//NSLog(@"Query: %@", query);
return query;
}
-(NSURL *)BeerQuery{
NSURL *temp;
NSString *currentQuery = [self compoundFormulaWithSingleOperator:@"OR" andBeers:self.beerList];
/*
NSLog(@"Beer List");
for (NSString *temp in self.brewery.beerList) {
NSLog(@"Beer: %@", temp);
}*/
temp = [NSURL URLWithString:[[NSString stringWithFormat:@"https://api.airtable.com/v0/app1lDaL7cCCFmKrJ/Beers?filterByFormula=%@&api_key=keyBAo5QorTmqZmN8", currentQuery] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// temp = [NSURL URLWithString:[[NSString stringWithFormat:@"https://api.airtable.com/v0/app1lDaL7cCCFmKrJ/Beers?filterByFormula=Brewery=\"%@\"&api_key=keyBAo5QorTmqZmN8", self.brewery.name] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//NSLog(@"URL: %@", temp.absoluteString);
return temp;
}
-(void)viewDidAppear:(BOOL)animated{
[self loadBeerList];
if(!self.loadedBeerList || self.beers.count != self.beerList.count){
[self loadBeersFromList];
}
/*
for (int i = 0; i < self.beerListCount; i++) {
NSLog(@"BeerList: %@", [self.beerList objectAtIndex:i]);
}*/
MyAnalytics *temp = [[MyAnalytics alloc] init];
[temp viewShown:@"Liked Beers Page"];
[self.mainTable reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(self.loadedBeerList){
if(self.beerListCount > 0){
self.noBeersFound = NO;
noBeersLikedImage.hidden = YES;
return self.beers.count;
}
else{
self.noBeersFound = YES;
[activity stopAnimating];
noBeersLikedImage.hidden = NO;
return 1;
}
}
else{
self.noBeersFound = YES;
[activity stopAnimating];
return 1;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{//NSLog(@"Row Hieght (Home Page): %.0f", tableView.frame.size.height/4);
//return tableView.frame.size.height/2;
return 152;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
[activity stopAnimating];
[activity removeFromSuperview];
if(self.noBeersFound){
//NSLog(@"Nothing Found");
static NSString *identifier = @"NothingFound";
[tableView registerNib:[UINib nibWithNibName:@"NothingFoundTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
// Get the cell's root view and set the table's
// rowHeight to the root cell's height.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NothingFoundTableViewCell"
owner:self
options:nil];
NothingFoundTableViewCell *cell = (NothingFoundTableViewCell *)nib[0];
@try {
cell.label.text = @"No Liked Beers Yet!";
}@catch(NSException *e){
NSLog(@"Exception: %@", e.description);
}
cell.backgroundColor = [UIColor clearColor];
cell.mainView.backgroundColor = cell.backgroundColor;
return cell;
}
else{
static NSString *identifier = @"BeerViewCell";
[tableView registerNib:[UINib nibWithNibName:@"BeerView" bundle:nil] forCellReuseIdentifier:identifier];
// Get the cell's root view and set the table's
// rowHeight to the root cell's height.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BeerView"
owner:self
options:nil];
BeerTableViewCell *cell = (BeerTableViewCell *)nib[0];
if(self.loadedBeerList)
@try {
cell.thisBeer = (Beer *)[self.beers objectAtIndex:indexPath.row];
cell.abvLabel.text = [NSString stringWithFormat:@"%.2f%%", cell.thisBeer.ABV];
cell.ibuLabel.text = (cell.thisBeer.IBU == 0) ? @"N/A" : [NSString stringWithFormat:@"%.0f", cell.thisBeer.IBU];
cell.likeCountLabel.text = [NSString stringWithFormat:@"%i", cell.thisBeer.votes];
//cell.likeCountLabel.text = [NSString stringWithFormat:@"%i", (int)indexPath.row];
cell.beernNameLabel.text = cell.thisBeer.beerName;
//This will change based on the serving style
cell.servingTypeImage.image = [UIImage imageNamed:@"beer.png"];
cell.beerImage.image = [UIImage imageWithCGImage:[cell.thisBeer.image CGImage] scale:2.0f orientation:UIImageOrientationUp];
cell.beerTypeLabel.text = cell.thisBeer.beerType;
cell.isAvailableLabel.text = (cell.thisBeer.isAvail) ? @"Available" : @"Not Available";
cell.servingStyle.text = cell.thisBeer.style;
if([self.likedBeers containsObject:cell.thisBeer.iden])[cell setLiked:YES];
if (!cell.lineAdded) {
//Adjust the rating label
//NSLog(@"Beer Rating: %.2f", cell.thisBeer.rating);
UILabel *beerRatingLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.beernNameLabel.frame.origin.x, cell.beernNameLabel.frame.origin.y + cell.beernNameLabel.frame.size.height, cell.beernNameLabel.frame.size.width, cell.beernNameLabel.frame.size.height/7.5)];
beerRatingLabel.layer.masksToBounds = YES;
beerRatingLabel.layer.cornerRadius = beerRatingLabel.frame.size.height/2;
//beerRatingLabel.backgroundColor = [UIColor greenColor];
[cell.parentView addSubview:beerRatingLabel];
beerRatingLabel.center = CGPointMake(beerRatingLabel.center.x, cell.beernNameLabel.frame.size.height + cell.beernNameLabel.frame.origin.y);
//Now, if the beer rating exists
if (cell.thisBeer.rating > 0) {
[UIView animateWithDuration:5 animations:^{
beerRatingLabel.frame = CGRectMake(beerRatingLabel.frame.origin.x, beerRatingLabel.frame.origin.y, (beerRatingLabel.frame.size.width/10) * cell.thisBeer.rating, beerRatingLabel.frame.size.height);}];
}
else {
beerRatingLabel.backgroundColor = [UIColor colorWithRed:0.380 green:0.490 blue:0.541 alpha:1.00];
}
if(self.view.frame.size.width >= 375){
beerRatingLabel.frame = CGRectMake(cell.beernNameLabel.frame.origin.x, beerRatingLabel.frame.origin.y, beerRatingLabel.frame.size.width, beerRatingLabel.frame.size.height);
}
else{
beerRatingLabel.frame = CGRectMake(cell.beernNameLabel.frame.origin.x/2, beerRatingLabel.frame.origin.y, beerRatingLabel.frame.size.width, beerRatingLabel.frame.size.height);
}
//Now for the colors
if (cell.thisBeer.rating > 0 && cell.thisBeer.rating < 5) beerRatingLabel.backgroundColor = [UIColor colorWithRed:0.827 green:0.125 blue:0.114 alpha:1.00];
if(cell.thisBeer.rating >= 5 && cell.thisBeer.rating < 7.5)beerRatingLabel.backgroundColor = [UIColor colorWithRed:1.000 green:0.886 blue:0.353 alpha:1.00];
if(cell.thisBeer.rating >= 7.5 && cell.thisBeer.rating < 10) beerRatingLabel.backgroundColor = [UIColor colorWithRed:0.482 green:0.765 blue:0.306 alpha:1.00];
if(cell.thisBeer.rating == 10 || cell.thisBeer.rating == 0) beerRatingLabel.backgroundColor = [UIColor colorWithRed:0.380 green:0.490 blue:0.541 alpha:1.00];
cell.lineAdded = YES;
[cell setLiked:YES];
}
//if([likedBeers containsObject:cell.thisBeer.iden]) [cell setLiked:YES];
if (cell.thisBeer.image.description.length == 0) {
cell.beerImage.image = [UIImage imageNamed:@"beer2.png"];
//cell.beerImage.image = self..profilePicture;
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
} @catch (NSException *exception) {
}
//NSLog(@"Beer: %@", [self.brewery.beerList objectAtIndex:indexPath.row]);
return cell;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
BeerDetailViewController *temp = [self.storyboard instantiateViewControllerWithIdentifier:@"beerDetail"];
//temp.thisBrewery = self.brewery;
temp.thisBeer = (Beer *)[self.beers objectAtIndex:indexPath.row];
temp.title = temp.thisBeer.beerName;
temp.ratingsEnabled = YES;
temp.navigationController.toolbar.tintColor = [UIColor whiteColor];
//NSLog(@"Beer Opened: %@", temp.thisBeer.beerName);
//Send an event for dropping Pin
//Google Analytics
/*
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIEventLabel value:@"Home Detail"];
[tracker allowIDFACollection];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"Home Page" action:@"Brewery Selected" label:[NSString stringWithFormat:@"%@ was selected by a user!", temp.thisbrewery.name] value:0] build]];*/
[self.navigationController pushViewController:temp animated:YES];
}
@end