Lets user select country by country code.

Allow the entry of the country dial code to also pre-select/filter
the list of options. Useful for countries way down the list, or
countries that tend to be spelled in widely different ways.

Closes #662
pull/1/head
Dirk-Willem van Gulik 10 years ago committed by Frederic Jacobs
parent ff82f60e05
commit a1d0b6b1ad

@ -12,6 +12,7 @@ MacrosSingletonInterface
+ (NSString *)countryNameFromCountryCode:(NSString *)code;
+ (NSArray *)countryCodesForSearchTerm:(NSString *)searchTerm;
+ (NSString*) normalizePhoneNumber:(NSString *) number;
+(NSArray *)validCountryCallingPrefixes:(NSString *)string;
+(NSUInteger) translateCursorPosition:(NSUInteger)offset
from:(NSString*)source

@ -39,13 +39,47 @@ MacrosSingletonImplemention
if (searchTerm) {
countryCodes = [countryCodes filter:^int(NSString *code) {
NSString *countryName = [self countryNameFromCountryCode:code];
return [ContactsManager name:countryName matchesQuery:searchTerm];
NSString *callingCode = [self callingCodeFromCountryCode:code];
if ([ContactsManager name:countryName matchesQuery:searchTerm]) {
return YES;
}
// We rely on the already internationalized string; as that is what
// the user would see entered (i.e. with COUNTRY_CODE_PREFIX).
if ([callingCode containsString:searchTerm]) {
return YES;
}
return NO;
}];
}
return [self sortedCountryCodesByName:countryCodes];
}
+(NSArray *)validCountryCallingPrefixes:(NSString *)string {
NSArray *countryCodes = NSLocale.ISOCountryCodes;
NSArray *matches = [countryCodes filter:^int(NSString *code) {
NSString *callingCode = [self callingCodeFromCountryCode:code];
return [string hasPrefix:callingCode];
}];
return [matches sortedArrayWithOptions:NSSortConcurrent usingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
if (obj1 == nil) {
return obj2 ? NSOrderedAscending : NSOrderedSame;
}
if (obj2 == nil) {
return NSOrderedDescending;
}
NSInteger d = (NSInteger)[obj1 length] - (NSInteger)[obj2 length];
return d ? ( d < 0 ? NSOrderedAscending : NSOrderedDescending) : NSOrderedSame;
}];
}
+ (NSArray*)sortedCountryCodesByName:(NSArray*)countryCodesByISOCode{
return [countryCodesByISOCode sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[self countryNameFromCountryCode:obj1] caseInsensitiveCompare:[self countryNameFromCountryCode:obj2]];

Loading…
Cancel
Save