[CNContact] check duplicated contact on edition

This commit is contained in:
Brieuc Viel 2017-12-05 14:26:54 +01:00
parent dd789a5ed6
commit 72b0d22deb
12 changed files with 89 additions and 21 deletions

View file

@ -473,13 +473,18 @@ static UICompositeViewDescription *compositeDescription = nil;
- (IBAction)onEditClick:(id)event {
if (_tableController.isEditing) {
[self setEditing:FALSE];
[self saveData];
_isAdding = FALSE;
self.tmpContact = NULL;
_avatarImage.hidden = FALSE;
_deleteButton.hidden = FALSE;
_editButton.hidden = FALSE;
if(![self hasDuplicateContactOf:_contact]){
[self setEditing:FALSE];
[self saveData];
_isAdding = FALSE;
self.tmpContact = NULL;
_avatarImage.hidden = FALSE;
_deleteButton.hidden = FALSE;
_editButton.hidden = FALSE;
}else{
LOGE(@"====>>>> Duplicated Contacts detected !!!");
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Contact error", nil) message:NSLocalizedString(@"Contact duplicate", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Continue", nil] show];
}
} else {
[self modifyTmpContact:_contact];
[self setEditing:TRUE];
@ -517,6 +522,30 @@ static UICompositeViewDescription *compositeDescription = nil;
}
}
- (BOOL) hasDuplicateContactOf:(Contact*) contactToCheck{
CNContactStore *store = [[CNContactStore alloc] init];
NSArray *keysToFetch = @[
CNContactEmailAddressesKey, CNContactPhoneNumbersKey,
CNContactInstantMessageAddressesKey, CNInstantMessageAddressUsernameKey,
CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey,
CNContactIdentifierKey, CNContactImageDataKey, CNContactNicknameKey
];
CNMutableContact *mCNContact =
[[store unifiedContactWithIdentifier:contactToCheck.identifier keysToFetch:keysToFetch error:nil] mutableCopy];
if(mCNContact == NULL){
for(NSString *address in contactToCheck.sipAddresses){
NSString *name = [FastAddressBook normalizeSipURI:address];
if([LinphoneManager.instance.fastAddressBook.addressBookMap objectForKey:name]){
return TRUE;
}
}
return FALSE;
}else{
return FALSE;
}
}
#pragma mark - Image picker delegate
- (void)imagePickerDelegateImage:(UIImage *)image info:(NSDictionary *)info {

View file

@ -219,8 +219,7 @@ static UICompositeViewDescription *compositeDescription = nil;
}
- (IBAction)onEditionChangeClick:(id)sender {
allButton.hidden = linphoneButton.hidden = _selectedButtonImage.hidden = addButton.hidden =
self.tableController.isEditing;
allButton.hidden = linphoneButton.hidden = _selectedButtonImage.hidden = addButton.hidden = self.tableController.isEditing;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

View file

@ -293,9 +293,6 @@
return ret;
}
- (BOOL)deleteContact:(Contact *)contact {
return [self deleteCNContact:contact.person];
}
- (CNContact *)getCNContactFromContact:(Contact *)acontact {
NSArray *keysToFetch = @[
@ -312,20 +309,47 @@
}
- (BOOL)deleteCNContact:(CNContact *)contact {
return TRUE;//[self deleteContact:] ;
}
- (BOOL)deleteContact:(Contact *)contact {
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest deleteContact:[contact mutableCopy]];
@try {
BOOL success = [store executeSaveRequest:saveRequest error:nil];
NSLog(@"Success %d", success);
if(success)
[self fetchContactsInBackGroundThread];
} @catch (NSException *exception) {
NSLog(@"description = %@", [exception description]);
return FALSE;
NSArray *keysToFetch = @[
CNContactEmailAddressesKey, CNContactPhoneNumbersKey,
CNContactInstantMessageAddressesKey, CNInstantMessageAddressUsernameKey,
CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey,
CNContactIdentifierKey, CNContactImageDataKey, CNContactNicknameKey
];
CNMutableContact *mCNContact =
[[store unifiedContactWithIdentifier:contact.identifier
keysToFetch:keysToFetch
error:nil] mutableCopy];
if(mCNContact != nil){
[saveRequest deleteContact:mCNContact];
@try {
BOOL success = [store executeSaveRequest:saveRequest error:nil];
NSLog(@"Success %d", success);
[self removeFriend:contact ];
[LinphoneManager.instance setContactsUpdated:TRUE];
if([contact.sipAddresses count] > 0){
for (NSString *sip in contact.sipAddresses) {
[_addressBookMap removeObjectForKey:([FastAddressBook normalizeSipURI:sip] ?: sip)];
}
}
if([contact.phones count] > 0){
for (NSString *phone in contact.phones) {
[_addressBookMap removeObjectForKey:([FastAddressBook normalizeSipURI:phone] ?: phone)];
}
}
} @catch (NSException *exception) {
NSLog(@"description = %@", [exception description]);
return FALSE;
}
}
return TRUE;
}
- (BOOL)deleteAllContacts {
NSArray *keys = @[ CNContactPhoneNumbersKey ];
NSString *containerId = store.defaultContainerIdentifier;
@ -416,4 +440,16 @@
lists = lists->next;
}
}
-(void)removeFriend:(Contact*) contact{
BOOL enabled = [LinphoneManager.instance lpConfigBoolForKey:@"use_rls_presence"];
const MSList *lists = linphone_core_get_friends_lists(LC);
while (lists) {
linphone_friend_list_remove_friend(lists->data, contact.friend);
linphone_friend_list_enable_subscriptions(lists->data, FALSE);
linphone_friend_list_enable_subscriptions(lists->data, enabled);
linphone_friend_list_update_subscriptions(lists->data);
lists = lists->next;
}
}
@end

View file

@ -37,6 +37,7 @@
244523B01E8266CC0037A187 /* chat_error.png in Resources */ = {isa = PBXBuildFile; fileRef = 244523AD1E8266CC0037A187 /* chat_error.png */; };
244523B11E8266CC0037A187 /* chat_read.png in Resources */ = {isa = PBXBuildFile; fileRef = 244523AE1E8266CC0037A187 /* chat_read.png */; };
244523BE1E8D3A6C0037A187 /* chat_unsecure.png in Resources */ = {isa = PBXBuildFile; fileRef = 244523BC1E8D3A6C0037A187 /* chat_unsecure.png */; };
249660951FD6A35F001D55AA /* Photos.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 249660941FD6A359001D55AA /* Photos.framework */; };
24A3459E1D95797700881A5C /* UIShopTableCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 24A3459D1D95797700881A5C /* UIShopTableCell.xib */; };
24A345A61D95798A00881A5C /* UIShopTableCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 24A345A51D95798A00881A5C /* UIShopTableCell.m */; };
24E1C7C01F9A235600D3F981 /* Contacts.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24E1C7B91F9A235500D3F981 /* Contacts.framework */; };
@ -945,6 +946,7 @@
244523AD1E8266CC0037A187 /* chat_error.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = chat_error.png; sourceTree = "<group>"; };
244523AE1E8266CC0037A187 /* chat_read.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = chat_read.png; sourceTree = "<group>"; };
244523BC1E8D3A6C0037A187 /* chat_unsecure.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = chat_unsecure.png; sourceTree = "<group>"; };
249660941FD6A359001D55AA /* Photos.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Photos.framework; path = System/Library/Frameworks/Photos.framework; sourceTree = SDKROOT; };
24A3459D1D95797700881A5C /* UIShopTableCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = UIShopTableCell.xib; sourceTree = "<group>"; };
24A345A51D95798A00881A5C /* UIShopTableCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UIShopTableCell.m; sourceTree = "<group>"; };
24A345A71D95799900881A5C /* UIShopTableCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIShopTableCell.h; sourceTree = "<group>"; };
@ -1849,6 +1851,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
249660951FD6A35F001D55AA /* Photos.framework in Frameworks */,
24E1C7C01F9A235600D3F981 /* Contacts.framework in Frameworks */,
8C5BCED61EB3859300A9AAEF /* mediastreamer_voip.framework in Frameworks */,
8C2595DF1DEDCC8E007A6424 /* CallKit.framework in Frameworks */,
@ -2210,6 +2213,7 @@
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
isa = PBXGroup;
children = (
249660941FD6A359001D55AA /* Photos.framework */,
24E1C7B91F9A235500D3F981 /* Contacts.framework */,
8C3EAA191EB8D9C300B732B6 /* linphonetester.framework */,
8C5BCEC61EB3859200A9AAEF /* bctoolbox-tester.framework */,