NearbySpotsViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // NearbySpotsViewController.m
  2. //
  3. // Copyright (c) 2011 Gowalla (http://gowalla.com/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "NearbySpotsViewController.h"
  23. #import "Spot.h"
  24. #import "SpotTableViewCell.h"
  25. #import "TTTLocationFormatter.h"
  26. #import "AFImageCache.h"
  27. #import "UIImageView+AFNetworking.h"
  28. @interface NearbySpotsViewController ()
  29. @property (readwrite, nonatomic, retain) NSArray *nearbySpots;
  30. @property (readwrite, nonatomic, retain) CLLocationManager *locationManager;
  31. @property (readwrite, nonatomic, retain) UIActivityIndicatorView *activityIndicatorView;
  32. - (void)loadSpotsForLocation:(CLLocation *)location;
  33. - (void)refresh:(id)sender;
  34. @end
  35. static TTTLocationFormatter *__locationFormatter;
  36. @implementation NearbySpotsViewController
  37. @synthesize nearbySpots = _spots;
  38. @synthesize locationManager = _locationManager;
  39. @synthesize activityIndicatorView = _activityIndicatorView;
  40. + (void)initialize {
  41. __locationFormatter = [[TTTLocationFormatter alloc] init];
  42. [__locationFormatter setUnitSystem:TTTImperialSystem];
  43. }
  44. - (id)init {
  45. self = [super init];
  46. if (!self) {
  47. return nil;
  48. }
  49. self.nearbySpots = [NSArray array];
  50. self.locationManager = [[[CLLocationManager alloc] init] autorelease];
  51. self.locationManager.delegate = self;
  52. self.locationManager.distanceFilter = 80.0;
  53. return self;
  54. }
  55. - (void)dealloc {
  56. [_spots release];
  57. [_locationManager release];
  58. [_activityIndicatorView release];
  59. [super dealloc];
  60. }
  61. - (void)loadSpotsForLocation:(CLLocation *)location {
  62. [self.activityIndicatorView startAnimating];
  63. self.navigationItem.rightBarButtonItem.enabled = NO;
  64. [Spot spotsWithURLString:@"/spots/advanced_search" near:location parameters:[NSDictionary dictionaryWithObject:@"128" forKey:@"per_page"] block:^(NSArray *records) {
  65. self.nearbySpots = [records sortedArrayUsingComparator:^ NSComparisonResult(id obj1, id obj2) {
  66. CLLocationDistance d1 = [[(Spot *)obj1 location] distanceFromLocation:location];
  67. CLLocationDistance d2 = [[(Spot *)obj2 location] distanceFromLocation:location];
  68. if (d1 < d2) {
  69. return NSOrderedAscending;
  70. } else if (d1 > d2) {
  71. return NSOrderedDescending;
  72. } else {
  73. return NSOrderedSame;
  74. }
  75. }];
  76. [self.tableView reloadData];
  77. [self.activityIndicatorView stopAnimating];
  78. self.navigationItem.rightBarButtonItem.enabled = YES;
  79. }];
  80. }
  81. #pragma mark - UIViewController
  82. - (void)viewDidLoad {
  83. [super viewDidLoad];
  84. self.title = NSLocalizedString(@"Nearby Spots", nil);
  85. self.activityIndicatorView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
  86. self.activityIndicatorView.hidesWhenStopped = YES;
  87. self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:self.activityIndicatorView] autorelease];
  88. self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)] autorelease];
  89. self.navigationItem.rightBarButtonItem.enabled = NO;
  90. [self.navigationController.navigationBar setTintColor:[UIColor darkGrayColor]];
  91. self.tableView.rowHeight = 70.0f;
  92. [self.locationManager startUpdatingLocation];
  93. }
  94. - (void)viewDidUnload {
  95. [super viewDidUnload];
  96. [self.locationManager stopUpdatingLocation];
  97. }
  98. #pragma mark - Actions
  99. - (void)refresh:(id)sender {
  100. self.nearbySpots = [NSArray array];
  101. [self.tableView reloadData];
  102. [[NSURLCache sharedURLCache] removeAllCachedResponses];
  103. [[AFImageCache sharedImageCache] removeAllObjects];
  104. if (self.locationManager.location) {
  105. [self loadSpotsForLocation:self.locationManager.location];
  106. }
  107. }
  108. #pragma mark - CLLocationManagerDelegate
  109. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
  110. [self loadSpotsForLocation:newLocation];
  111. }
  112. #pragma mark - UITableViewDelegate
  113. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  114. return 1;
  115. }
  116. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  117. return [self.nearbySpots count];
  118. }
  119. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  120. static NSString *CellIdentifier = @"Cell";
  121. SpotTableViewCell *cell = (SpotTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  122. if (cell == nil) {
  123. cell = [[[SpotTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
  124. }
  125. Spot *spot = [self.nearbySpots objectAtIndex:indexPath.row];
  126. cell.textLabel.text = spot.name;
  127. if (self.locationManager.location) {
  128. cell.detailTextLabel.text = [__locationFormatter stringFromDistanceAndBearingFromLocation:self.locationManager.location toLocation:spot.location];
  129. }
  130. [cell.imageView setImageWithURL:[NSURL URLWithString:spot.imageURLString] placeholderImage:[UIImage imageNamed:@"placeholder-stamp.png"]];
  131. return cell;
  132. }
  133. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  134. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  135. }
  136. @end