Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // TableViewController.m
- // foursquareRequests
- //
- // Created by Steven Palomino on 3/15/14.
- // Copyright (c) 2014 Steven Palomino Design. All rights reserved.
- //
- #import "TableViewController.h"
- #import "AFNetworking.h"
- #import "CustomObject.h"
- #import "DetailCellViewController.h"
- #import "AllBusinessesViewController.h"
- #import "VenuesTableViewCell.h"
- #import "UIImageView+WebCache.h"
- #import "MBProgressHUD.h"
- #import "VenuesTableViewCell.h"
- @interface TableViewController ()
- @end
- @implementation TableViewController{
- void (^progressHandler)(NSString *someValue);
- }
- @synthesize longitude, latitude;
- -(void)viewWillAppear:(BOOL)animated
- {
- self.navigationController.navigationBar.topItem.title = @"Closest Cup";
- self.title = @"Closest Cup";
- [self.navigationController.navigationBar setTitleTextAttributes:
- [NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont fontWithName:@"Poetsen One" size:29],
- NSFontAttributeName,
- [UIColor whiteColor], NSForegroundColorAttributeName,
- nil]];
- //Set background of tableview to "click this button" image
- //_myTableView.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrowClick2.png"]];
- //set navbar text color to white and background color to brown-ish
- self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
- self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.18 green:0.16 blue:0.16 alpha:1];
- //make sure no cells are still highlighted when tableview appears
- NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
- [_myTableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- -(void)gestureHandler:(UITapGestureRecognizer*)sender
- {
- NSLog(@"works!");
- }
- -(void)loadVenues{
- NSLog(@"Your location - Lat:%@, Long:%@", latitude, longitude);
- //CustomObject *customObject = [[CustomObject alloc]init];
- NSString *base = @"https://api.foursquare.com/v2/venues/explore?client_id=SK34ZCSYWESWVO501VCNPXY5T4ZZPEJSRLVYIBB2OPABNHCY&client_secret=M45XCWUBM23SD00JFTRBKCJSRQLW2VI2CDQ2TEUY0UEKKOGN%20&v=20130815&ll=";
- NSString *coordinatesString = [NSString stringWithFormat:@"%@%@,%@&query=coffee&radius=7000", base,latitude, longitude];
- NSURL *url = [NSURL URLWithString:coordinatesString];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- operation.responseSerializer = [AFJSONResponseSerializer serializer];
- //Starts progress indicator to show user content is loading
- MBProgressHUD *loading = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
- loading.labelText = @"Finding Coffee";
- [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
- self.venuesDictionary = (NSDictionary *)responseObject;
- //Populate arrays with data from responseObject being pulled from foursquare
- _venues = [[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"name"];
- _lat = [[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"location"]valueForKey:@"lat"];
- _lon = [[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"location"]valueForKey:@"lng"];
- _distance = [[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"location"]valueForKey:@"distance"];
- _prefix = [[[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"categories"]valueForKey:@"icon"]valueForKey:@"prefix"];
- _address = [[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"location"]valueForKey:@"address"];
- _city = [[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"location"]valueForKey:@"city"];
- _state = [[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"location"]valueForKey:@"state"];
- _zip = [[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"location"]valueForKey:@"postalCode"];
- _phoneNumber = [[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"contact"]valueForKey:@"formattedPhone"];
- _website = [[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"url"];
- _rating = [[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"rating"];
- _phone = [[[[[_venuesDictionary[@"response"]valueForKey:@"groups"]valueForKey:@"items"]valueForKey:@"venue"]valueForKey:@"contact"]valueForKey:@"phone"];
- //NSLog(@"rating: %i",[[_venues objectAtIndex:0] count] );
- // for (int i = 0; i < [[_website objectAtIndex:0]count]; i++) {
- // if ([[_website objectAtIndex:0]objectAtIndex:i] != [NSNull null]) {
- // NSLog(@"not this one");
- // }else{
- // NSLog(@"this one's null");
- // }
- // }
- //NSLog(@"one: %@", [_prefix objectAtIndex:0]);
- //Address string concatenation
- _addressArray = [[NSMutableArray alloc]init];
- for (int i = 0; i < [_address count]; i++) {
- NSString *address = [[NSString alloc]init];
- address = [NSString stringWithFormat:@"%@\n%@, %@ %@", [_address objectAtIndex:i],[_city objectAtIndex:i],[_state objectAtIndex:i],[_zip objectAtIndex:i]];
- [_addressArray addObject:address];
- }
- //miles array and formatter methods
- _miles = [[NSMutableArray alloc]init];
- NSNumber *currentNumber = [[NSNumber alloc]init];
- NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
- [formatter setMaximumFractionDigits:2];
- [formatter setMinimum:0];
- //loops through distance array and converts to miles, only allows 2 decimal places, then puts it in an array
- for (int i = 0; i < [[_distance objectAtIndex:0] count]; i++) {
- float currentFloat = [[[_distance objectAtIndex:0]objectAtIndex:i] floatValue] / 1609.34;
- currentNumber = [NSNumber numberWithFloat:currentFloat];
- NSString *formatted = [formatter stringFromNumber:currentNumber];
- [_miles addObject:formatted];
- }
- //Adds the 88px suffix to all _prefix array strings then adds them to a _paths array
- _paths = [NSMutableArray array];
- //NSLog(@"prefix: %@", [_prefix objectAtIndex:0]);
- //NSLog(@"venues: %@", responseObject);
- for (int i = 0; i < [[_venues objectAtIndex:0] count]; i++) {
- if ([[[_prefix objectAtIndex:0]objectAtIndex:i]count] != 0) {
- NSMutableString *pathStrings = [[NSMutableString alloc]initWithFormat:@"%@88.png", [[[_prefix objectAtIndex:0]objectAtIndex:i] objectAtIndex:0]];
- [_paths addObject:pathStrings];
- }else if ([[[_prefix objectAtIndex:0]objectAtIndex:i]count] == 0){
- NSMutableString *pathStrings = [[NSMutableString alloc]initWithString:@""];
- [_paths addObject:pathStrings];
- }
- }
- //creates array of custom objects with venue name and coordinates
- //_objects = [NSMutableArray array];
- _addObjects = [[NSMutableArray alloc]init];
- CustomObject *customObject = [[CustomObject alloc]init];
- for (int i = 0; i < [[_venues objectAtIndex:0]count]; i++) {
- if ([[_venues objectAtIndex:0]objectAtIndex:i] != nil) {
- NSString *lati = [[_lat objectAtIndex:0]objectAtIndex:i];
- NSString *longi = [[_lon objectAtIndex:0]objectAtIndex:i];
- CLLocationCoordinate2D positionCoords = CLLocationCoordinate2DMake([lati doubleValue], [longi doubleValue]);
- [_addObjects addObject:[customObject initWithName:[[_venues objectAtIndex:0]objectAtIndex:i] coordinates:positionCoords website:[[_website objectAtIndex:0]objectAtIndex:i] phone:[[_phoneNumber objectAtIndex:0]objectAtIndex:i] address:[[_address objectAtIndex:0]objectAtIndex:i] rating:[[_rating objectAtIndex:0]objectAtIndex:i] miles:[_miles objectAtIndex:i]]];
- }
- }
- #warning sort
- NSLog(@"%@", _addObjects);
- // [_addObjects sortUsingComparator: ^(id obj1, id obj2) {
- // NSString *dist1 = [obj1 objectAtIndex:1];
- // NSString *dist2 = [obj2 objectAtIndex:1];
- //
- // return [dist1 compare:dist2];
- // }];
- NSSortDescriptor *sortDescriptor;
- sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"_miles"
- ascending:YES];
- NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
- NSArray *sortedArray = [_addObjects sortedArrayUsingDescriptors:sortDescriptors];
- _objects = [NSMutableArray arrayWithArray:sortedArray];
- //NSLog(@"%@", _objects);
- //NSLog(@"objects: %@", [[_objects objectAtIndex:0]description]);
- [_myTableView reloadData];
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving venues"
- message:[error localizedDescription]
- delegate:nil
- cancelButtonTitle:@"Ok"
- otherButtonTitles:nil];
- [alertView show];
- }];
- [operation start];
- }
- - (IBAction)getJosephs:(UIBarButtonItem *)sender {
- [self loadVenues];
- }
- - (void)viewDidLoad
- {
- //This needs to be loaded first before shops are found because the search uses the coordinates to
- locationManager = [[CLLocationManager alloc]init];
- locationManager.delegate = self;
- locationManager.desiredAccuracy = kCLLocationAccuracyBest;
- [locationManager startUpdatingLocation];
- //#warning don't run loadVenues until location is received
- NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self
- selector: @selector(loadVenues) userInfo: nil repeats: NO];
- //[self loadVenues];
- [super viewDidLoad];
- }
- -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- {
- CLLocation *location = [locations lastObject];
- latitude = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
- longitude = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
- }
- #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 [[_venues objectAtIndex:0]count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //NSLog(@"%@", _objects);
- VenuesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
- CustomObject *currentObject = [_objects objectAtIndex:indexPath.row];
- cell.venuesTitle.text = [currentObject nameOfVenue];
- NSString *preDistance = @"about";
- NSString *postDistance = @"miles";
- cell.venuesSubTitle.text = [NSString stringWithFormat:@"%@ %@ %@", preDistance,[_miles objectAtIndex:indexPath.row] ,postDistance];
- if ([[_phoneNumber objectAtIndex:0]objectAtIndex:indexPath.row] != [NSNull null]) {
- cell.phoneIcon.alpha = 1;
- }else{
- cell.phoneIcon.alpha = .4;
- }
- if ([[_website objectAtIndex:0]objectAtIndex:indexPath.row] != [NSNull null]) {
- cell.webIcon.alpha = 1;
- }else{
- cell.webIcon.alpha = .4;
- }
- if ([[_rating objectAtIndex:0]objectAtIndex:indexPath.row] != [NSNull null]) {
- cell.ratingIcon.alpha = 1;
- }else{
- cell.ratingIcon.alpha = .4;
- }
- if (cell != nil) {
- NSMutableString *imagePath = [NSMutableString stringWithFormat:@"%@", [_paths objectAtIndex:indexPath.row]];
- [cell.venuesImage setContentMode:UIViewContentModeScaleAspectFit];
- [cell.venuesImage setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:[UIImage imageNamed:@"[email protected]"]];
- }
- return cell;
- }
- -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- {
- DetailCellViewController *DetailCVC = segue.destinationViewController;
- UITableViewCell *cell = (UITableViewCell*)sender;
- NSIndexPath *indexPath = [_myTableView indexPathForCell:cell];
- if ([segue.identifier isEqualToString:@"toDetails"]) {
- CustomObject *currentObject = [_objects objectAtIndex:indexPath.row];
- DetailCVC.currentObject = currentObject;
- DetailCVC.phone = [[_phone objectAtIndex:0]objectAtIndex:indexPath.row];
- }
- if ([segue.identifier isEqualToString:@"toAllBusinesses"]) {
- AllBusinessesViewController *AllBusinessesVC = segue.destinationViewController;
- AllBusinessesVC.objects = _objects;
- AllBusinessesVC.latitude = latitude;
- AllBusinessesVC.longitude = longitude;
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement