5 // Created by Quang Anh Do on 07/06/2010.
6 // Copyright 2010 Quang Anh Do. All rights reserved.
9 #import "ATutorHelper.h"
10 #import "ATutorAppDelegate.h"
11 #import "OARequestParameter.h"
12 #import "CommonFunctions.h"
13 #import "OAServiceTicket.h"
14 #import "NSDictionary_JSONExtensions.h"
17 @interface ATutorHelper (Private)
19 - (void)peopleCallback:(OAServiceTicket *)ticket didFinishWithResponse:(id)response;
20 - (void)personCallback:(OAServiceTicket *)ticket didFinishWithResponse:(id)response;
21 - (NSDictionary *)matchDisplayNameWithId:(NSArray *)data;
26 @implementation ATutorHelper
29 @synthesize numberOfContacts;
31 @synthesize contactMapping;
37 [contactMapping dealloc];
44 if (self = [super init]) {
45 self.consumer = [(ATutorAppDelegate *)[[UIApplication sharedApplication] delegate] consumer];
46 self.numberOfContacts = 0;
47 self.contacts = [[NSMutableArray alloc] init];
48 self.contactMapping = [[NSMutableArray alloc] init];
54 - (void)fetchContactList {
55 NSLog(@"=-=-=-=-=-=-=-=-Fetching contact list-=-=-=-=-=-=-=-=");
57 [consumer getDataForUrl:@"/people/@me/@contacts"
58 andParameters:[NSArray arrayWithObjects:[OARequestParameter requestParameterWithName:@"count" value:@"100"],
59 [OARequestParameter requestParameterWithName:@"startIndex" value:[NSString stringWithFormat:@"%d", numberOfContacts]],
60 [OARequestParameter requestParameterWithName:@"sortBy" value:@"displayName"],
63 didFinishSelector:@selector(peopleCallback:didFinishWithResponse:)];
66 - (void)fetchOwnProfile {
67 NSLog(@"=-=-=-=-=-=-=-=-Fetching own profile-=-=-=-=-=-=-=-=");
69 [consumer getDataForUrl:@"/people/@me/@self"
72 didFinishSelector:@selector(personCallback:didFinishWithResponse:)];
75 - (void)peopleCallback:(OAServiceTicket *)ticket didFinishWithResponse:(id)response {
76 if (ticket.didSucceed) {
78 NSDictionary *data = [NSDictionary dictionaryWithJSONData:[response dataUsingEncoding:NSUTF8StringEncoding] error:&error];
79 NSArray *entries = [data objectForKey:@"entry"];
82 [contactMapping addObjectsFromArray:entries];
83 numberOfContacts += [entries count];
85 for (NSDictionary *entry in entries) {
86 [contacts addObject:[Contact contactWithDictionary:entry]];
89 // Continue fetching or not?
90 if (numberOfContacts < [[data objectForKey:@"totalResults"] intValue]) { // Fetch friend
91 [self fetchContactList];
92 } else { // Fetch own profile
93 [self fetchOwnProfile];
96 alertMessage(@"Error", @"Unable to fetch your contact list");
100 - (void)personCallback:(OAServiceTicket *)ticket didFinishWithResponse:(id)response {
101 if (ticket.didSucceed) {
102 NSError *error = nil;
103 NSDictionary *data = [NSDictionary dictionaryWithJSONData:[response dataUsingEncoding:NSUTF8StringEncoding] error:&error];
104 NSDictionary *entry = [data objectForKey:@"entry"];
107 [contactMapping addObject:entry];
110 [contacts addObject:[Contact contactWithDictionary:entry]];
113 NSLog(@"Archiving contact list");
115 [NSKeyedArchiver archiveRootObject:[self matchDisplayNameWithId:contactMapping]
116 toFile:[applicationDocumentsDirectory() stringByAppendingPathComponent:@"contact_mapping.plist"]];
118 [NSKeyedArchiver archiveRootObject:contacts
119 toFile:[applicationDocumentsDirectory() stringByAppendingPathComponent:@"contacts.plist"]];
122 if (delegate && [delegate respondsToSelector:@selector(doneFetchingContactList)]) {
123 [delegate performSelector:@selector(doneFetchingContactList)];
126 alertMessage(@"Error", @"Unable to fetch your profile");
130 - (NSDictionary *)matchDisplayNameWithId:(NSArray *)data {
131 NSMutableDictionary *retVal = [[NSMutableDictionary alloc] init];
133 for (NSDictionary *contact in data) {
134 [retVal setObject:[contact objectForKey:@"displayName"]
135 forKey:[contact objectForKey:@"id"]];
138 return [retVal autorelease];