2011-05-31 16:27:34 -05:00
|
|
|
// NearbySpotsViewController.m
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2011 Gowalla (http://gowalla.com/)
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
#import "NearbySpotsViewController.h"
|
2011-07-27 15:14:15 -05:00
|
|
|
|
2011-05-31 16:27:34 -05:00
|
|
|
#import "Spot.h"
|
2011-07-27 15:14:15 -05:00
|
|
|
|
2011-05-31 16:27:34 -05:00
|
|
|
#import "SpotTableViewCell.h"
|
2011-07-27 15:14:15 -05:00
|
|
|
|
2011-05-31 16:27:34 -05:00
|
|
|
#import "TTTLocationFormatter.h"
|
2011-07-27 15:14:15 -05:00
|
|
|
#import "AFImageCache.h"
|
|
|
|
|
#import "UIImageView+AFNetworking.h"
|
2011-05-31 16:27:34 -05:00
|
|
|
|
|
|
|
|
@interface NearbySpotsViewController ()
|
|
|
|
|
@property (readwrite, nonatomic, retain) NSArray *nearbySpots;
|
|
|
|
|
@property (readwrite, nonatomic, retain) CLLocationManager *locationManager;
|
|
|
|
|
@property (readwrite, nonatomic, retain) UIActivityIndicatorView *activityIndicatorView;
|
|
|
|
|
|
|
|
|
|
- (void)loadSpotsForLocation:(CLLocation *)location;
|
|
|
|
|
- (void)refresh:(id)sender;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation NearbySpotsViewController
|
|
|
|
|
@synthesize nearbySpots = _spots;
|
|
|
|
|
@synthesize locationManager = _locationManager;
|
|
|
|
|
@synthesize activityIndicatorView = _activityIndicatorView;
|
|
|
|
|
|
|
|
|
|
- (id)init {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (!self) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.nearbySpots = [NSArray array];
|
|
|
|
|
|
|
|
|
|
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
|
|
|
|
|
self.locationManager.delegate = self;
|
|
|
|
|
self.locationManager.distanceFilter = 80.0;
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
|
[_spots release];
|
|
|
|
|
[_locationManager release];
|
|
|
|
|
[_activityIndicatorView release];
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)loadSpotsForLocation:(CLLocation *)location {
|
|
|
|
|
[self.activityIndicatorView startAnimating];
|
|
|
|
|
self.navigationItem.rightBarButtonItem.enabled = NO;
|
|
|
|
|
|
2011-09-22 10:25:07 -05:00
|
|
|
[Spot spotsWithURLString:@"/spots" near:location parameters:[NSDictionary dictionaryWithObject:@"128" forKey:@"per_page"] block:^(NSArray *records) {
|
2011-05-31 16:27:34 -05:00
|
|
|
self.nearbySpots = [records sortedArrayUsingComparator:^ NSComparisonResult(id obj1, id obj2) {
|
|
|
|
|
CLLocationDistance d1 = [[(Spot *)obj1 location] distanceFromLocation:location];
|
|
|
|
|
CLLocationDistance d2 = [[(Spot *)obj2 location] distanceFromLocation:location];
|
|
|
|
|
|
|
|
|
|
if (d1 < d2) {
|
|
|
|
|
return NSOrderedAscending;
|
|
|
|
|
} else if (d1 > d2) {
|
|
|
|
|
return NSOrderedDescending;
|
|
|
|
|
} else {
|
|
|
|
|
return NSOrderedSame;
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
[self.tableView reloadData];
|
|
|
|
|
|
|
|
|
|
[self.activityIndicatorView stopAnimating];
|
|
|
|
|
self.navigationItem.rightBarButtonItem.enabled = YES;
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - UIViewController
|
|
|
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
|
2011-08-03 11:32:51 -05:00
|
|
|
self.title = NSLocalizedString(@"AFNetworking", nil);
|
2011-05-31 16:27:34 -05:00
|
|
|
|
|
|
|
|
self.activityIndicatorView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
|
|
|
|
|
self.activityIndicatorView.hidesWhenStopped = YES;
|
|
|
|
|
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:self.activityIndicatorView] autorelease];
|
|
|
|
|
|
|
|
|
|
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)] autorelease];
|
|
|
|
|
self.navigationItem.rightBarButtonItem.enabled = NO;
|
|
|
|
|
|
|
|
|
|
[self.navigationController.navigationBar setTintColor:[UIColor darkGrayColor]];
|
|
|
|
|
|
2011-06-01 09:10:33 -05:00
|
|
|
self.tableView.rowHeight = 70.0f;
|
|
|
|
|
|
2011-05-31 16:27:34 -05:00
|
|
|
[self.locationManager startUpdatingLocation];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)viewDidUnload {
|
|
|
|
|
[super viewDidUnload];
|
|
|
|
|
[self.locationManager stopUpdatingLocation];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Actions
|
|
|
|
|
|
|
|
|
|
- (void)refresh:(id)sender {
|
|
|
|
|
self.nearbySpots = [NSArray array];
|
|
|
|
|
[self.tableView reloadData];
|
|
|
|
|
[[NSURLCache sharedURLCache] removeAllCachedResponses];
|
2011-07-27 15:14:15 -05:00
|
|
|
[[AFImageCache sharedImageCache] removeAllObjects];
|
2011-05-31 16:27:34 -05:00
|
|
|
|
|
|
|
|
if (self.locationManager.location) {
|
|
|
|
|
[self loadSpotsForLocation:self.locationManager.location];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-27 15:14:15 -05:00
|
|
|
#pragma mark - CLLocationManagerDelegate
|
|
|
|
|
|
|
|
|
|
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
|
|
|
|
|
[self loadSpotsForLocation:newLocation];
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-03 11:32:51 -05:00
|
|
|
#pragma mark - UITableViewDataSource
|
2011-05-31 16:27:34 -05:00
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
|
|
return [self.nearbySpots count];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
static NSString *CellIdentifier = @"Cell";
|
|
|
|
|
|
|
|
|
|
SpotTableViewCell *cell = (SpotTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
|
|
|
if (cell == nil) {
|
|
|
|
|
cell = [[[SpotTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
|
|
|
|
|
}
|
2011-08-03 11:32:51 -05:00
|
|
|
|
2011-09-16 22:41:21 -05:00
|
|
|
Spot *spot = [self.nearbySpots objectAtIndex:indexPath.row];
|
|
|
|
|
|
2011-08-03 11:32:51 -05:00
|
|
|
static TTTLocationFormatter *_locationFormatter = nil;
|
|
|
|
|
if (!_locationFormatter) {
|
|
|
|
|
_locationFormatter = [[TTTLocationFormatter alloc] init];
|
2011-09-22 11:58:35 -05:00
|
|
|
if (![[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue]) {
|
|
|
|
|
[_locationFormatter setUnitSystem:TTTImperialSystem];
|
|
|
|
|
}
|
2011-08-03 11:32:51 -05:00
|
|
|
}
|
2011-05-31 16:27:34 -05:00
|
|
|
|
|
|
|
|
if (self.locationManager.location) {
|
2011-08-03 11:32:51 -05:00
|
|
|
cell.detailTextLabel.text = [_locationFormatter stringFromDistanceAndBearingFromLocation:self.locationManager.location toLocation:spot.location];
|
2011-05-31 16:27:34 -05:00
|
|
|
}
|
2011-09-16 22:41:21 -05:00
|
|
|
|
|
|
|
|
cell.spot = spot;
|
|
|
|
|
|
2011-05-31 16:27:34 -05:00
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-03 11:32:51 -05:00
|
|
|
#pragma mark - UITableViewDelegate
|
|
|
|
|
|
|
|
|
|
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
|
|
|
|
|
if ([self tableView:tableView numberOfRowsInSection:section] > 0) {
|
|
|
|
|
return NSLocalizedString(@"Nearby Spots", nil);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-31 16:27:34 -05:00
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|