From fa7362af5576c32ffa0cd979ad6baa9467524213 Mon Sep 17 00:00:00 2001 From: Yann Diorcet Date: Thu, 12 Jul 2012 22:49:14 +0200 Subject: [PATCH] Fix add contact and add address to (new) contact --- Classes/ContactDetailsTableViewController.h | 2 +- Classes/ContactDetailsTableViewController.m | 22 ++-- Classes/ContactDetailsViewController.h | 2 + Classes/ContactDetailsViewController.m | 51 ++++++++- Classes/ContactsTableViewController.h | 2 + Classes/ContactsTableViewController.m | 121 ++++++++------------ Classes/ContactsViewController.h | 7 +- Classes/ContactsViewController.m | 54 ++++++++- Classes/ContactsViewController.xib | 90 ++++++++++++++- Classes/DialerViewController.m | 4 +- Classes/SettingsViewController.m | 2 - 11 files changed, 248 insertions(+), 109 deletions(-) diff --git a/Classes/ContactDetailsTableViewController.h b/Classes/ContactDetailsTableViewController.h index 0a4ca9519..7ec5903b4 100644 --- a/Classes/ContactDetailsTableViewController.h +++ b/Classes/ContactDetailsTableViewController.h @@ -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; diff --git a/Classes/ContactDetailsTableViewController.m b/Classes/ContactDetailsTableViewController.m index 8f65d82f9..40f5c3f09 100644 --- a/Classes/ContactDetailsTableViewController.m +++ b/Classes/ContactDetailsTableViewController.m @@ -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]; } diff --git a/Classes/ContactDetailsViewController.h b/Classes/ContactDetailsViewController.h index 0628ffe9e..853bf28a3 100644 --- a/Classes/ContactDetailsViewController.h +++ b/Classes/ContactDetailsViewController.h @@ -39,5 +39,7 @@ - (void)newContact; - (void)newContact:(NSString*)address; +- (void)editContact:(ABRecordRef)contact; +- (void)editContact:(ABRecordRef)contact address:(NSString*)address; @end diff --git a/Classes/ContactDetailsViewController.m b/Classes/ContactDetailsViewController.m index 289d3ea67..280fdd6d1 100644 --- a/Classes/ContactDetailsViewController.m +++ b/Classes/ContactDetailsViewController.m @@ -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 diff --git a/Classes/ContactsTableViewController.h b/Classes/ContactsTableViewController.h index 7ae506cb8..5ab544424 100644 --- a/Classes/ContactsTableViewController.h +++ b/Classes/ContactsTableViewController.h @@ -27,8 +27,10 @@ ABAddressBookRef addressBook; BOOL sipFilter; + NSString *tempAddress; } +@property (nonatomic, retain) NSString* tempAddress; @property (nonatomic, assign) BOOL sipFilter; @end diff --git a/Classes/ContactsTableViewController.m b/Classes/ContactsTableViewController.m index a1b564d9f..b422153f6 100644 --- a/Classes/ContactsTableViewController.m +++ b/Classes/ContactsTableViewController.m @@ -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 diff --git a/Classes/ContactsViewController.h b/Classes/ContactsViewController.h index f713dd8b7..3c7a81d01 100644 --- a/Classes/ContactsViewController.h +++ b/Classes/ContactsViewController.h @@ -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 diff --git a/Classes/ContactsViewController.m b/Classes/ContactsViewController.m index add579a53..17f8b6efb 100644 --- a/Classes/ContactsViewController.m +++ b/Classes/ContactsViewController.m @@ -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 diff --git a/Classes/ContactsViewController.xib b/Classes/ContactsViewController.xib index abc6f1e2e..cd8f7e34e 100644 --- a/Classes/ContactsViewController.xib +++ b/Classes/ContactsViewController.xib @@ -2,10 +2,10 @@ 1296 - 11E53 + 11D50 2182 - 1138.47 - 569.00 + 1138.32 + 568.00 com.apple.InterfaceBuilder.IBCocoaTouchPlugin 1181 @@ -46,6 +46,7 @@ 292 {106, 58} + _NS:9 NO @@ -84,6 +85,7 @@ 292 {{106, 0}, {106, 58}} + _NS:9 NO @@ -112,6 +114,7 @@ 292 {{212, 0}, {108, 58}} + _NS:9 NO @@ -137,6 +140,7 @@ {320, 58} + _NS:9 @@ -153,6 +157,8 @@ 274 {{0, 58}, {320, 402}} + + _NS:10 1 @@ -179,6 +185,7 @@ {320, 460} + _NS:9 @@ -259,6 +266,15 @@ 47 + + + onAddContactClick: + + + 7 + + 86 + dataSource @@ -321,7 +337,7 @@ - header + toolBar 4 @@ -377,9 +393,71 @@ - 84 + 86 + + + + + ContactsTableViewController + UITableViewController + + IBProjectSource + ./Classes/ContactsTableViewController.h + + + + ContactsViewController + UIViewController + + id + id + id + + + + onAddContactClick: + id + + + onAllClick: + id + + + onLinphoneClick: + id + + + + UIButton + UIButton + ContactsTableViewController + UITableView + + + + allButton + UIButton + + + linphoneButton + UIButton + + + tableController + ContactsTableViewController + + + tableView + UITableView + + + + IBProjectSource + ./Classes/ContactsViewController.h + + + - 0 IBCocoaTouchFramework diff --git a/Classes/DialerViewController.m b/Classes/DialerViewController.m index 1fc186f4e..dd5370a0a 100644 --- a/Classes/DialerViewController.m +++ b/Classes/DialerViewController.m @@ -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 { diff --git a/Classes/SettingsViewController.m b/Classes/SettingsViewController.m index 8062de0a5..7480c0708 100644 --- a/Classes/SettingsViewController.m +++ b/Classes/SettingsViewController.m @@ -73,8 +73,6 @@ [self.view addSubview: navigationController.view]; } - - - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {