Fix add contact and add address to (new) contact

This commit is contained in:
Yann Diorcet 2012-07-12 22:49:14 +02:00
parent c1367078a5
commit fa7362af55
11 changed files with 248 additions and 109 deletions

View file

@ -42,8 +42,8 @@
@property (nonatomic, assign) ABRecordID contactID;
- (void)newContact;
- (void)newContact:(NSString*)address;
- (void)removeContact;
- (void)addSipField:(NSString*)address;
- (void)loadData;
- (void)saveData;

View file

@ -277,6 +277,10 @@ static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef inf
}
- (void)addEntry:(UITableView*)tableview section:(NSInteger)section animated:(BOOL)animated {
[self addEntry:tableview section:section animated:animated value:@""];
}
- (void)addEntry:(UITableView*)tableview section:(NSInteger)section animated:(BOOL)animated value:(NSString *)value{
NSMutableArray *sectionArray = [dataCache objectAtIndex:section];
NSUInteger count = [sectionArray count];
if(section == 0) {
@ -307,7 +311,7 @@ static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef inf
lMap = ABMultiValueCreateMutable(kABDictionaryPropertyType);
}
CFStringRef keys[] = { kABPersonInstantMessageUsernameKey, kABPersonInstantMessageServiceKey};
CFTypeRef values[] = { @"", CONTACT_SIP_FIELD };
CFTypeRef values[] = { [value copy], CONTACT_SIP_FIELD };
CFDictionaryRef lDict = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, 2, NULL, NULL);
ABMultiValueAddValueAndLabel(lMap, lDict, (CFStringRef)[labelArray objectAtIndex:0], &identifier);
CFRelease(lDict);
@ -395,20 +399,8 @@ static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef inf
[self loadData];
}
- (void)newContact:(NSString*)address {
contact = ABPersonCreate();
ABMultiValueRef lMap = ABMultiValueCreateMutable(kABDictionaryPropertyType);
CFStringRef keys[] = { kABPersonInstantMessageUsernameKey, kABPersonInstantMessageServiceKey};
CFTypeRef values[] = { [address copy], CONTACT_SIP_FIELD };
CFDictionaryRef lDict = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, 2, NULL, NULL);
ABMultiValueAddValueAndLabel(lMap, lDict, (CFStringRef)[labelArray objectAtIndex:0], NULL);
CFRelease(lDict);
ABRecordSetValue(contact, kABPersonInstantMessageProperty, lMap, nil);
CFRelease(lMap);
self->contactID = kABRecordInvalidID;
[self loadData];
- (void)addSipField:(NSString*)address {
[self addEntry:[self tableView] section:1 animated:FALSE value:address];
}

View file

@ -39,5 +39,7 @@
- (void)newContact;
- (void)newContact:(NSString*)address;
- (void)editContact:(ABRecordRef)contact;
- (void)editContact:(ABRecordRef)contact address:(NSString*)address;
@end

View file

