NearbySpotsViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. @implementation NearbySpotsViewController
  36. @synthesize nearbySpots = _spots;
  37. @synthesize locationManager = _locationManager;
  38. @synthesize activityIndicatorView = _activityIndicatorView;
  39. - (id)init {
  40. self = [super init];
  41. if (!self) {
  42. return nil;
  43. }
  44. self.nearbySpots = [NSArray array];
  45. self.locationManager = [[[CLLocationManager alloc] init] autorelease];
  46. self.locationManager.delegate = self;
  47. self.locationManager.distanceFilter = 80.0;
  48. return self;
  49. }
  50. - (void)dealloc {
  51. [_spots release];
  52. [_locationManager release];
  53. [_activityIndicatorView release];
  54. [super dealloc];
  55. }
  56. - (void)loadSpotsForLocation:(CLLocation *)location {
  57. [self.activityIndicatorView startAnimating];
  58. self.navigationItem.rightBarButtonItem.enabled = NO;
  59. [Spot spotsWithURLString:@"/spots/advanced_search" near:location parameters:[NSDictionary dictionaryWithObject:@"128" forKey:@"per_page"] block:^(NSArray *records) {
  60. self.nearbySpots = [records sortedArrayUsingComparator:^ NSComparisonResult(id obj1, id obj2) {
  61. CLLocationDistance d1 = [[(Spot *)obj1 location] distanceFromLocation:location];
  62. CLLocationDistance d2 = [[(Spot *)obj2 location] distanceFromLocation:location];
  63. if (d1 < d2) {
  64. return NSOrderedAscending;
  65. } else if (d1 > d2) {
  66. return NSOrderedDescending;
  67. } else {
  68. return NSOrderedSame;
  69. }
  70. }];
  71. [self.tableView reloadData];
  72. [self.activityIndicatorView stopAnimating];
  73. self.navigationItem.rightBarButtonItem.enabled = YES;
  74. }];
  75. }
  76. #pragma mark - UIViewController
  77. - (void)viewDidLoad {
  78. [super viewDidLoad];
  79. self.title = NSLocalizedString(@"AFNetworking", nil);
  80. self.activityIndicatorView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
  81. self.activityIndicatorView.hidesWhenStopped = YES;
  82. self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:self.activityIndicatorView] autorelease];
  83. self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)] autorelease];
  84. self.navigationItem.rightBarButtonItem.enabled = NO;
  85. [self.navigationController.navigationBar setTintColor:[UIColor darkGrayColor]];
  86. self.tableView.rowHeight = 70.0f;
  87. [self.locationManager startUpdatingLocation];
  88. }
  89. - (void)viewDidUnload {
  90. [super viewDidUnload];
  91. [self.locationManager stopUpdatingLocation];
  92. }
  93. #pragma mark - Actions
  94. - (void)refresh:(id)sender {
  95. self.nearbySpots = [NSArray array];
  96. [self.tableView reloadData];
  97. [[NSURLCache sharedURLCache] removeAllCachedResponses];
  98. [[AFImageCache sharedImageCache] removeAllObjects];
  99. if (self.locationManager.location) {
  100. [self loadSpotsForLocation:self.locationManager.location];
  101. }
  102. }
  103. #pragma mark - CLLocationManagerDelegate
  104. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
  105. [self loadSpotsForLocation:newLocation];
  106. }
  107. #pragma mark - UITableViewDataSource
  108. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  109. return 1;
  110. }
  111. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  112. return [self.nearbySpots count];
  113. }
  114. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  115. static NSString *CellIdentifier = @"Cell";
  116. SpotTableViewCell *cell = (SpotTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  117. if (cell == nil) {
  118. cell = [[[SpotTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
  119. }
  120. Spot *spot = [self.nearbySpots objectAtIndex:indexPath.row];
  121. static TTTLocationFormatter *_locationFormatter = nil;
  122. if (!_locationFormatter) {
  123. _locationFormatter = [[TTTLocationFormatter alloc] init];
  124. [_locationFormatter setUnitSystem:TTTImperialSystem];
  125. }
  126. if (self.locationManager.location) {
  127. cell.detailTextLabel.text = [_locationFormatter stringFromDistanceAndBearingFromLocation:self.locationManager.location toLocation:spot.location];
  128. }
  129. cell.spot = spot;
  130. return cell;
  131. }
  132. #pragma mark - UITableViewDelegate
  133. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  134. if ([self tableView:tableView numberOfRowsInSection:section] > 0) {
  135. return NSLocalizedString(@"Nearby Spots", nil);
  136. }
  137. return nil;
  138. }
  139. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  140. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  141. }
  142. @end