Permalink
Browse files

Re-implementing check-in

Fixing errors in original implementation of OAuth

Miscellaneous UI cleanup and bugfixes
  • Loading branch information...
1 parent 3818596 commit 9bbc5d692ce512e495b3e364e002aa5abf67e657 @mattt mattt committed Aug 5, 2010
@@ -17,7 +17,7 @@ @implementation AuthenticationViewController
- (void)viewDidLoad {
[super viewDidLoad];
- NSString * OAuthURLString = [kGowallaOAuthURL stringByAppendingFormat:@"?redirect_uri=%@&client_id=%@", kGowallaRedirectURI, kGowallaAPIKey];
+ NSString * OAuthURLString = [kGowallaOAuthURL stringByAppendingFormat:@"?redirect_uri=%@&client_id=%@&scope=%@", kGowallaRedirectURI, kGowallaAPIKey, @"read-write"];
NSURL * OAuthURL = [NSURL URLWithString:OAuthURLString];
NSURLRequest * OAuthURLRequest = [[[NSURLRequest alloc] initWithURL:OAuthURL] autorelease];
@@ -17,15 +17,22 @@
@interface SpotViewController : UITableViewController <CLLocationManagerDelegate> {
Spot * spot;
NSArray * checkIns;
-
+
+ CLLocationManager * locationManager;
+
IBOutlet UILabel * nameLabel;
IBOutlet EGOImageView * imageView;
+ IBOutlet UIButton * checkInButton;
IBOutlet MKMapView * mapView;
}
@property (nonatomic, retain) Spot * spot;
@property (nonatomic, retain) NSArray * checkIns;
+@property (nonatomic, retain) CLLocationManager * locationManager;
+
- (id)initWithSpot:(Spot *)someSpot;
-@end
+- (IBAction)checkIn:(id)sender;
+
+@end
@@ -25,6 +25,7 @@
@interface SpotViewController ()
- (void)updateContent;
+- (void)handleCheckInState;
@end
static NSDateFormatter * _dateFormatter;
@@ -36,20 +37,41 @@ @implementation SpotViewController
@synthesize spot;
@synthesize checkIns;
+@synthesize locationManager;
- (id)initWithSpot:(Spot *)someSpot {
if (self = [super initWithNibName:@"SpotView" bundle:nil]) {
self.spot = someSpot;
+
+ self.locationManager = [[CLLocationManager alloc] init];
+ self.locationManager.delegate = self;
+ self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
+ self.locationManager.distanceFilter = 80.0;
}
return self;
}
-- (void)dealloc {
+- (void)dealloc {
+ self.locationManager.delegate = nil;
+
[spot release];
+ [locationManager release];
[super dealloc];
}
+- (void)handleCheckInState {
+ if ([Spot canCheckInAtSpot:self.spot fromLocation:self.locationManager.location]) {
+ [checkInButton setTitle:NSLocalizedString(@"Check In", nil)
+ forState:UIControlStateNormal];
+ checkInButton.enabled = YES;
+ } else {
+ [checkInButton setTitle:[self.locationManager distanceAndDirectionTo:self.spot.location]
+ forState:UIControlStateDisabled];
+ checkInButton.enabled = NO;
+ }
+}
+
#pragma mark -
#pragma mark View Lifecycle
@@ -70,25 +92,61 @@ - (void)viewDidLoad {
parameters:nil
delegate:self
selector:@selector(requestDidFinish:)];
-
- [spotRequest startAsynchronous];
+
+ [spotRequest startAsynchronous];
+
+ [self.locationManager startUpdatingLocation];
}
- (void)viewDidUnload {
[super viewDidUnload];
nameLabel = nil;
imageView = nil;
mapView = nil;
+ [self.locationManager stopUpdatingLocation];
}
- (void)updateContent {
nameLabel.text = self.spot.name;
[imageView setImageURL:self.spot.imageURL];
+ checkInButton.titleLabel.numberOfLines = 2;
+ checkInButton.titleLabel.adjustsFontSizeToFitWidth = YES;
+
[mapView addAnnotation:self.spot];
[mapView setRegion:MKCoordinateRegionMakeWithDistance(self.spot.coordinate, 1000, 1000) animated:YES];
}
+#pragma mark -
+#pragma mark IBAction
+
+- (IBAction)checkIn:(id)sender {
+ [checkInButton setEnabled:NO];
+
+ CLLocation * currentLocation = self.locationManager.location;
+ CLLocationAccuracy accuracy = currentLocation.horizontalAccuracy;
+ CLLocationDegrees latitude = currentLocation.coordinate.latitude;
+ CLLocationDegrees longitude = currentLocation.coordinate.longitude;
+
+ NSString * URLString = [@"https://api.gowalla.com/checkins.json" stringByAppendingFormat:@"?oauth_token=%@", [[NSUserDefaults standardUserDefaults] objectForKey:kGowallaBasicOAuthAccessTokenPreferenceKey]];
+
+ checkInRequest = [[EGOHTTPFormRequest alloc] initWithURL:[NSURL URLWithString:URLString]
+ delegate:self];
+ [checkInRequest setDidFailSelector:@selector(requestDidFinish:)];
+
+ [checkInRequest setPostValue:self.spot.identifier
+ forKey:@"spot_id"];
+ [checkInRequest setPostValue:[[NSNumber numberWithDouble:latitude] stringValue]
+ forKey:@"lat"];
+ [checkInRequest setPostValue:[[NSNumber numberWithDouble:longitude] stringValue]
+ forKey:@"lng"];
+ [checkInRequest setPostValue:[[NSNumber numberWithDouble:accuracy] stringValue]
+ forKey:@"accuracy"];
+
+
+ [checkInRequest startSynchronous];
+}
+
#pragma mark -
#pragma mark EGOHTTPRequest
@@ -104,7 +162,7 @@ - (void)requestDidFinish:(EGOHTTPRequest *)request {
[self.tableView reloadData];
} else if ([request isEqual:checkInRequest]) {
- CheckIn * checkIn = [[CheckIn alloc] initWithDictionary:response];
+ CheckIn * checkIn = [[[CheckIn alloc] initWithDictionary:response] autorelease];
NSString * html = [response valueForKey:@"detail_html"];
CheckInSuccessViewController * viewController = [[[CheckInSuccessViewController alloc] initWithCheckIn:checkIn
@@ -114,12 +172,25 @@ - (void)requestDidFinish:(EGOHTTPRequest *)request {
[self.navigationController presentModalViewController:modalNavigationController animated:YES];
}
} else {
- [[[[UIAlertView alloc] initWithTitle:[response valueForKey:@"title"]
- message:[response valueForKey:@"message"]
+ [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil)
+ message:request.responseString
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil] autorelease] show];
- }
+ }
+
+ [self handleCheckInState];
+}
+
+#pragma mark -
+#pragma mark CLLocationManagerDelegate
+
+- (void)locationManager:(CLLocationManager *)manager
+ didUpdateToLocation:(CLLocation *)newLocation
+ fromLocation:(CLLocation *)oldLocation
+{
+ NSLog(@"locationManager:didUpdateToLocation:fromLocation:");
+ [self handleCheckInState];
}
#pragma mark -
@@ -156,7 +227,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}
CheckIn * checkIn = [self.checkIns objectAtIndex:indexPath.row];
-
+
[cell setImageURL:checkIn.user.imageURL];
cell.textLabel.text = checkIn.user.name;
cell.detailTextLabel.text = [_dateFormatter stringFromDate:checkIn.timestamp];
@@ -174,4 +245,3 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
}
@end
-
@@ -144,9 +144,9 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
+ static NSString * CellIdentifier = @"Cell";
- AFImageLoadingCell *cell = (AFImageLoadingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+ AFImageLoadingCell * cell = (AFImageLoadingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[AFImageLoadingCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
@@ -11,6 +11,7 @@
#import "AuthenticationViewController.h"
#import "EGOHTTPFormRequest.h"
+#import "URLParser.h"
@implementation Gowalla_BasicAppDelegate
@@ -41,11 +42,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSLog(@"application:handleOpenURL: %@", url);
- NSString * URLString = [url absoluteString];
- NSString * codeKey = @"code=";
- NSRange codeRange = [URLString rangeOfString:codeKey];
- NSString * code = [URLString substringFromIndex:codeRange.location + [codeKey length]];
-
+ URLParser * parser = [[[URLParser alloc] initWithURLString:[url absoluteString]] autorelease];
+ NSString * code = [parser valueForVariable:@"code"];
NSURL * OAuthTokenURL = [NSURL URLWithString:@"https://api.gowalla.com/api/oauth/token"];
EGOHTTPFormRequest * request = [[[EGOHTTPFormRequest alloc] initWithURL:OAuthTokenURL delegate:self] autorelease];
@@ -57,15 +55,17 @@ - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
forKey:@"client_secret"];
[request setPostValue:code
forKey:@"code"];
- [request setPostValue:kGowallaRedirectURI
+ [request setPostValue:kGowallaRedirectURI
forKey:@"redirect_uri"];
+ [request setPostValue:@"read-write"
+ forKey:@"scope"];
[request setDidFinishSelector:@selector(applicationDidAuthorizeWithRequest:)];
[request startSynchronous];
return YES;
}
-
+
- (void)applicationDidAuthorizeWithRequest:(EGOHTTPFormRequest *)request {
NSLog(@"applicationDidAuthorizeWithRequest: %@", request.responseString);
NSDictionary * response = [NSDictionary dictionaryWithJSONData:[request responseData] error:nil];
@@ -80,7 +80,26 @@ - (void)applicationDidAuthorizeWithRequest:(EGOHTTPFormRequest *)request {
forKey:kGowallaBasicOAuthTokenExpirationPreferenceKey];
[[NSUserDefaults standardUserDefaults] synchronize];
- [authenticationViewController dismissModalViewControllerAnimated:YES];
+ // Refresh access token if it is expired
+ if ([expirationDate compare:[NSDate date]] == NSOrderedAscending) {
+ NSURL * OAuthTokenURL = [NSURL URLWithString:@"https://api.gowalla.com/api/oauth/token"];
+ EGOHTTPFormRequest * request = [[[EGOHTTPFormRequest alloc] initWithURL:OAuthTokenURL delegate:self] autorelease];
+ [request setPostValue:@"refresh_token"
+ forKey:@"grant_type"];
+ [request setPostValue:kGowallaAPIKey
+ forKey:@"client_id"];
+ [request setPostValue:kGowallaAPISecret
+ forKey:@"client_secret"];
+ [request setPostValue:[[NSUserDefaults standardUserDefaults] objectForKey:kGowallaBasicOAuthRefreshTokenPreferenceKey]
+ forKey:@"refresh_token"];
+ [request setPostValue:kGowallaRedirectURI
+ forKey:@"redirect_uri"];
+
+ [request setDidFinishSelector:@selector(applicationDidAuthorizeWithRequest:)];
+ [request startSynchronous];
+ } else {
+ [authenticationViewController dismissModalViewControllerAnimated:YES];
+ }
}
#pragma mark -
@@ -54,7 +54,7 @@ + (EGOHTTPRequest *)requestForPath:(NSString *)path
[request addRequestHeader:@"X-Gowalla-API-Key"
value:kGowallaAPIKey];
- return [request autorelease];
+ return request;
}
@end
@@ -7,12 +7,11 @@
//
#import <UIKit/UIKit.h>
+#import "EGOImageView.h"
-@class EGOImageView;
-
-@interface AFImageLoadingCell : UITableViewCell {
+@interface AFImageLoadingCell : UITableViewCell <EGOImageViewDelegate> {
@private
- EGOImageView* imageView;
+ EGOImageView * imageView;
}
- (void)setImageURL:(NSURL *)url;
@@ -16,12 +16,20 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];
imageView.frame = CGRectMake(5.0f, 5.0f, 50.0f, 50.0f);
+ imageView.delegate = self;
[self.contentView addSubview:imageView];
}
return self;
}
+- (void)dealloc {
+ imageView.delegate = nil;
+ [imageView cancelImageLoad];
+ [imageView release];
+ [super dealloc];
+}
+
- (void)setImageURL:(NSURL *)url {
imageView.imageURL = url;
}
@@ -41,9 +49,11 @@ - (void)layoutSubviews {
self.detailTextLabel.frame = CGRectOffset(self.detailTextLabel.frame, 60.0f, 0.0f);
}
-- (void)dealloc {
- [imageView release];
- [super dealloc];
+#pragma mark -
+#pragma mark EGOImageViewDelegate
+
+- (void)imageViewLoadedImage:(EGOImageView*)someImageView {
+ [imageView setNeedsDisplay];
}
@end
@@ -43,6 +43,7 @@
F81D48A711DAFEEE0052A291 /* User.m in Sources */ = {isa = PBXBuildFile; fileRef = F81D48A611DAFEEE0052A291 /* User.m */; };
F81D48AA11DAFEF70052A291 /* CheckIn.m in Sources */ = {isa = PBXBuildFile; fileRef = F81D48A911DAFEF70052A291 /* CheckIn.m */; };
F81D48BE11DB02FB0052A291 /* ISO8601DateFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = F81D48BD11DB02FB0052A291 /* ISO8601DateFormatter.m */; };
+ F826BB41120B121700FA5439 /* URLParser.m in Sources */ = {isa = PBXBuildFile; fileRef = F826BB40120B121700FA5439 /* URLParser.m */; };
F84CB51111DBA498004E8367 /* NSData+Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = F84CB51011DBA498004E8367 /* NSData+Base64.m */; };
F84CB51B11DBA50D004E8367 /* CheckInSuccessViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F84CB51911DBA50D004E8367 /* CheckInSuccessViewController.m */; };
F84CB58E11DBC3AC004E8367 /* AuthenticationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F84CB58C11DBC3AC004E8367 /* AuthenticationViewController.m */; };
@@ -123,6 +124,8 @@
F81D48A911DAFEF70052A291 /* CheckIn.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CheckIn.m; sourceTree = "<group>"; };
F81D48BC11DB02FB0052A291 /* ISO8601DateFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ISO8601DateFormatter.h; sourceTree = "<group>"; };
F81D48BD11DB02FB0052A291 /* ISO8601DateFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ISO8601DateFormatter.m; sourceTree = "<group>"; };
+ F826BB3F120B121700FA5439 /* URLParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = URLParser.h; sourceTree = "<group>"; };
+ F826BB40120B121700FA5439 /* URLParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = URLParser.m; sourceTree = "<group>"; };
F84CB50F11DBA498004E8367 /* NSData+Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+Base64.h"; sourceTree = "<group>"; };
F84CB51011DBA498004E8367 /* NSData+Base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+Base64.m"; sourceTree = "<group>"; };
F84CB51811DBA50D004E8367 /* CheckInSuccessViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CheckInSuccessViewController.h; sourceTree = "<group>"; };
@@ -279,6 +282,8 @@
F81D46B711DACB580052A291 /* EGOHTTPRequest */,
F81D46BC11DACB580052A291 /* EGOImageLoader */,
F81D468E11DACA340052A291 /* TouchJSON */,
+ F826BB3F120B121700FA5439 /* URLParser.h */,
+ F826BB40120B121700FA5439 /* URLParser.m */,
);
path = Libraries;
sourceTree = "<group>";
@@ -509,6 +514,7 @@
F84CB58E11DBC3AC004E8367 /* AuthenticationViewController.m in Sources */,
F84CB65111DBD33E004E8367 /* GowallaAPI.m in Sources */,
F8946C4D11DCFEF500E14FCF /* PassportViewController.m in Sources */,
+ F826BB41120B121700FA5439 /* URLParser.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -0,0 +1,12 @@
+#import <Foundation/Foundation.h>
+
+@interface URLParser : NSObject {
+ NSArray *variables;
+}
+
+@property (nonatomic, retain) NSArray *variables;
+
+- (id)initWithURLString:(NSString *)url;
+- (NSString *)valueForVariable:(NSString *)varName;
+
+@end
Oops, something went wrong.

0 comments on commit 9bbc5d6

Please sign in to comment.