Permalink
Browse files

Show the auth view on startup when the stored credential are invalid.

  • Loading branch information...
1 parent 67ad0a5 commit 0730cb22b67a5511cea8aca3069d2d253b6cb06b @alloy alloy committed Oct 4, 2011
@@ -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;
}
@@ -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;
@@ -42,6 +42,7 @@ + (void)authenticate:(NSString *)username password:(NSString *)password {
}
+@synthesize delegate;
@synthesize tableView;
@synthesize usernameField, passwordField;
@synthesize usernameCell, passwordCell;
@@ -120,6 +121,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
- (IBAction)dismissDialog:(id)sender {
+ [self.delegate authenticationViewWillDismiss:self];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
@@ -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;
@@ -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;
@@ -3,7 +3,6 @@
#import "RootViewController.h"
#import "CalendarViewController.h"
-#import "AuthenticationViewController.h"
// ONLY FOR DEBUGGING PURPOSES!
#import "Authentication.h"
@@ -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;
}
@@ -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
@@ -192,6 +192,7 @@ - (void)yieldDownloadedData {
@implementation JSONDownload
- (void)yieldDownloadedData {
+ //NSLog(@"Downloaded:\n%@", [[NSString alloc] initWithData:downloadData encoding:NSUTF8StringEncoding]);
block([downloadData yajl_JSON]);
}
View
@@ -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 {
@@ -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) {
View
@@ -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.
Oops, something went wrong.

0 comments on commit 0730cb2

Please sign in to comment.