Skip to content

Commit

Permalink
Show the auth view on startup when the stored credential are invalid.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Oct 4, 2011
1 parent 67ad0a5 commit 0730cb2
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 62 deletions.
16 changes: 7 additions & 9 deletions Classes/Controllers/AuthenticationViewController.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#import <UIKit/UIKit.h>

@class AuthenticationViewController;

@protocol AuthenticationViewControllerDelegate
- (void)authenticationViewWillDismiss:(AuthenticationViewController *)controller;
@end

@interface AuthenticationViewController : UIViewController {
IBOutlet UITableView *tableView;
IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;
IBOutlet UILabel *signedInAsLabel;
IBOutlet UITableViewCell *usernameCell;
IBOutlet UITableViewCell *passwordCell;
IBOutlet UITableViewCell *signingInCell, *signedInCell;
IBOutlet UIBarButtonItem *doneButton;
IBOutlet UIButton *helpBannerButton;
BOOL signingIn, signedIn;
}

Expand All @@ -24,6 +20,8 @@
@property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton;
@property (nonatomic, retain) IBOutlet UIButton *helpBannerButton;

@property (nonatomic, assign) id <AuthenticationViewControllerDelegate> delegate;

+ (BOOL)signIn;
+ (NSString *)signedInAs;
+ (void)retrieveUsername:(NSString **)username password:(NSString **)password;
Expand Down
8 changes: 4 additions & 4 deletions Classes/Controllers/AuthenticationViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ + (void)authenticate:(NSString *)username password:(NSString *)password {
}


@synthesize delegate;
@synthesize tableView;
@synthesize usernameField, passwordField;
@synthesize usernameCell, passwordCell;
Expand Down Expand Up @@ -120,6 +121,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N


- (IBAction)dismissDialog:(id)sender {
[self.delegate authenticationViewWillDismiss:self];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}

Expand All @@ -144,11 +146,9 @@ - (IBAction)saveCredentials:(id)sender {
[[Trakt sharedInstance] verifyCredentials:^(BOOL valid) {
if (valid) {
signingIn = NO;
signedIn = YES;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
withRowAnimation:UITableViewRowAnimationRight];
//[self dismissDialog:self];
[self dismissDialog:self];
} else {
NSLog(@"FAIL!");
self.doneButton.enabled = YES;
self.usernameField.enabled = YES;
self.passwordField.enabled = YES;
Expand Down
6 changes: 3 additions & 3 deletions Classes/Controllers/iTraktAppDelegate.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import <UIKit/UIKit.h>
#import "HTTPDownload.h"
#import "AuthenticationViewController.h"

@interface iTraktAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UITabBarController* tabBarController;
@interface iTraktAppDelegate : NSObject <UIApplicationDelegate, AuthenticationViewControllerDelegate> {
BOOL refreshDataWhenAuthViewDismisses;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
Expand Down
32 changes: 25 additions & 7 deletions Classes/Controllers/iTraktAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#import "RootViewController.h"
#import "CalendarViewController.h"
#import "AuthenticationViewController.h"

// ONLY FOR DEBUGGING PURPOSES!
#import "Authentication.h"
Expand All @@ -22,21 +21,32 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
//NSLog(@"[!] Clearing cache");
//[[EGOCache currentCache] clearCache];

refreshDataWhenAuthViewDismisses = NO;

[[Trakt sharedInstance] setApiKey:API_KEY];

if ([AuthenticationViewController signIn]) {
NSLog(@"[!] Signed in.");
[[Trakt sharedInstance] verifyCredentials:^(BOOL valid) {
if (valid) {
NSLog(@"Credentials are valid");
// Give the controller a chance to initialize
[self performSelector:@selector(refreshDataStartingAtCurrentSelectedTopLevelController) withObject:nil afterDelay:0];
} else {
NSLog(@"Credentials are invalid");
refreshDataWhenAuthViewDismisses = YES;
[self presentAuthenticationDialog:nil];
}
}];
} else {
NSLog(@"[!] Not signed in.");
[self presentAuthenticationDialog:nil];
}

[[Trakt sharedInstance] setApiKey:API_KEY];

[HTTPDownload setGlobalDelegate:self];

[self.window makeKeyAndVisible];

// Give the controller a chance to initialize
[self performSelector:@selector(refreshDataStartingAtCurrentSelectedTopLevelController) withObject:nil afterDelay:0];

return YES;
}

Expand Down Expand Up @@ -82,11 +92,19 @@ - (void)refreshDataStartingAtCurrentSelectedTopLevelController {


- (IBAction)presentAuthenticationDialog:(id)sender {
AuthenticationViewController *controller = [[AuthenticationViewController alloc] initWithNibName:@"AuthenticationViewController" bundle:nil];
AuthenticationViewController *controller;
controller = [[AuthenticationViewController alloc] initWithNibName:@"AuthenticationViewController"
bundle:nil];
controller.delegate = self;
[self.window.rootViewController presentModalViewController:controller animated:YES];
[controller release];
}

- (void)authenticationViewWillDismiss:(AuthenticationViewController *)controller {
refreshDataWhenAuthViewDismisses = NO;
[self refreshDataStartingAtCurrentSelectedTopLevelController];
}


#pragma mark -
#pragma mark Memory management
Expand Down
1 change: 1 addition & 0 deletions Classes/Lib/HTTPDownload.m
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ - (void)yieldDownloadedData {
@implementation JSONDownload

- (void)yieldDownloadedData {
//NSLog(@"Downloaded:\n%@", [[NSString alloc] initWithData:downloadData encoding:NSUTF8StringEncoding]);
block([downloadData yajl_JSON]);
}

Expand Down
11 changes: 9 additions & 2 deletions Classes/Models/Episode.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ - (NSString *)title {
}

- (NSURL *)thumbURL {
return [NSURL URLWithString:[dictionary valueForKey:@"thumb"]];
if ([NSNull null] != [dictionary objectForKey:@"thumb"]) {
return [dictionary valueForKey:@"thumb"];
}
return nil;
}

- (UIImage *)thumb {
return thumb == nil ? [UIImage imageNamed:@"placeholder-landscape"] : thumb;
}

- (NSInteger)season {
Expand Down Expand Up @@ -89,7 +96,7 @@ - (void)toggleSeen:(void (^)())requestCompletedBlock {

- (void)ensureThumbIsLoaded:(void (^)())downloadedBlock {
// important to first check if we already have the thumb loaded for performance!
if (self.thumb == nil) {
if (self.thumb == nil && self.thumbURL != nil) {
[[Trakt sharedInstance] showThumbForURL:self.thumbURL block:^(UIImage *theThumb, BOOL cached) {
self.thumb = theThumb;
if (!cached) {
Expand Down
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ General

iOS app

* Use CocoaPods for the dependencies.
* Possibly switch to AFNetworking to replace the http request and json/image code.
* Handle connection errors in some way which is needed to provide a good UX. I.e. should we retry, take a failure block, etc.
* Echofon style network failure notification
* Make HTTPDownload post start/stop notifications so that the refresh/stop buttons in the top-level controllers can show them accurately.
Expand Down
Loading

0 comments on commit 0730cb2

Please sign in to comment.