@ -51,12 +51,27 @@
}
- (void)newContact:(NSString*)address {
[tableController newContact:address];
[tableController newContact];
[tableController addSipField:address];
[tableController setEditing:TRUE animated:FALSE];
[[tableController tableView] reloadData];
[editButton setOn];
}
- (void)editContact:(ABRecordRef)acontact {
[self setContact:acontact];
[tableController setEditing:TRUE animated:FALSE];
[[tableController tableView] reloadData];
[editButton setOn];
}
- (void)editContact:(ABRecordRef)acontact address:(NSString*)address {
[self setContact:acontact];
[tableController addSipField:address];
[tableController setEditing:TRUE animated:FALSE];
[[tableController tableView] reloadData];
[editButton setOn];
}
#pragma mark - Property Functions
@ -89,13 +104,37 @@
forControlEvents:UIControlEventTouchUpInside];
}
- (void)viewWillAppear:(BOOL)animated {
if([tableController isEditing]) {
[tableController resetData];
[tableController setEditing:FALSE animated:FALSE];
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {
[tableController viewWillDisappear:NO];
}
if([tableController isEditing]) {
[tableController setEditing:FALSE animated:FALSE];
[tableController resetData];
[editButton setOff];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[editButton setOff];
if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {
[tableController viewWillAppear:NO];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {
[tableController viewDidAppear:NO];
}
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {
[tableController viewDidDisappear:NO];
}
}
#pragma mark - UICompositeViewDelegate Functions

View file

@ -27,8 +27,10 @@
ABAddressBookRef addressBook;
BOOL sipFilter;
NSString *tempAddress;
}
@property (nonatomic, retain) NSString* tempAddress;
@property (nonatomic, assign) BOOL sipFilter;
@end

View file

@ -25,6 +25,7 @@
@implementation ContactsTableViewController
@synthesize sipFilter;
@synthesize tempAddress;
#pragma mark - Lifecycle Functions
@ -56,6 +57,7 @@
ABAddressBookUnregisterExternalChangeCallback(addressBook, sync_address_book, self);
CFRelease(addressBook);
[addressBookMap removeAllObjects];
[tempAddress release];
[super dealloc];
}
@ -70,27 +72,6 @@
#pragma mark -
static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef info, void *context) {
ContactsTableViewController* controller = (ContactsTableViewController*)context;
ABAddressBookRevert(addressBook);
[(UITableView *)controller.view reloadData];
}
#pragma mark - ViewController Functions
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self reloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
#pragma mark - UITableViewDataSource Functions
- (void)reloadData {
NSLog(@"Load contact list");
@synchronized (addressBookMap) {
@ -157,73 +138,69 @@ static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef inf
[self.tableView reloadData];
}
static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef info, void *context) {
ContactsTableViewController* controller = (ContactsTableViewController*)context;
ABAddressBookRevert(addressBook);
[controller reloadData];
}
#pragma mark - ViewController Functions
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self setTempAddress:nil];
}
#pragma mark - UITableViewDataSource Functions
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [addressBookMap count] + 1;
return [addressBookMap count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0) {
return 1;
} else {
return [(OrderedDictionary *)[addressBookMap objectForKey: [addressBookMap keyAtIndex: section - 1]] count];
}
return [(OrderedDictionary *)[addressBookMap objectForKey: [addressBookMap keyAtIndex: section]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellId = @"UIContactCell";
static NSString *kNewContactCellId = @"NewContactCell";
if([indexPath section] != 0) {
UIContactCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
if (cell == nil) {
cell = [[[UIContactCell alloc] initWithIdentifier:kCellId] autorelease];
}
OrderedDictionary *subDic = [addressBookMap objectForKey: [addressBookMap keyAtIndex: [indexPath section] - 1]];
NSString *key = [[subDic allKeys] objectAtIndex:[indexPath row]];
ABRecordRef contact = [subDic objectForKey:key];
[cell setContact: contact];
return cell;
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewContactCellId];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kNewContactCellId] autorelease];
//TODO
/*
[cell setSelectedBackgroundView:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list_highlight.png"]]autorelease]];*/
}
[cell.textLabel setText:@"Add new contact"];
return cell;
static NSString *kCellId = @"UIContactCell";
UIContactCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
if (cell == nil) {
cell = [[[UIContactCell alloc] initWithIdentifier:kCellId] autorelease];
}
OrderedDictionary *subDic = [addressBookMap objectForKey: [addressBookMap keyAtIndex: [indexPath section]]];
NSString *key = [[subDic allKeys] objectAtIndex:[indexPath row]];
ABRecordRef contact = [subDic objectForKey:key];
[cell setContact: contact];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"";
} else {
return [addressBookMap keyAtIndex: section - 1];
}
return [addressBookMap keyAtIndex: section];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath section] == 0) {
// Go to Contact details view
NSDictionary *dict = [[[NSDictionary alloc] initWithObjectsAndKeys:
[[[NSArray alloc] initWithObjects: nil] autorelease]
, @"newContact",
nil] autorelease];
[[PhoneMainView instance] changeView:PhoneView_ContactDetails dict:dict push:TRUE];
OrderedDictionary *subDic = [addressBookMap objectForKey: [addressBookMap keyAtIndex: [indexPath section]]];
ABRecordRef lPerson = [subDic objectForKey: [subDic keyAtIndex:[indexPath row]]];
// Go to Contact details view
NSDictionary * dict;
if(tempAddress == nil) {
dict = [[[NSDictionary alloc] initWithObjectsAndKeys:
[[[NSArray alloc] initWithObjects: lPerson, nil] autorelease]
, @"setContact:",
nil] autorelease];
} else {
OrderedDictionary *subDic = [addressBookMap objectForKey: [addressBookMap keyAtIndex: [indexPath section] - 1]];
ABRecordRef lPerson = [subDic objectForKey: [subDic keyAtIndex:[indexPath row]]];
// Go to Contact details view
NSDictionary *dict = [[[NSDictionary alloc] initWithObjectsAndKeys:
[[[NSArray alloc] initWithObjects: lPerson, nil] autorelease]
, @"setContact:",
nil] autorelease];
[[PhoneMainView instance] changeView:PhoneView_ContactDetails dict:dict push:TRUE];
dict = [[[NSDictionary alloc] initWithObjectsAndKeys:
[[[NSArray alloc] initWithObjects: lPerson, tempAddress, nil] autorelease]
, @"editContact:address:",
nil] autorelease];
[self setTempAddress:nil];
}
[[PhoneMainView instance] changeView:PhoneView_ContactDetails dict:dict push:TRUE];
}
@end

