Skip to content

Commit

Permalink
Finished adding profile images to Accounts list in Preferences and in…
Browse files Browse the repository at this point in the history
… the Accounts popup menu.
  • Loading branch information
luciuskwok committed Aug 13, 2010
1 parent 3635fc3 commit 616d794
Show file tree
Hide file tree
Showing 13 changed files with 1,428 additions and 174 deletions.
3 changes: 3 additions & 0 deletions Classes-Mac/AddAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ - (void) didLogin:(TwitterLoginAction *)action {
// Set database connection.
[account setTwitter:twitter];

// Load profile images
[twitter loadAccountProfileImages];

// Tell delegate we're done
if ([delegate respondsToSelector:@selector(didLoginToAccount:)])
[delegate didLoginToAccount:account];
Expand Down
2 changes: 1 addition & 1 deletion Classes-Mac/MainWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ - (void)reloadUsersMenu {
}

// Profile image
[item setImage:account.profileImage];
[item setImage:[account profileImage16px]];

[usersPopUp.menu addItem:item];
}
Expand Down
4 changes: 2 additions & 2 deletions Classes-Mac/Preferences.xib
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSTableColumn" id="911828482">
<string key="NSIdentifier">status</string>
<double key="NSWidth">40</double>
<double key="NSMinWidth">40</double>
<double key="NSWidth">24</double>
<double key="NSMinWidth">24</double>
<double key="NSMaxWidth">1000</double>
<object class="NSTableHeaderCell" key="NSHeaderCell">
<int key="NSCellFlags">75628096</int>
Expand Down
3 changes: 1 addition & 2 deletions Classes-Mac/PreferencesController.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColu
if (account.xAuthToken == nil)
result = alertImage;
else
result = account.profileImage;
result = [account profileImage16px];
}

}
Expand All @@ -140,7 +140,6 @@ - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColu
#pragma mark Double click

