From c7c3b22347b3cfd8ddcf9451a9949fa4e3221f68 Mon Sep 17 00:00:00 2001 From: Guillaume BIENKOWSKI Date: Tue, 25 Nov 2014 17:51:20 +0100 Subject: [PATCH] Factor some code in Contact management --- Classes/ContactDetailsTableViewController.m | 163 +++++++------------- 1 file changed, 54 insertions(+), 109 deletions(-) diff --git a/Classes/ContactDetailsTableViewController.m b/Classes/ContactDetailsTableViewController.m index bc09f348b..abaaaa2d4 100644 --- a/Classes/ContactDetailsTableViewController.m +++ b/Classes/ContactDetailsTableViewController.m @@ -156,6 +156,15 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe return nil; } +- (ABPropertyID)propertyIDForSection:(ContactSections_e)section { + switch (section) { + case ContactSections_Sip: return kABPersonInstantMessageProperty; + case ContactSections_Number: return kABPersonPhoneProperty; + case ContactSections_Email: return kABPersonEmailProperty; + default: return kABInvalidPropertyType; + } +} + + (NSString*)localizeLabel:(NSString*)str { CFStringRef lLocalizedLabel = ABAddressBookCopyLocalizedLabel((CFStringRef) str); NSString * retStr = [NSString stringWithString:(NSString*) lLocalizedLabel]; @@ -279,8 +288,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); ABMultiValueReplaceValueAtIndex(lMap, lDict, index); } else { - - CFStringRef label = (CFStringRef)[labelArray objectAtIndex:0]; + CFStringRef label = (CFStringRef)[labelArray objectAtIndex:0]; ABMultiValueAddValueAndLabel(lMap, lDict, label, &index); } @@ -411,37 +419,21 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe - (void)removeEmptyEntry:(UITableView*)tableview section:(NSInteger)section animated:(BOOL)animated { NSMutableArray *sectionDict = [self getSectionData:section]; - int row = [sectionDict count] - 1; + NSInteger row = [sectionDict count] - 1; if(row >= 0) { Entry *entry = [sectionDict objectAtIndex:row]; - if(contactSections[section] == ContactSections_Number) { - ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index); - if(![(NSString*) valueRef length]) { - [self removeEntry:tableview path:[NSIndexPath indexPathForRow:row inSection:section] animated:animated]; - } - CFRelease(valueRef); - CFRelease(lMap); - } else if(contactSections[section] == ContactSections_Sip) { - ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - CFDictionaryRef lDict = ABMultiValueCopyValueAtIndex(lMap, index); - CFStringRef valueRef = CFDictionaryGetValue(lDict, kABPersonInstantMessageUsernameKey); - if(![(NSString*) valueRef length]) { - [self removeEntry:tableview path:[NSIndexPath indexPathForRow:row inSection:section] animated:animated]; - } - CFRelease(lDict); - CFRelease(lMap); - } else if(contactSections[section] == ContactSections_Email) { - ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index); + + ABPropertyID property = [self propertyIDForSection:contactSections[section]]; + if( property != kABInvalidPropertyType ){ + ABMultiValueRef lMap = ABRecordCopyValue(contact, property); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + CFTypeRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index); if(![(NSString*) valueRef length]) { [self removeEntry:tableview path:[NSIndexPath indexPathForRow:row inSection:section] animated:animated]; } CFRelease(valueRef); CFRelease(lMap); + } } if(contactDetailsDelegate != nil) { @@ -451,32 +443,19 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe - (void)removeEntry:(UITableView*)tableview path:(NSIndexPath*)indexPath animated:(BOOL)animated { NSMutableArray *sectionArray = [self getSectionData:[indexPath section]]; - Entry *entry = [sectionArray objectAtIndex:[indexPath row]]; - if(contactSections[[indexPath section]] == ContactSections_Number) { - ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); + Entry *entry = [sectionArray objectAtIndex:[indexPath row]]; + ABPropertyID property = [self propertyIDForSection:contactSections[indexPath.section]]; + + if( property != kABInvalidPropertyType ){ + ABMultiValueRef lcMap = ABRecordCopyValue(contact, property); ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); CFRelease(lcMap); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); ABMultiValueRemoveValueAndLabelAtIndex(lMap, index); - ABRecordSetValue(contact, kABPersonPhoneProperty, lMap, nil); - CFRelease(lMap); - } else if(contactSections[[indexPath section]] == ContactSections_Sip) { - ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty); - ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); - CFRelease(lcMap); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - ABMultiValueRemoveValueAndLabelAtIndex(lMap, index); - ABRecordSetValue(contact, kABPersonInstantMessageProperty, lMap, nil); - CFRelease(lMap); - } else if(contactSections[[indexPath section]] == ContactSections_Email) { - ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonEmailProperty); - ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); - CFRelease(lcMap); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - ABMultiValueRemoveValueAndLabelAtIndex(lMap, index); - ABRecordSetValue(contact, kABPersonEmailProperty, lMap, nil); + ABRecordSetValue(contact, property, lMap, nil); CFRelease(lMap); } + [sectionArray removeObjectAtIndex:[indexPath row]]; NSArray *tagInsertIndexPath = [NSArray arrayWithObject:indexPath]; @@ -545,7 +524,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe if(contactSections[[indexPath section]] == ContactSections_Number) { ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); if(labelRef != NULL) { label = [ContactDetailsTableViewController localizeLabel:(NSString*) labelRef]; @@ -559,7 +538,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe CFRelease(lMap); } else if(contactSections[[indexPath section]] == ContactSections_Sip) { ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); if(labelRef != NULL) { label = [ContactDetailsTableViewController localizeLabel:(NSString*) labelRef]; @@ -584,7 +563,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe CFRelease(lMap); } else if(contactSections[[indexPath section]] == ContactSections_Email) { ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); if(labelRef != NULL) { label = [ContactDetailsTableViewController localizeLabel:(NSString*) labelRef]; @@ -621,7 +600,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe NSString *dest=NULL;; if(contactSections[[indexPath section]] == ContactSections_Number) { ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index); if(valueRef != NULL) { dest = [ContactDetailsTableViewController localizeLabel:(NSString*) valueRef]; @@ -630,7 +609,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe CFRelease(lMap); } else if(contactSections[[indexPath section]] == ContactSections_Sip) { ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); CFDictionaryRef lDict = ABMultiValueCopyValueAtIndex(lMap, index); CFStringRef valueRef = CFDictionaryGetValue(lDict, kABPersonInstantMessageUsernameKey); dest = [FastAddressBook normalizeSipURI:[NSString stringWithString:(NSString*) valueRef]]; @@ -638,7 +617,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe CFRelease(lMap); } else if(contactSections[[indexPath section]] == ContactSections_Email) { ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index); if(valueRef != NULL) { dest = [FastAddressBook normalizeSipURI:[NSString stringWithString:(NSString*) valueRef]]; @@ -666,28 +645,12 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe } } else { NSString *key = nil; - if(contactSections[[indexPath section]] == ContactSections_Number) { - ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); - if(labelRef != NULL) { - key = [NSString stringWithString:(NSString*) labelRef]; - CFRelease(labelRef); - } - CFRelease(lMap); - } else if(contactSections[[indexPath section]] == ContactSections_Sip) { - ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); - if(labelRef != NULL) { - key = [NSString stringWithString:(NSString*) labelRef]; - CFRelease(labelRef); - } - CFRelease(lMap); - } else if(contactSections[[indexPath section]] == ContactSections_Email) { - ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); + ABPropertyID property = [self propertyIDForSection:contactSections[indexPath.section]]; + + if( property != kABInvalidPropertyType ){ + ABMultiValueRef lMap = ABRecordCopyValue(contact, property); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + CFTypeRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); if(labelRef != NULL) { key = [NSString stringWithString:(NSString*) labelRef]; CFRelease(labelRef); @@ -772,7 +735,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { - int last_index = [[self getSectionData:[indexPath section]] count] - 1; + NSInteger last_index = [[self getSectionData:[indexPath section]] count] - 1; if (indexPath.row == last_index) { return UITableViewCellEditingStyleInsert; } @@ -843,10 +806,12 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe - (void)changeContactDetailsLabel:(NSString *)value { if(value != nil) { - NSMutableArray *sectionDict = [self getSectionData:[editingIndexPath section]]; - Entry *entry = [sectionDict objectAtIndex:[editingIndexPath row]]; - ContactSections_e thesection = contactSections[[editingIndexPath section]]; - if(thesection == ContactSections_Number) { + NSInteger section = editingIndexPath.section; + NSMutableArray *sectionDict = [self getSectionData:section]; + ABPropertyID property = [self propertyIDForSection:(int)section]; + Entry *entry = [sectionDict objectAtIndex:editingIndexPath.row]; + + if( property != kABInvalidPropertyType ){ ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); CFRelease(lcMap); @@ -854,23 +819,8 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe ABMultiValueReplaceLabelAtIndex(lMap, (CFStringRef)(value), index); ABRecordSetValue(contact, kABPersonPhoneProperty, lMap, nil); CFRelease(lMap); - } else if(thesection == ContactSections_Sip) { - ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty); - ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); - CFRelease(lcMap); - NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - ABMultiValueReplaceLabelAtIndex(lMap, (CFStringRef)(value), index); - ABRecordSetValue(contact, kABPersonInstantMessageProperty, lMap, nil); - CFRelease(lMap); - } else if(thesection == ContactSections_Email) { - ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonEmailProperty); - ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); - CFRelease(lcMap); - NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - ABMultiValueReplaceLabelAtIndex(lMap, (CFStringRef)(value), index); - ABRecordSetValue(contact, kABPersonEmailProperty, lMap, nil); - CFRelease(lMap); } + [self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject: editingIndexPath] withRowAnimation:FALSE]; [self.tableView reloadSectionIndexTitles]; @@ -906,26 +856,21 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe Entry *entry = [sectionDict objectAtIndex:[path row]]; ContactSections_e sect = contactSections[[path section]]; + ABPropertyID property = [self propertyIDForSection:sect]; NSString *value = [textField text]; - if(sect == ContactSections_Number) { - ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); - ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); - CFRelease(lcMap); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); - ABMultiValueReplaceValueAtIndex(lMap, (CFStringRef)value, index); - ABRecordSetValue(contact, kABPersonPhoneProperty, lMap, nil); - CFRelease(lMap); - } else if(sect == ContactSections_Sip) { + + if(sect == ContactSections_Sip) { [self setSipContactEntry:entry withValue:value]; - } else if(sect == ContactSections_Email) { - ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonEmailProperty); + } else if( property != kABInvalidPropertyType ){ + ABMultiValueRef lcMap = ABRecordCopyValue(contact, property); ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); CFRelease(lcMap); - int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); ABMultiValueReplaceValueAtIndex(lMap, (CFStringRef)value, index); - ABRecordSetValue(contact, kABPersonEmailProperty, lMap, nil); + ABRecordSetValue(contact, property, lMap, nil); CFRelease(lMap); } + [cell.detailTextLabel setText:value]; } else { [LinphoneLogger logc:LinphoneLoggerError format:"Not valid UIEditableTableViewCell"];