CB-11541 iOS: Add extra labels for phone, ims Refactor converting methods. Add posibility to use default labels: 'home fax', 'work fax'
Fix bug with contact's removal in tests Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/commit/539ad679 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/539ad679 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/539ad679 Branch: refs/heads/2.2.x Commit: 539ad679caed235f87812767bb5cd7464031bac8 Parents: 76f695e Author: Nikita Matrosov <[email protected]> Authored: Thu Nov 3 15:33:51 2016 +0300 Committer: Nikita Matrosov <[email protected]> Committed: Thu Nov 3 15:33:51 2016 +0300 ---------------------------------------------------------------------- src/ios/CDVContact.h | 13 ++++- src/ios/CDVContact.m | 138 +++++++++++++++++++--------------------------- tests/tests.js | 25 ++++++++- 3 files changed, 90 insertions(+), 86 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/539ad679/src/ios/CDVContact.h ---------------------------------------------------------------------- diff --git a/src/ios/CDVContact.h b/src/ios/CDVContact.h index 47aa764..7aed45d 100644 --- a/src/ios/CDVContact.h +++ b/src/ios/CDVContact.h @@ -53,6 +53,8 @@ typedef NSUInteger CDVContactError; - (bool)setFromContactDict:(NSDictionary*)aContact asUpdate:(BOOL)bUpdate; + (BOOL)needsConversion:(NSString*)W3Label; ++ (NSDictionary *) getContactLabels; ++ (NSArray *) filterLabels: (NSString *) contactApiLabel; + (CFStringRef)convertContactTypeToPropertyLabel:(NSString*)label; + (NSString*)convertPropertyLabelToContactType:(NSString*)label; + (BOOL)isValidW3ContactType:(NSString*)label; @@ -91,15 +93,24 @@ typedef NSUInteger CDVContactError; #define kW3ContactWorkLabel @"work" #define kW3ContactHomeLabel @"home" #define kW3ContactOtherLabel @"other" -#define kW3ContactPhoneFaxLabel @"fax" +#define kW3ContactPhoneWorkFaxLabel @"work fax" +#define kW3ContactPhoneHomeFaxLabel @"home fax" #define kW3ContactPhoneMobileLabel @"mobile" #define kW3ContactPhonePagerLabel @"pager" +#define kW3ContactPhoneIPhoneLabel @"iphone" +#define kW3ContactPhoneMainLabel @"main" #define kW3ContactUrlBlog @"blog" #define kW3ContactUrlProfile @"profile" #define kW3ContactImAIMLabel @"aim" #define kW3ContactImICQLabel @"icq" #define kW3ContactImMSNLabel @"msn" #define kW3ContactImYahooLabel @"yahoo" +#define kW3ContactImSkypeLabel @"skype" +#define kW3ContactImFacebookMessengerLabel @"facebook" +#define kW3ContactImGoogleTalkLabel @"gtalk" +#define kW3ContactImJabberLabel @"jabber" +#define kW3ContactImQQLabel @"qq" +#define kW3ContactImGaduLabel @"gadu" #define kW3ContactFieldId @"id" // special translation for IM field value and type #define kW3ContactImType @"type" http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/539ad679/src/ios/CDVContact.m ---------------------------------------------------------------------- diff --git a/src/ios/CDVContact.m b/src/ios/CDVContact.m index 676ba5e..2d56333 100644 --- a/src/ios/CDVContact.m +++ b/src/ios/CDVContact.m @@ -722,86 +722,80 @@ static NSDictionary* org_apache_cordova_contacts_defaultFields = nil; return bConvert; } +/* Return dictionary where key is contact api label, value is iPhone constant +*/ ++ (NSDictionary*) getContactLabels +{ + return @{ + kW3ContactWorkLabel : (NSString*) kABWorkLabel, + kW3ContactHomeLabel : (NSString*) kABHomeLabel, + kW3ContactOtherLabel: (NSString*) kABOtherLabel, + kW3ContactPhoneMobileLabel: (NSString*) kABPersonPhoneMobileLabel, + kW3ContactPhonePagerLabel: (NSString*) kABPersonPhonePagerLabel, + kW3ContactPhoneWorkFaxLabel: (NSString*) kABPersonPhoneWorkFAXLabel, + kW3ContactPhoneHomeFaxLabel: (NSString*) kABPersonPhoneHomeFAXLabel, + kW3ContactPhoneIPhoneLabel: (NSString*) kABPersonPhoneIPhoneLabel, + kW3ContactPhoneMainLabel: (NSString*) kABPersonPhoneMainLabel, + kW3ContactImAIMLabel: (NSString*) kABPersonInstantMessageServiceAIM, + kW3ContactImICQLabel: (NSString*) kABPersonInstantMessageServiceICQ, + kW3ContactImMSNLabel: (NSString*) kABPersonInstantMessageServiceMSN, + kW3ContactImYahooLabel: (NSString*) kABPersonInstantMessageServiceYahoo, + kW3ContactImSkypeLabel: (NSString*) kABPersonInstantMessageServiceSkype, + kW3ContactImGoogleTalkLabel: (NSString*) kABPersonInstantMessageServiceGoogleTalk, + kW3ContactImFacebookMessengerLabel: (NSString*) kABPersonInstantMessageServiceFacebook, + kW3ContactImJabberLabel: (NSString*) kABPersonInstantMessageServiceJabber, + kW3ContactImQQLabel: (NSString*) kABPersonInstantMessageServiceQQ, + kW3ContactImGaduLabel: (NSString*) kABPersonInstantMessageServiceGaduGadu, + kW3ContactUrlProfile: (NSString*) kABPersonHomePageLabel + }; +} + +/* Make case insensitive search in labels dictionary by contact API label +*/ ++ (NSArray *) filterLabels: (NSString *) contactApiLabel +{ + NSDictionary *labels = [CDVContact getContactLabels]; + NSPredicate * labelFilter = [NSPredicate predicateWithFormat:@"SELF LIKE[c] %@", contactApiLabel]; + NSArray *labelKeys = [labels allKeys]; + NSArray *result = [labelKeys filteredArrayUsingPredicate: labelFilter]; + return result; +} + /* Translation of property type labels contact API ---> iPhone * - * phone: work, home, other, mobile, fax, pager --> - * kABWorkLabel, kABHomeLabel, kABOtherLabel, kABPersonPhoneMobileLabel, kABPersonHomeFAXLabel || kABPersonHomeFAXLabel, kABPersonPhonePagerLabel + * phone: work, home, other, mobile, home fax, work fax, main, iphone, pager --> + * kABWorkLabel, kABHomeLabel, kABOtherLabel, kABPersonPhoneMobileLabel, kABPersonHomeFAXLabel, kABPersonHomeFAXLabel, kABPersonPhonePagerLabel, kABPersonPhoneIPhoneLabel, kABPersonPhoneMainLabel * emails: work, home, other ---> kABWorkLabel, kABHomeLabel, kABOtherLabel - * ims: aim, gtalk, icq, xmpp, msn, skype, qq, yahoo --> kABPersonInstantMessageService + (AIM, ICG, MSN, Yahoo). No support for gtalk, xmpp, skype, qq + * ims: aim, gtalk, icq, xmpp, msn, skype, qq, yahoo, gadu --> kABPersonInstantMessageService + (AIM, ICG, MSN, Yahoo, Gtalk, Skype, QQ, Gadu). No support for xmpp * addresses: work, home, other --> kABWorkLabel, kABHomeLabel, kABOtherLabel * * */ + (CFStringRef)convertContactTypeToPropertyLabel:(NSString*)label { - CFStringRef type; - + CFStringRef type; + NSDictionary *labels = [CDVContact getContactLabels]; if ([label isKindOfClass:[NSNull class]] || ![label isKindOfClass:[NSString class]]) { type = NULL; // no label - } else if ([label caseInsensitiveCompare:kW3ContactWorkLabel] == NSOrderedSame) { - type = kABWorkLabel; - } else if ([label caseInsensitiveCompare:kW3ContactHomeLabel] == NSOrderedSame) { - type = kABHomeLabel; - } else if ([label caseInsensitiveCompare:kW3ContactOtherLabel] == NSOrderedSame) { - type = kABOtherLabel; - } else if ([label caseInsensitiveCompare:kW3ContactPhoneMobileLabel] == NSOrderedSame) { - type = kABPersonPhoneMobileLabel; - } else if ([label caseInsensitiveCompare:kW3ContactPhonePagerLabel] == NSOrderedSame) { - type = kABPersonPhonePagerLabel; - } else if ([label caseInsensitiveCompare:kW3ContactImAIMLabel] == NSOrderedSame) { - type = kABPersonInstantMessageServiceAIM; - } else if ([label caseInsensitiveCompare:kW3ContactImICQLabel] == NSOrderedSame) { - type = kABPersonInstantMessageServiceICQ; - } else if ([label caseInsensitiveCompare:kW3ContactImMSNLabel] == NSOrderedSame) { - type = kABPersonInstantMessageServiceMSN; - } else if ([label caseInsensitiveCompare:kW3ContactImYahooLabel] == NSOrderedSame) { - type = kABPersonInstantMessageServiceYahoo; - } else if ([label caseInsensitiveCompare:kW3ContactUrlProfile] == NSOrderedSame) { - type = kABPersonHomePageLabel; - } else { + } + else { + NSArray *filteredLabels = [CDVContact filterLabels:label]; // CB-3950 If label is not one of kW3*Label constants, threat it as custom label, // otherwise fetching contact and then saving it will break this label in address book. - type = (__bridge CFStringRef)(label); + type = (__bridge CFStringRef) ([filteredLabels count] != 0 ? [labels objectForKey:[filteredLabels firstObject]] : label); } - return type; } + (NSString*)convertPropertyLabelToContactType:(NSString*)label { - NSString* type = nil; - - if (label != nil) { // improve efficiency...... - if ([label isEqualToString:(NSString*)kABPersonPhoneMobileLabel]) { - type = kW3ContactPhoneMobileLabel; - } else if ([label isEqualToString:(NSString*)kABPersonPhoneHomeFAXLabel] || - [label isEqualToString:(NSString*)kABPersonPhoneWorkFAXLabel]) { - type = kW3ContactPhoneFaxLabel; - } else if ([label isEqualToString:(NSString*)kABPersonPhonePagerLabel]) { - type = kW3ContactPhonePagerLabel; - } else if ([label isEqualToString:(NSString*)kABHomeLabel]) { - type = kW3ContactHomeLabel; - } else if ([label isEqualToString:(NSString*)kABWorkLabel]) { - type = kW3ContactWorkLabel; - } else if ([label isEqualToString:(NSString*)kABOtherLabel]) { - type = kW3ContactOtherLabel; - } else if ([label isEqualToString:(NSString*)kABPersonInstantMessageServiceAIM]) { - type = kW3ContactImAIMLabel; - } else if ([label isEqualToString:(NSString*)kABPersonInstantMessageServiceICQ]) { - type = kW3ContactImICQLabel; - } else if ([label isEqualToString:(NSString*)kABPersonInstantMessageServiceJabber]) { - type = kW3ContactOtherLabel; - } else if ([label isEqualToString:(NSString*)kABPersonInstantMessageServiceMSN]) { - type = kW3ContactImMSNLabel; - } else if ([label isEqualToString:(NSString*)kABPersonInstantMessageServiceYahoo]) { - type = kW3ContactImYahooLabel; - } else if ([label isEqualToString:(NSString*)kABPersonHomePageLabel]) { - type = kW3ContactUrlProfile; - } else { - // CB-3950 If label is not one of kW3*Label constants, threat it as custom label, - // otherwise fetching contact and then saving it will break this label in address book. - type = label; - } + NSString* type = nil; + NSDictionary *labels = [CDVContact getContactLabels]; + if (label != nil) { + NSArray *labelKeys = [labels allKeysForObject:label]; + // CB-3950 If label is not one of kW3*Label constants, threat it as custom label, + // otherwise fetching contact and then saving it will break this label in address book. + type = [labelKeys count] != 0 ? [labelKeys firstObject] : label; } return type; } @@ -814,31 +808,11 @@ static NSDictionary* org_apache_cordova_contacts_defaultFields = nil; + (BOOL)isValidW3ContactType:(NSString*)label { BOOL isValid = NO; - if ([label isKindOfClass:[NSNull class]] || ![label isKindOfClass:[NSString class]]) { isValid = NO; // no label - } else if ([label caseInsensitiveCompare:kW3ContactWorkLabel] == NSOrderedSame) { - isValid = YES; - } else if ([label caseInsensitiveCompare:kW3ContactHomeLabel] == NSOrderedSame) { - isValid = YES; - } else if ([label caseInsensitiveCompare:kW3ContactOtherLabel] == NSOrderedSame) { - isValid = YES; - } else if ([label caseInsensitiveCompare:kW3ContactPhoneMobileLabel] == NSOrderedSame) { - isValid = YES; - } else if ([label caseInsensitiveCompare:kW3ContactPhonePagerLabel] == NSOrderedSame) { - isValid = YES; - } else if ([label caseInsensitiveCompare:kW3ContactImAIMLabel] == NSOrderedSame) { - isValid = YES; - } else if ([label caseInsensitiveCompare:kW3ContactImICQLabel] == NSOrderedSame) { - isValid = YES; - } else if ([label caseInsensitiveCompare:kW3ContactImMSNLabel] == NSOrderedSame) { - isValid = YES; - } else if ([label caseInsensitiveCompare:kW3ContactImYahooLabel] == NSOrderedSame) { - isValid = YES; } else { - isValid = NO; + isValid = [[CDVContact filterLabels:label] count] != 0; } - return isValid; } http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/539ad679/tests/tests.js ---------------------------------------------------------------------- diff --git a/tests/tests.js b/tests/tests.js index 8388a93..c560a9d 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -346,6 +346,25 @@ exports.defineAutoTests = function() { }; specContext.contactObj.save(onSuccessSave, fail.bind(null, done)); }); + + it('spec 7.4 contact detail type should equal default label', function(done) { + var specContext = this; + specContext.contactObj = navigator.contacts.create({ + "displayName": "test name", + "ims": [{ + "type": "SKYPE", + "value": "000" + }] + }); + specContext.contactObj.save(onSuccessSave, fail.bind(null, done)); + function onSuccessSave(savedContact) { + specContext.contactObj = savedContact; + var imsType = savedContact.ims[0].type; + var expectedType = (cordova.platformId == 'android') ? "Skype" : "skype"; + expect(imsType).toBe(expectedType); + done(); + } + }); }); }); @@ -769,7 +788,7 @@ exports.defineAutoTests = function() { expect(found.birthday).toEqual(jasmine.any(Date)); expect(found.birthday).toEqual(bDay); done(); - }, done, this); + }, this); }, MEDIUM_TIMEOUT); it("contacts.spec.32 Find should return a contact with correct IM field", function(done) { @@ -791,11 +810,11 @@ exports.defineAutoTests = function() { expect(found.ims).toEqual(jasmine.any(Array)); expect(found.ims[0]).toBeDefined(); if (found.ims[0]) { - expect(found.ims[0].type).toEqual(ims[0].type); + expect(found.ims[0].type).toEqual(cordova.platformId == 'android' ? ims[0].type : ims[0].type.toLowerCase()); expect(found.ims[0].value).toEqual(ims[0].value); } done(); - }, done, this); + }, this); }, MEDIUM_TIMEOUT); }); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