- (void)openSelectedTableRow:(id)sender {
//- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
// Double clicks end up here.

int rowIndex = [tableView selectedRow];
Expand Down
3 changes: 3 additions & 0 deletions Classes-Shared/Twitter/Twitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
- (void)refreshNow;
- (void)scheduleRefreshTimer:(NSTimeInterval)interval;

// Profile images
- (void)loadAccountProfileImages;

// Actions
- (void)startTwitterAction:(TwitterAction*)action withAccount:(TwitterAccount *)account;

Expand Down
44 changes: 44 additions & 0 deletions Classes-Shared/Twitter/Twitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#import "Twitter.h"
#import "TwitterTimeline.h"
#import "TwitterUserInfoAction.h"



Expand Down Expand Up @@ -313,6 +314,46 @@ - (NSArray *)usersWithName:(NSString *)name {
return set;
}

#pragma mark Profile images

- (void)loadProfileImageURL:(NSString *)url withAccount:(TwitterAccount *)account {
if (url == nil)
return;
if (account.profileImageURL != nil) {
if ([url isEqualToString:account.profileImageURL])
return; // Don't reload the profile image if the url is the same.
}
[account loadProfileImageURL:url];
}

- (void)loadUserInfoForAccount:(TwitterAccount *)account {
TwitterUserInfoAction *action = [[[TwitterUserInfoAction alloc] initWithScreenName:account.screenName] autorelease];
action.completionAction = @selector(didLoadUserInfo:);
action.completionTarget = self;
[self startTwitterAction:action withAccount:account];
}

- (void)didLoadUserInfo:(TwitterUserInfoAction*)action {
// Update just the profile image.
NSString *url = action.userResult.profileImageURL;
TwitterAccount *account = [self accountWithScreenName:action.userResult.screenName];
[self loadProfileImageURL:url withAccount:account];
}

- (void)loadAccountProfileImages {
for (TwitterAccount *account in accounts) {
TwitterUser *user = [self userWithIdentifier:account.identifier];
if (user.profileImageURL == nil) {
// Load user info
[self loadUserInfoForAccount:account];
} else {
// Load profile image data
[self loadProfileImageURL:user.profileImageURL withAccount:account];
}
}
}


#pragma mark TwitterActions

- (void)startTwitterAction:(TwitterAction*)action withAccount:(TwitterAccount *)account {
Expand Down Expand Up @@ -347,6 +388,9 @@ - (void)fireRefreshTimer:(NSTimer *)timer {
[account reloadNewer];
}
refreshCount = 3;

// Also update account profile images
[self loadAccountProfileImages];
} else {
for (TwitterAccount *account in accounts) {
[account.homeTimeline reloadNewer];
Expand Down
15 changes: 12 additions & 3 deletions Classes-Shared/Twitter/TwitterAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@


#import <Foundation/Foundation.h>
#import "LKLoadURLAction.h"

@class TwitterStatusUpdate, TwitterTimeline, TwitterDirectMessageTimeline, Twitter;


@interface TwitterAccount : NSObject {
@interface TwitterAccount : NSObject <LKLoadURLActionDelegate> {
// Core account info. Saved in user defaults.
NSNumber *identifier;
NSString *screenName;
NSString *xAuthToken;
NSString *xAuthSecret;
id profileImage;
NSData *profileImageData;
NSString *profileImageURL;

// Cached data. References to sqlite db.
TwitterTimeline *homeTimeline;
Expand All @@ -42,7 +44,8 @@
@property (nonatomic, assign) NSString *password;
@property (nonatomic, retain) NSString *xAuthToken;
@property (nonatomic, retain) NSString *xAuthSecret;
@property (nonatomic, retain) id profileImage;
@property (nonatomic, retain) NSData *profileImageData;
@property (nonatomic, retain) NSString *profileImageURL;

@property (nonatomic, retain) TwitterTimeline *homeTimeline;
@property (nonatomic, retain) TwitterTimeline *mentions;
Expand All @@ -63,8 +66,14 @@

- (void)deleteCaches;

// Unread messages
- (BOOL)hasUnreadInHomeTimeline;
- (BOOL)hasUnreadInMentions;
- (BOOL)hasUnreadInDirectMessages;

// Profile images
- (void)loadProfileImageURL:(NSString *)url;
- (id)profileImage;
- (id)profileImage16px;

@end
61 changes: 56 additions & 5 deletions Classes-Shared/Twitter/TwitterAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
#import "TwitterStatusUpdate.h"
#import "TwitterLoadTimelineAction.h"
#import "TwitterList.h"
#import "HelTweeticaAppDelegate.h"

#ifndef TARGET_PROJECT_MAC
#import "SFHFKeychainUtils.h"
#endif


@implementation TwitterAccount
@synthesize identifier, screenName, xAuthToken, xAuthSecret, profileImage;
@synthesize identifier, screenName, xAuthToken, xAuthSecret, profileImageData, profileImageURL;
@synthesize homeTimeline, mentions, directMessages, favorites, lists, listSubscriptions, savedSearches;


Expand Down Expand Up @@ -66,7 +67,8 @@ - (id) initWithCoder: (NSCoder*) decoder {
self.screenName = [decoder decodeObjectForKey:@"screenName"];
self.xAuthToken = [decoder decodeObjectForKey:@"xAuthToken"];
self.xAuthSecret = [decoder decodeObjectForKey:@"xAuthSecret"];
self.profileImage = [decoder decodeObjectForKey:@"profileImage"];
self.profileImageData = [decoder decodeObjectForKey:@"profileImageData"];
self.profileImageURL = [decoder decodeObjectForKey:@"profileImageURL"];

self.homeTimeline.latestReadIdentifier = [decoder decodeObjectForKey:@"homeTimeline.latestReadIdentifier"];
self.mentions.latestReadIdentifier = [decoder decodeObjectForKey:@"mentions.latestReadIdentifier"];
Expand All @@ -80,7 +82,8 @@ - (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject: screenName forKey:@"screenName"];
[encoder encodeObject: xAuthToken forKey:@"xAuthToken"];
[encoder encodeObject: xAuthSecret forKey:@"xAuthSecret"];
[encoder encodeObject: profileImage forKey:@"profileImage"];
[encoder encodeObject: profileImageData forKey:@"profileImageData"];
[encoder encodeObject: profileImageURL forKey:@"profileImageURL"];

[encoder encodeObject: homeTimeline.latestReadIdentifier forKey:@"homeTimeline.latestReadIdentifier"];
[encoder encodeObject: mentions.latestReadIdentifier forKey:@"mentions.latestReadIdentifier"];
Expand All @@ -92,7 +95,8 @@ - (void) dealloc {
[screenName release];
[xAuthToken release];
[xAuthSecret release];
[profileImage release];
[profileImageData release];
[profileImageURL release];

[homeTimeline release];
[mentions release];
Expand Down Expand Up @@ -270,8 +274,55 @@ - (BOOL)hasUnreadInMentions {
}

- (BOOL)hasUnreadInDirectMessages {
if ([directMessages numberOfStatusUpdates] == 0) return NO;
if ([directMessages newestStatusIdentifier] == nil) return NO;
return [self messageIdentifier:directMessages.latestReadIdentifier isFirstObjectInTimeline:directMessages] == NO;
}

#pragma mark Profile image

- (void)loadProfileImageURL:(NSString *)url {
// Start an action to load the url.
LKLoadURLAction *action = [[[LKLoadURLAction alloc] init] autorelease];
action.delegate = self;
action.identifier = url;
[action loadURL:[NSURL URLWithString:url]];
[[NSApp delegate] incrementNetworkActionCount];
}

- (void)loadURLAction:(LKLoadURLAction*)action didLoadData:(NSData*)data {
if (data.length > 0) {
self.profileImageURL = action.identifier;
self.profileImageData = data;
}
[[NSApp delegate] decrementNetworkActionCount];
}

- (void)loadURLAction:(LKLoadURLAction*)action didFailWithError:(NSError*)error {
[[NSApp delegate] decrementNetworkActionCount];
}

- (id)profileImage {
if (profileImageData == nil)
return nil;
#ifdef TARGET_PROJECT_MAC
return [[[NSImage alloc] initWithData:profileImageData] autorelease];
#else
return [[[UIImage alloc] initWithData:profileImageData] autorelease];
#endif
}

- (id)profileImage16px {
if (profileImageData == nil)
return nil;
#ifdef TARGET_PROJECT_MAC
NSImage *image = [[[NSImage alloc] initWithData:profileImageData] autorelease];
[image setSize:NSMakeSize(16, 16)];
return image;
#else
return [[[UIImage alloc] initWithData:profileImageData] autorelease];
#endif

}


@end
12 changes: 12 additions & 0 deletions Classes-Shared/Twitter/TwitterDirectMessageTimeline.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ - (void)addMessages:(NSArray *)messages sent:(BOOL)sent {
}
}

- (NSNumber *)newestStatusIdentifier {
// This is used only by the unread messages indicator, so only consider received messages.
NSString *query = [NSString stringWithFormat:@"Select identifier from %@ where sent == 0 order by CreatedDate desc limit 1", databaseTableName];
LKSqliteStatement *statement = [twitter.database statementWithQuery:query];
NSNumber *n = nil;

if ([statement step] == SQLITE_ROW) { // Row has data.
n = [statement objectForColumnIndex:0];
}
return n;
}

#pragma mark Conversations

- (NSArray *)usersSortedByMostRecentDirectMessage {
Expand Down
Loading

0 comments on commit 616d794

Please sign in to comment.