/
YelpWebPageBrowser.m
293 lines (215 loc) · 9.61 KB
/
YelpWebPageBrowser.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
//
// YelpWebPageBrowser.m
// MappingMashupApp
//
// Created by Ross Matsuda on 3/17/13.
// Copyright (c) 2013 The Peanut Gallery. All rights reserved.
//
#import "YelpWebPageBrowser.h"
#import "Business.h"
#import "AppDelegate.h"
#import "NSString+Extended.h"
#import "BookmarkedBusiness.h"
@interface YelpWebPageBrowser ()
{
__weak IBOutlet UIView *popoutView;
__weak IBOutlet UILabel *popoutViewTextLabel;
__weak IBOutlet UIWebView *webView;
Business *currentBusiness;
__weak IBOutlet UIButton *backButton;
__weak IBOutlet UIButton *forwardButton;
}
//Add buttons for left/right here
- (IBAction)backButtonPress:(id)sender;
- (IBAction)forwardButtonPress:(id)sender;
@end
@implementation YelpWebPageBrowser
@synthesize yelpURLString,managedObjectContext, name, latitude, longitude, phone, viewDate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Custom formatting for navigation bar
// Core Data
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
self.managedObjectContext = appDelegate.managedObjectContext;
currentBusiness = [self fetchBusinessShownInWebpage];
//Adds bookmark button to top right corner
//self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBookmark)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"addBookmarkC"] style:UIBarButtonItemStyleBordered target:self action:@selector(addBookmark)];
//Enable button just in case
[self.navigationItem.rightBarButtonItem setEnabled:YES];
[self fetchToSeeIfCurrentBusinessIsBookmarkedAndSetButtonDisabled];
//Check to see if this venue is already bookmarked
// if (venue.isBookmarked == YES)
// {
// NSLog(@"Page already bookmarked");
// [self.navigationItem.rightBarButtonItem setEnabled:NO];
// }
//Load yelp page
NSString *homePage = [NSString stringWithFormat:@"%@", yelpURLString];
NSURL * url = [NSURL URLWithString:homePage];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest: urlRequest];
//Scroll off "swipe to navigate" message after 1.5 seconds
//Hold for 1.5 seconds, then scroll off left side.
// [UIView animateWithDuration:1.0 animations:^(void)
// {popoutView.center = CGPointMake(popoutView.center.x, popoutView.center.y-10);}
// completion:^(BOOL finished){
// [UIView animateWithDuration:2.5 animations:
// ^{popoutView.center = CGPointMake(popoutView.center.x, popoutView.center.y+200);
// popoutView.alpha = 0;
// }
// ];
// }
// ];
[UIView animateWithDuration:0.0
delay:0.0
options: UIViewAnimationCurveEaseIn
animations:^{
popoutView.center = CGPointMake(popoutView.center.x, popoutView.center.y+200);
popoutView.alpha = 0;
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
backButton.enabled = NO;
forwardButton.enabled = NO;
//End of method bracket
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//Code to manage back/forward buttons
if ([webView canGoBack] == NO)
{
backButton.enabled = NO;
}
else
{
backButton.enabled = YES;
}
if ([webView canGoForward] == NO)
{
forwardButton.enabled = NO;
}
else
{
forwardButton.enabled = YES;
}
}
-(void)fetchToSeeIfCurrentBusinessIsBookmarkedAndSetButtonDisabled
{
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"BookmarkedBusiness" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc]init];
NSFetchedResultsController * fetchResultsController;
//Now customize your search! We'd want to switch this to see if isBookmarked == true
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:nil];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"yelpURLString == %@", yelpURLString];
NSError *searchError;
//Lock and load
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entityDescription];
fetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
[fetchResultsController performFetch:&searchError];
NSLog(@"------ fetchresults Array: %@", fetchResultsController.fetchedObjects);
if ([fetchResultsController.fetchedObjects count] > 0) {
[self.navigationItem.rightBarButtonItem setEnabled: NO];
}
}
-(void) addBookmark
{
//this code will change the bookmark attribute on the currentbusiness "Business" class object
//AND create a new "BoomarkedBusiness" class object with the same attributes, so that it may persist even if removed from the history list
currentBusiness.isBookmarked = [NSNumber numberWithBool:YES];
BookmarkedBusiness *businessToBookmark = [NSEntityDescription insertNewObjectForEntityForName:@"BookmarkedBusiness" inManagedObjectContext:managedObjectContext];
businessToBookmark.name = name;
businessToBookmark.phone = phone;
businessToBookmark.latitude = latitude;
businessToBookmark.longitude = longitude;
businessToBookmark.yelpURLString = yelpURLString;
businessToBookmark.viewDate = viewDate;
businessToBookmark.isBookmarked = [NSNumber numberWithBool:YES];
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"failed to save: %@", [error userInfo]);
}
popoutViewTextLabel.text = @"Bookmark added";
//Scroll on, pause for 1.5 seconds, scroll off
[UIView animateWithDuration:0.5 animations:^(void)
{popoutView.center = CGPointMake(popoutView.center.x, popoutView.center.y-201);
popoutView.alpha = 0.90;}
completion:^(BOOL finished){
[UIView animateWithDuration:1.25 animations:^(void)
{popoutView.center = CGPointMake(popoutView.center.x, popoutView.center.y);
popoutView.alpha = 1;}
completion:^(BOOL finished){
[UIView animateWithDuration:0.5 animations:
^{popoutView.center = CGPointMake(popoutView.center.x, popoutView.center.y+200);
popoutView.alpha = 0;}
];
}
];
}
];
//Disable button so they can't bookmark the same page again.
[self.navigationItem.rightBarButtonItem setEnabled:NO];
}
//Add to bookmarks.
-(void)addBookmarkStatusTo: (Business*)business
{
//venue.isBookmarked = YES;
NSError *error;
if (![self.managedObjectContext save:&error])
{
NSLog(@"Add bookmark status failed.");
}
}
//fetch the managed object of the business we're looking at in this webView
-(Business*) fetchBusinessShownInWebpage
{
NSArray *businessesArray;
Business *business;
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Business" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc]init];
NSFetchedResultsController * fetchResultsController;
NSString *predicateURLString = [yelpURLString urlencode];
//Now customize your search! We'd want to switch this to see if isBookmarked == true
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:nil];
NSPredicate * predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"yelpURLString == '%@'", yelpURLString]];
NSError *searchError;
//Lock and load
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entityDescription];
fetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
[fetchResultsController performFetch:&searchError];
//Something about making the arrays equal size or some shit. Gawd.
//This will update your display array with your fetch results
businessesArray = fetchResultsController.fetchedObjects;
if (businessesArray != nil && businessesArray.count > 0) {
business = [businessesArray objectAtIndex:0];
}
return business;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Button presses to go back and forward
- (IBAction)backButtonPress:(id)sender {
[webView goBack];
}
- (IBAction)forwardButtonPress:(id)sender {
[webView goForward];
}
@end