View file

@ -36,7 +36,10 @@
@property (nonatomic, retain) IBOutlet UIButton* allButton;
@property (nonatomic, retain) IBOutlet UIButton* linphoneButton;
- (IBAction)onAllClick:(id) event;
- (IBAction)onLinphoneClick:(id) event;
- (IBAction)onAllClick:(id)event;
- (IBAction)onLinphoneClick:(id)event;
- (IBAction)onAddContactClick:(id)event;
- (void)setAddress:(NSString*)address;
@end

View file

@ -18,6 +18,7 @@
*/
#import "ContactsViewController.h"
#import "PhoneMainView.h"
#import "AddressBook/ABPerson.h"
@ -69,9 +70,34 @@ typedef enum _HistoryView {
#pragma mark - ViewController Functions
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {
[tableController viewWillDisappear:NO];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self changeView:History_All];
if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {
[tableController viewWillAppear:NO];
}
[self changeView:History_All];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {
[tableController viewDidAppear:NO];
}
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {
[tableController viewDidDisappear:NO];
}
}
- (void)viewDidLoad {
@ -90,6 +116,10 @@ typedef enum _HistoryView {
#pragma mark -
- (void)setAddress:(NSString*)address {
[tableController setTempAddress:address];
}
- (void)changeView: (HistoryView) view {
if(view == History_All) {
@ -110,12 +140,30 @@ typedef enum _HistoryView {
#pragma mark - Action Functions
- (IBAction)onAllClick: (id) event {
- (IBAction)onAllClick:(id)event {
[self changeView: History_All];
}
- (IBAction)onLinphoneClick: (id) event {
- (IBAction)onLinphoneClick:(id)event {
[self changeView: History_Linphone];
}
- (IBAction)onAddContactClick:(id)event {
// Go to Contact details view
NSDictionary * dict;
if([tableController tempAddress] == nil) {
dict = [[[NSDictionary alloc] initWithObjectsAndKeys:
[[[NSArray alloc] initWithObjects: nil] autorelease]
, @"newContact",
nil] autorelease];
} else {
dict = [[[NSDictionary alloc] initWithObjectsAndKeys:
[[[NSArray alloc] initWithObjects: [tableController tempAddress], nil] autorelease]
, @"newContact:",
nil] autorelease];
[tableController setTempAddress:nil];
}
[[PhoneMainView instance] changeView:PhoneView_ContactDetails dict:dict push:TRUE];
}
@end

View file

@ -2,10 +2,10 @@
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="8.00">
<data>
<int key="IBDocument.SystemTarget">1296</int>
<string key="IBDocument.SystemVersion">11E53</string>
<string key="IBDocument.SystemVersion">11D50</string>
<string key="IBDocument.InterfaceBuilderVersion">2182</string>
<string key="IBDocument.AppKitVersion">1138.47</string>
<string key="IBDocument.HIToolboxVersion">569.00</string>
<string key="IBDocument.AppKitVersion">1138.32</string>
<string key="IBDocument.HIToolboxVersion">568.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="NS.object.0">1181</string>
@ -46,6 +46,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrameSize">{106, 58}</string>
<reference key="NSSuperview" ref="95706395"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="596330568"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="IBUIOpaque">NO</bool>
@ -84,6 +85,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{106, 0}, {106, 58}}</string>
<reference key="NSSuperview" ref="95706395"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="600417980"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="IBUIOpaque">NO</bool>
@ -112,6 +114,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{212, 0}, {108, 58}}</string>
<reference key="NSSuperview" ref="95706395"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="562388802"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="IBUIOpaque">NO</bool>
@ -137,6 +140,7 @@
</array>
<string key="NSFrameSize">{320, 58}</string>
<reference key="NSSuperview" ref="812520855"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="257572356"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<object class="NSColor" key="IBUIBackgroundColor">
@ -153,6 +157,8 @@
<int key="NSvFlags">274</int>
<string key="NSFrame">{{0, 58}, {320, 402}}</string>
<reference key="NSSuperview" ref="812520855"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView"/>
<string key="NSReuseIdentifierKey">_NS:10</string>
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">1</int>
@ -179,6 +185,7 @@
</array>
<string key="NSFrameSize">{320, 460}</string>
<reference key="NSSuperview"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="95706395"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<object class="NSColor" key="IBUIBackgroundColor">
@ -259,6 +266,15 @@
</object>
<int key="connectionID">47</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
<string key="label">onAddContactClick:</string>
<reference key="source" ref="600417980"/>
<reference key="destination" ref="841351856"/>
<int key="IBEventType">7</int>
</object>
<int key="connectionID">86</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">dataSource</string>
@ -321,7 +337,7 @@
<reference ref="600417980"/>
</array>
<reference key="parent" ref="812520855"/>
<string key="objectName">header</string>
<string key="objectName">toolBar</string>
</object>
<object class="IBObjectRecord">
<int key="objectID">4</int>
@ -377,9 +393,71 @@
<nil key="activeLocalization"/>
<dictionary class="NSMutableDictionary" key="localizations"/>
<nil key="sourceID"/>
<int key="maxID">84</int>
<int key="maxID">86</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
<object class="IBPartialClassDescription">
<string key="className">ContactsTableViewController</string>
<string key="superclassName">UITableViewController</string>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/ContactsTableViewController.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
<string key="className">ContactsViewController</string>
<string key="superclassName">UIViewController</string>
<dictionary class="NSMutableDictionary" key="actions">
<string key="onAddContactClick:">id</string>
<string key="onAllClick:">id</string>
<string key="onLinphoneClick:">id</string>
</dictionary>
<dictionary class="NSMutableDictionary" key="actionInfosByName">
<object class="IBActionInfo" key="onAddContactClick:">
<string key="name">onAddContactClick:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo" key="onAllClick:">
<string key="name">onAllClick:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo" key="onLinphoneClick:">
<string key="name">onLinphoneClick:</string>
<string key="candidateClassName">id</string>
</object>
</dictionary>
<dictionary class="NSMutableDictionary" key="outlets">
<string key="allButton">UIButton</string>
<string key="linphoneButton">UIButton</string>
<string key="tableController">ContactsTableViewController</string>
<string key="tableView">UITableView</string>
</dictionary>
<dictionary class="NSMutableDictionary" key="toOneOutletInfosByName">
<object class="IBToOneOutletInfo" key="allButton">
<string key="name">allButton</string>
<string key="candidateClassName">UIButton</string>
</object>
<object class="IBToOneOutletInfo" key="linphoneButton">
<string key="name">linphoneButton</string>
<string key="candidateClassName">UIButton</string>
</object>
<object class="IBToOneOutletInfo" key="tableController">
<string key="name">tableController</string>
<string key="candidateClassName">ContactsTableViewController</string>
</object>
<object class="IBToOneOutletInfo" key="tableView">
<string key="name">tableView</string>
<string key="candidateClassName">UITableView</string>
</object>
</dictionary>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/ContactsViewController.h</string>
</object>
</object>
</array>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes"/>
<int key="IBDocument.localizationMode">0</int>
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">

View file

@ -217,9 +217,9 @@
// Go to Contact details view
NSDictionary *dict = [[[NSDictionary alloc] initWithObjectsAndKeys:
[[[NSArray alloc] initWithObjects:[addressField text], nil] autorelease]
, @"newContact:",
, @"setAddress:",
nil] autorelease];
[[PhoneMainView instance] changeView:PhoneView_ContactDetails dict:dict push:TRUE];
[[PhoneMainView instance] changeView:PhoneView_Contacts dict:dict push:TRUE];
}
- (IBAction)onBackClick: (id) event {

View file

@ -73,8 +73,6 @@
[self.view addSubview: navigationController.view];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {