From e7473b244563f8b142a84e62cbf642b6f2900ef1 Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Fri, 9 Jan 2015 14:15:44 +0100 Subject: [PATCH] Fix save_new_contacts_as_phone_number option: factorize some code which was duplicated 4 times.. --- Classes/ContactDetailsViewController.m | 77 +++++++++----------------- 1 file changed, 26 insertions(+), 51 deletions(-) diff --git a/Classes/ContactDetailsViewController.m b/Classes/ContactDetailsViewController.m index eb038eb3e..bf13d4471 100644 --- a/Classes/ContactDetailsViewController.m +++ b/Classes/ContactDetailsViewController.m @@ -143,22 +143,19 @@ static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef inf [[LinphoneManager instance].fastAddressBook reload]; } -- (void)newContact { - [LinphoneLogger logc:LinphoneLoggerLog format:"New contact"]; - contact = NULL; - [self resetData]; - contact = ABPersonCreate(); - [tableController setContact:contact]; - [self enableEdit:FALSE]; - [[tableController tableView] reloadData]; +- (void) selectContact:(ABRecordRef)acontact andReload:(BOOL)reload { + contact = NULL; + [self resetData]; + contact = acontact; + [tableController setContact:contact]; + + if (reload) { + [self enableEdit:FALSE]; + [[tableController tableView] reloadData]; + } } -- (void)newContact:(NSString*)address { - [LinphoneLogger logc:LinphoneLoggerLog format:"New contact"]; - contact = NULL; - [self resetData]; - contact = ABPersonCreate(); - [tableController setContact:contact]; +- (void) addCurrentContactContactField:(NSString*)address { LinphoneAddress *linphoneAddress = linphone_address_new([address cStringUsingEncoding:[NSString defaultCStringEncoding]]); NSString *username = [NSString stringWithUTF8String:linphone_address_get_username(linphoneAddress)]; @@ -174,54 +171,32 @@ static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef inf } linphone_address_destroy(linphoneAddress); - [self enableEdit:FALSE]; - [[tableController tableView] reloadData]; + [self enableEdit:FALSE]; + [[tableController tableView] reloadData]; +} + +- (void)newContact { + [self selectContact:ABPersonCreate() andReload:YES]; +} + +- (void)newContact:(NSString*)address { + [self selectContact:ABPersonCreate() andReload:NO]; + [self addCurrentContactContactField:address]; } - (void)editContact:(ABRecordRef)acontact { - [LinphoneLogger logc:LinphoneLoggerLog format:"Edit contact %p", acontact]; - contact = NULL; - [self resetData]; - contact = ABAddressBookGetPersonWithRecordID(addressBook, ABRecordGetRecordID(acontact)); - [tableController setContact:contact]; - [self enableEdit:FALSE]; - [[tableController tableView] reloadData]; + [self selectContact:ABAddressBookGetPersonWithRecordID(addressBook, ABRecordGetRecordID(acontact)) andReload:YES]; } - (void)editContact:(ABRecordRef)acontact address:(NSString*)address { - [LinphoneLogger logc:LinphoneLoggerLog format:"Edit contact %p", acontact]; - contact = NULL; - [self resetData]; - contact = ABAddressBookGetPersonWithRecordID(addressBook, ABRecordGetRecordID(acontact)); - [tableController setContact:contact]; - if ([[LinphoneManager instance] lpConfigBoolForKey:@"show_contacts_emails_preference"] == true) { - LinphoneAddress *linphoneAddress = linphone_address_new([address cStringUsingEncoding:[NSString defaultCStringEncoding]]); - NSString *username = [NSString stringWithUTF8String:linphone_address_get_username(linphoneAddress)]; - if ([username rangeOfString:@"@"].length > 0) { - [tableController addEmailField:username]; - } else { - [tableController addSipField:address]; - } - linphone_address_destroy(linphoneAddress); - } else { - [tableController addSipField:address]; - } - [self enableEdit:FALSE]; - [[tableController tableView] reloadData]; + [self selectContact:ABAddressBookGetPersonWithRecordID(addressBook, ABRecordGetRecordID(acontact)) andReload:NO]; + [self addCurrentContactContactField:address]; } - -#pragma mark - Property Functions - - (void)setContact:(ABRecordRef)acontact { - [LinphoneLogger logc:LinphoneLoggerLog format:"Set contact %p", acontact]; - contact = NULL; - [self resetData]; - contact = ABAddressBookGetPersonWithRecordID(addressBook, ABRecordGetRecordID(acontact)); - [tableController setContact:contact]; + [self selectContact:ABAddressBookGetPersonWithRecordID(addressBook, ABRecordGetRecordID(acontact)) andReload:NO]; } - #pragma mark - ViewController Functions - (void)viewDidLoad{