mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
Reduce time between editing contacts and seeing those changes in the app
* Move a couple lib methods behind our own interface * Cache parsing phone numbers since it's expensive * I considered caching formatting as well since it's also a bit expensive, but generating an appropriate cache key was actually slower than the raw implementation. // FREEBIE
This commit is contained in:
parent
e6ff79c126
commit
1ee30023b9
|
@ -16,7 +16,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
|
||||||
OWSAssert(text != nil);
|
OWSAssert(text != nil);
|
||||||
OWSAssert(regionCode != nil);
|
OWSAssert(regionCode != nil);
|
||||||
|
|
||||||
NBPhoneNumberUtil *phoneUtil = [PhoneNumberUtil sharedUtil].nbPhoneNumberUtil;
|
PhoneNumberUtil *phoneUtil = [PhoneNumberUtil sharedUtil];
|
||||||
|
|
||||||
NSError *parseError = nil;
|
NSError *parseError = nil;
|
||||||
NBPhoneNumber *number = [phoneUtil parse:text defaultRegion:regionCode error:&parseError];
|
NBPhoneNumber *number = [phoneUtil parse:text defaultRegion:regionCode error:&parseError];
|
||||||
|
|
|
@ -26,4 +26,9 @@
|
||||||
|
|
||||||
+ (instancetype)sharedUtil;
|
+ (instancetype)sharedUtil;
|
||||||
|
|
||||||
|
- (NBPhoneNumber *)parse:(NSString *)numberToParse defaultRegion:(NSString *)defaultRegion error:(NSError **)error;
|
||||||
|
- (NSString *)format:(NBPhoneNumber *)phoneNumber
|
||||||
|
numberFormat:(NBEPhoneNumberFormat)numberFormat
|
||||||
|
error:(NSError **)error;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -6,10 +6,12 @@
|
||||||
#import "ContactsManagerProtocol.h"
|
#import "ContactsManagerProtocol.h"
|
||||||
#import "FunctionalUtil.h"
|
#import "FunctionalUtil.h"
|
||||||
#import "Util.h"
|
#import "Util.h"
|
||||||
|
#import <libPhoneNumber-iOS/NBPhoneNumber.h>
|
||||||
|
|
||||||
@interface PhoneNumberUtil ()
|
@interface PhoneNumberUtil ()
|
||||||
|
|
||||||
@property (nonatomic, readonly) NSMutableDictionary *countryCodesFromCallingCodeCache;
|
@property (nonatomic, readonly) NSMutableDictionary *countryCodesFromCallingCodeCache;
|
||||||
|
@property (nonatomic, readonly) NSCache *parsedPhoneNumberCache;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@ -32,6 +34,7 @@
|
||||||
if (self) {
|
if (self) {
|
||||||
_nbPhoneNumberUtil = [[NBPhoneNumberUtil alloc] init];
|
_nbPhoneNumberUtil = [[NBPhoneNumberUtil alloc] init];
|
||||||
_countryCodesFromCallingCodeCache = [NSMutableDictionary new];
|
_countryCodesFromCallingCodeCache = [NSMutableDictionary new];
|
||||||
|
_parsedPhoneNumberCache = [NSCache new];
|
||||||
|
|
||||||
OWSSingletonAssert();
|
OWSSingletonAssert();
|
||||||
}
|
}
|
||||||
|
@ -39,6 +42,37 @@
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (nullable NBPhoneNumber *)parse:(NSString *)numberToParse
|
||||||
|
defaultRegion:(NSString *)defaultRegion
|
||||||
|
error:(NSError **)error
|
||||||
|
{
|
||||||
|
NSString *hashKey = [NSString stringWithFormat:@"numberToParse:%@defaultRegion:%@", numberToParse, defaultRegion];
|
||||||
|
|
||||||
|
NBPhoneNumber *result = [self.parsedPhoneNumberCache objectForKey:hashKey];
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
result = [self.nbPhoneNumberUtil parse:numberToParse defaultRegion:defaultRegion error:error];
|
||||||
|
if (result) {
|
||||||
|
[self.parsedPhoneNumberCache setObject:result forKey:hashKey];
|
||||||
|
} else {
|
||||||
|
[self.parsedPhoneNumberCache setObject:[NSNull null] forKey:hashKey];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([result class] == [NSNull class]) {
|
||||||
|
return nil;
|
||||||
|
} else {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSString *)format:(NBPhoneNumber *)phoneNumber
|
||||||
|
numberFormat:(NBEPhoneNumberFormat)numberFormat
|
||||||
|
error:(NSError **)error
|
||||||
|
{
|
||||||
|
return [self.nbPhoneNumberUtil format:phoneNumber numberFormat:numberFormat error:error];
|
||||||
|
}
|
||||||
|
|
||||||
// country code -> country name
|
// country code -> country name
|
||||||
+ (NSString *)countryNameFromCountryCode:(NSString *)countryCode {
|
+ (NSString *)countryNameFromCountryCode:(NSString *)countryCode {
|
||||||
NSDictionary *countryCodeComponent = @{NSLocaleCountryCode : countryCode};
|
NSDictionary *countryCodeComponent = @{NSLocaleCountryCode : countryCode};
|
||||||
|
|
Loading…
Reference in a new issue