Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add twitter friends list

  • Loading branch information...
commit 9675bdc9aa8f607af4dc77eb3fbecda312139ac7 1 parent ce3e58b
@hiroeorz authored
View
4 Classes/TwitterClient.m
@@ -42,6 +42,7 @@ - (void)saveFriends {
NSFileManager *fileManager = [NSFileManager defaultManager];
TwitterFriendsGetter *getter = [[TwitterFriendsGetter alloc] init];
NSString *tmpFilePath = [getter tmpFilePath];
+ [getter release];
if ([fileManager fileExistsAtPath:tmpFilePath]) {
NSError *error;
@@ -61,6 +62,9 @@ - (void)saveFriendsWithCursor:(NSNumber *)cursorNumber {
OAMutableURLRequest *request = [self authenticatedRequest:baseUrl];
[request setHTTPMethod:@"GET"];
+ /* このインスタンスのリリースは自分でやらせています
+ ticket:didFinishWithData 内。
+ */
TwitterFriendsGetter *getter = [[TwitterFriendsGetter alloc] init];
OADataFetcher *fetcher = [[[OADataFetcher alloc] init] autorelease];
View
9 Classes/TwitterFriendsGetter.m
@@ -68,7 +68,7 @@ - (void)ticket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
NSArray *users = [result objectForKey:@"users"];
NSMutableArray *array = nil;
NSString *tmpFilePath = [self tmpFilePath];
- BOOL knownAreaFlag = NO;
+ BOOL knownAddressFlag = NO;
NSString *lastName = [self lastName];
if ([[NSFileManager defaultManager] fileExistsAtPath:tmpFilePath]) {
@@ -81,10 +81,10 @@ - (void)ticket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
NSString *screen_name = [user objectForKey:@"screen_name"];
if ([screen_name compare:lastName] == NSOrderedSame) {
- knownAreaFlag = YES;
+ knownAddressFlag = YES;
}
- if (!knownAreaFlag) {
+ if (!knownAddressFlag) {
[array addObject:screen_name];
}
}
@@ -92,7 +92,7 @@ - (void)ticket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
[array writeToFile:tmpFilePath atomically:YES];
[array release];
- if ([next_cursor integerValue] == 0 || knownAreaFlag) {
+ if ([next_cursor integerValue] == 0 || knownAddressFlag) {
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:tmpFilePath
toPath:[self filePath]
@@ -116,6 +116,7 @@ - (void)ticket:(OAServiceTicket *)ticket didFailWithError:(NSError *)error {
trycount--;
TwitterClient *client = [[[TwitterClient alloc] init] autorelease];
[client saveFriendsWithCursor:nextCursor];
+ [self autorelease];
}
}
View
5 Classes/TwitterFriendsListViewController.h
@@ -8,6 +8,11 @@
#import <UIKit/UIKit.h>
+
+#define kRecentFriendsFileName @"recent_friends.plist"
+#define kRecentFriendsGroupName @"recent"
+#define kRecentFriendMaxCount 10
+
@class SendTweetViewController;
@class NowPlayingFriendsAppDelegate;
View
82 Classes/TwitterFriendsListViewController.m
@@ -12,6 +12,14 @@
#import "TwitterFriendsListViewController.h"
+@interface TwitterFriendsListViewController (Local)
+
+- (void)addRecentFriendToFile:(NSString *)friendName;
+- (NSString *)recentFriendFilePath;
+- (NSMutableArray *)arrayOfRecentFriends;
+@end
+
+
@implementation TwitterFriendsListViewController
@dynamic appDelegate;
@@ -77,7 +85,7 @@ - (void)viewDidLoad {
}
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
-
+
for (NSString *screen_name in array) {
NSString *initialChar =
[[screen_name substringWithRange:NSMakeRange(0, 1)] lowercaseString];
@@ -101,7 +109,17 @@ - (void)viewDidLoad {
NSArray *keys =
[[dic allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
- self.initials = keys;
+ NSMutableArray *mutableKeys = [[NSMutableArray alloc] initWithArray:keys];
+
+ NSMutableArray *recentFriends = [self arrayOfRecentFriends];
+
+ if ([recentFriends count] > 0) {
+ [friends setObject:recentFriends forKey:kRecentFriendsGroupName];
+ [mutableKeys insertObject:kRecentFriendsGroupName atIndex:0];
+ }
+
+ self.initials = mutableKeys;
+ [mutableKeys release];
}
- (void)viewDidAppear:(BOOL)animated {
@@ -200,6 +218,7 @@ - (NSIndexPath *)tableView:(UITableView *)tableView
NSArray *namesArray = [friends objectForKey:key];
self.selectedName = [namesArray objectAtIndex:[indexPath row]];
+ [self addRecentFriendToFile:selectedName];
[self dismissModalViewControllerAnimated:YES];
return indexPath;
@@ -212,15 +231,6 @@ - (NSIndexPath *)tableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index {
- /*
- id initial = [areas objectAtIndex:index];
-
- if ([area class] == [UITableViewIndexSearch class]) {
- [table setContentOffset:CGPointZero animated:YES];
- return NSNotFound;
- }
- */
-
return index;
}
@@ -228,6 +238,56 @@ - (NSInteger)tableView:(UITableView *)tableView
#pragma mark -
#pragma Local Methods
+- (NSMutableArray *)arrayOfRecentFriends {
+
+ NSString *filePath = [self recentFriendFilePath];
+ NSMutableArray *array = nil;
+
+ if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
+ array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
+ } else {
+ array = [[NSMutableArray alloc] init];
+ }
+
+ return [array autorelease];
+}
+
+- (void)addRecentFriendToFile:(NSString *)friendName {
+
+ NSString *filePath = [self recentFriendFilePath];
+ NSMutableArray *array = [self arrayOfRecentFriends];
+ BOOL hasObject = NO;
+
+ for (NSString *name in array) {
+ if ([name compare:friendName] == NSOrderedSame) {
+ hasObject = YES;
+ }
+ }
+
+ if (hasObject == NO) {
+ [array insertObject:friendName atIndex:0];
+ }
+
+ if ([array count] > kRecentFriendMaxCount) {
+ [array removeLastObject];
+ }
+
+ NSLog(@"add: %@ to File: %@", array, filePath);
+ [array writeToFile:filePath atomically:YES];
+}
+
+- (NSString *)recentFriendFilePath {
+
+ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
+ NSUserDomainMask, YES);
+ NSString *documentsDirectory = [paths objectAtIndex:0];
+ NSString *filePath =
+ [documentsDirectory stringByAppendingPathComponent:kRecentFriendsFileName];
+
+ return filePath;
+}
+
+
- (NowPlayingFriendsAppDelegate *)appDelegate {
return [[UIApplication sharedApplication] delegate];
}
Please sign in to comment.
Something went wrong with that request. Please try again.