changed git call from https to git readonly
[atutor.git] / mods / atsocial_iphone_app / Classes / ContactsDataSource.m
1 //
2 //  ContactsDataSource.m
3 //  ATutor
4 //
5 //  Created by Quang Anh Do on 10/07/2010.
6 //  Copyright 2010 Quang Anh Do. All rights reserved.
7 //
8
9 #import "ContactsDataSource.h"
10 #import "CommonFunctions.h"
11 #import "Contact.h"
12 #import "ContactItemCell.h"
13
14
15 @implementation ContactsDataSource
16
17 - (id)init {
18         if (self = [super init]) {
19                 NSDictionary *contactList = [NSKeyedUnarchiver unarchiveObjectWithFile:[applicationDocumentsDirectory() stringByAppendingPathComponent:@"contacts.plist"]];
20                 
21                 NSMutableDictionary *nameIndexes = [NSMutableDictionary dictionary];
22                 
23                 for (Contact *contact in contactList) {
24                         // Setup contact item
25                         NSString *urlString = linkToContact(contact.identifier, contact.displayName);
26                         TTTableTextItem *contactItem = [TTTableTextItem itemWithText:contact.displayName URL:urlString];
27                         
28                         // Setup name indexes for section headers
29                         NSString *firstLetter = [contact.displayName substringToIndex:1];
30                         NSMutableArray *existingArray;
31                         if (existingArray = [nameIndexes valueForKey:firstLetter]) {
32                                 [existingArray addObject:contactItem];
33                         } else {
34                                 NSMutableArray *tempArray = [NSMutableArray array];
35                                 [nameIndexes setObject:tempArray forKey:firstLetter];
36                                 [tempArray addObject:contactItem];
37                         }                       
38                         
39                         [self.items addObject:contactItem];
40                 }
41                 
42                 // Final touches
43                 self.sections = [[[nameIndexes allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] mutableCopy];
44                 self.items = [NSMutableArray array];
45                 
46                 for (NSString *index in self.sections) {
47                         [self.items addObject:[nameIndexes objectForKey:index]];
48                 }
49                 
50                 TTTableSummaryItem *countItem = [TTTableSummaryItem itemWithText:[NSString stringWithFormat:@"%d Contacts", [contactList count]] URL:NULL];
51                 [[self.items lastObject] addObject:countItem];
52         }
53         
54         return self;
55 }
56
57 - (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView {
58         return [TTSectionedDataSource lettersForSectionsWithSearch:NO summary:YES];
59 }
60
61 - (Class)tableView:(UITableView*)tableView cellClassForObject:(id)object {
62         return [ContactItemCell class];
63 }
64
65 @end