Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
isa = PBXGroup;
children = (
763CE32F1677F555006B7525 /* IDTwitterAccountChooserViewController.h */,
763CE3301677F555006B7525 /* IDTwitterAccountChooserViewController.m */,
763CE3191677F469006B7525 /* AppDelegate.h */,
763CE3301677F555006B7525 /* IDTwitterAccountChooserViewController.m */,
763CE31A1677F469006B7525 /* AppDelegate.m */,
763CE32B1677F4A7006B7525 /* RootViewController.h */,
763CE32C1677F4A7006B7525 /* RootViewController.m */,
Expand Down Expand Up @@ -259,6 +259,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch";
INFOPLIST_FILE = "Demo/Demo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -270,6 +271,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch";
INFOPLIST_FILE = "Demo/Demo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand Down
140 changes: 70 additions & 70 deletions IDTwitterAccountChooserViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,75 +82,71 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfa


- (void)loadMetadataForCellsWithIndexPaths:(NSArray *)indexPaths {
for(NSIndexPath *indexPath in indexPaths) {
ACAccount *account = [self.twitterAccounts objectAtIndex:indexPath.row];
NSString *username = [account username];
if(username == nil || ([imagesDictionary objectForKey:username] != nil && [realNamesDictionary objectForKey:username] != nil))
NSMutableArray *usernameArray = [[NSMutableArray alloc] init];
ACAccount *account;

for(NSIndexPath *indexPath in indexPaths) {
ACAccount *tmpAccount = [self.twitterAccounts objectAtIndex:indexPath.row];
NSString *username = [tmpAccount username];
if(username == nil || ([imagesDictionary objectForKey:username] != nil && [realNamesDictionary objectForKey:username] != nil))
continue;
NSURL *url = [NSURL URLWithString:[kTwitterAPIRootURL stringByAppendingString:@"users/show.json"]];
NSDictionary *parameters = @{
@"screen_name": username
};
TWRequest *request = [[TWRequest alloc] initWithURL:url
parameters:parameters
requestMethod:TWRequestMethodGET];
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if(error != nil && responseData == nil) {
NSLog(@"TWRequest error: %@", [error localizedDescription]);
return;
}
error = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if(error != nil) {
NSLog(@"JSON deserialization error: %@", [error localizedDescription]);
return;
}
NSString *name = [responseDictionary objectForKey:@"name"];
if([realNamesDictionary objectForKey:username] == nil && name != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[realNamesDictionary setObject:name forKey:username];
[self.tableView reloadData];
});
}
NSString *profileImageURLString = [responseDictionary objectForKey:@"profile_image_url"];
if([imagesDictionary objectForKey:username] == nil && profileImageURLString != nil) {
NSString *filename = [[profileImageURLString componentsSeparatedByString:@"/"] lastObject];
NSString *extension = [filename pathExtension];
NSString *basename = [filename stringByDeletingPathExtension];
NSString *biggerFilename = [[basename stringByReplacingOccurrencesOfString:@"normal" withString:@"bigger" options:(NSAnchoredSearch | NSBackwardsSearch) range:NSMakeRange(0, [basename length])] stringByAppendingPathExtension:extension];
NSString *biggerFilenameURLString = [profileImageURLString stringByReplacingOccurrencesOfString:filename
withString:biggerFilename
options:(NSAnchoredSearch | NSBackwardsSearch)
range:NSMakeRange(0, [profileImageURLString length])];
NSURL *imageURL = [NSURL URLWithString:biggerFilenameURLString];
TWRequest *imageRequest = [[TWRequest alloc] initWithURL:imageURL
parameters:nil
requestMethod:TWRequestMethodGET];
[imageRequest setAccount:account];
[imageRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if(error != nil && responseData == nil) {
NSLog(@"TWRequest error: %@", [error localizedDescription]);
return;
}
UIImage *biggerImage = [UIImage imageWithData:responseData];
UIImage *image = [UIImage imageWithCGImage:[biggerImage CGImage]
scale:2.0
orientation:UIImageOrientationUp];
if([imagesDictionary objectForKey:username] == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[imagesDictionary setObject:image forKey:username];
[self.tableView reloadData];
});
}
}];

}
}];

[usernameArray addObject:username];
account = tmpAccount;
}

if([usernameArray count]) {
NSString *usernameJoined = [usernameArray componentsJoinedByString:@","];
NSURL *url = [NSURL URLWithString:[kTwitterAPIRootURL stringByAppendingString:@"users/lookup.json"]];

NSDictionary *parameters = @{
@"screen_name": usernameJoined
};

TWRequest *request = [[TWRequest alloc] initWithURL:url
parameters:parameters
requestMethod:TWRequestMethodGET];

[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if(error != nil && responseData == nil) {
NSLog(@"TWRequest error: %@", [error localizedDescription]);
return;
}
error = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if(error != nil) {
NSLog(@"JSON deserialization error: %@", [error localizedDescription]);
return;
}
for (NSDictionary *user in responseDictionary) {
NSString *name = [user objectForKey:@"name"];
NSString *username = [[user objectForKey:@"screen_name"] lowercaseString];
if([realNamesDictionary objectForKey:username] == nil && name != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[realNamesDictionary setObject:name forKey:username];
[self.tableView reloadData];
});
}

NSString *profileImageURLString = [user objectForKey:@"profile_image_url"];
if([imagesDictionary objectForKey:username] == nil && profileImageURLString != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString: profileImageURLString]]];
[imagesDictionary setObject:myImage forKey:username];
[self.tableView reloadData];
});
}
}
}];
}
}




#pragma mark - Button Actions


Expand Down Expand Up @@ -199,19 +195,23 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell.accessoryType = UITableViewCellAccessoryNone;
}
ACAccount *account = [self.twitterAccounts objectAtIndex:indexPath.row];
// NSDictionary *properties = [account valueForKey:@"properties"];
// if(properties != nil) {
// NSLog(@"properties: %@", properties);
// }
// NSDictionary *properties = [account valueForKey:@"properties"];
// if(properties != nil) {
// NSLog(@"properties: %@", properties);
// }

NSString *username = [account username];
cell.textLabel.text = [account accountDescription];

NSString *realName = [realNamesDictionary objectForKey:username];
cell.detailTextLabel.text = realName;

UIImage *image = [imagesDictionary objectForKey:username];
[cell.imageView setImage:image];

CGFloat widthScale = 34 / image.size.width;
CGFloat heightScale = 34 / image.size.height;
cell.imageView.transform = CGAffineTransformMakeScale(widthScale, heightScale);

return cell;
}

Expand Down