Refactor libphonenumber and TelegramAPI out of TelegramCore

This commit is contained in:
Peter 2019-10-18 11:35:04 +04:00
parent 3e6a37c1b2
commit 5f33b7ef51
53 changed files with 106 additions and 62 deletions

View file

@ -0,0 +1,21 @@
load("//Config:buck_rule_macros.bzl", "static_library")
static_library(
name = "PhoneNumberFormat",
srcs = glob([
"Sources/**/*.swift",
"Sources/**/*.m",
]),
headers = glob([
"Sources/**/*.h",
]),
exported_headers = glob([
"Sources/**/*.h",
]),
deps = [
"//submodules/libphonenumber:libphonenumber",
],
frameworks = [
"$SDKROOT/System/Library/Frameworks/Foundation.framework",
],
)

View file

@ -0,0 +1,12 @@
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface FormatPhoneNumber : NSObject
+ (NSString *)cleanInternationalPhone:(NSString *)phone forceInternational:(bool)forceInternational;
+ (NSString *)formatPhoneNumber:(NSString *)number;
@end
NS_ASSUME_NONNULL_END

View file

@ -0,0 +1,55 @@
#import "FormatPhoneNumber.h"
#import <libphonenumber/libphonenumber.h>
static NBPhoneNumberUtil *getNBPhoneNumberUtil() {
static NBPhoneNumberUtil *value;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
value = [[NBPhoneNumberUtil alloc] init];
});
return value;
}
@implementation FormatPhoneNumber
+ (NSString *)cleanInternationalPhone:(NSString *)phone forceInternational:(bool)forceInternational {
if (phone.length == 0) {
return @"";
}
char buf[phone.length];
int bufPtr = 0;
bool hadPlus = false;
int length = (int)phone.length;
for (int i = 0; i < length; i++) {
unichar c = [phone characterAtIndex:i];
if ((c >= '0' && c <= '9') || (c == '+' && !hadPlus)) {
buf[bufPtr++] = (char)c;
if (c == '+') {
hadPlus = true;
}
}
}
NSString *result = [[NSString alloc] initWithBytes:buf length:bufPtr encoding:NSUTF8StringEncoding];
if (forceInternational && bufPtr != 0 && buf[0] != '+') {
result = [[NSString alloc] initWithFormat:@"+%@", result];
}
return result;
}
+ (NSString *)formatPhoneNumber:(NSString *)number {
NBPhoneNumber *parsed = [getNBPhoneNumberUtil() parse:[@"+" stringByAppendingString:number] defaultRegion:nil error:nil];
if (parsed == nil) {
return number;
}
NSString *result = [getNBPhoneNumberUtil() format:parsed numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:nil];
if (result == nil) {
return number;
}
return result;
}
@end

View file

@ -0,0 +1,15 @@
import Foundation
import libphonenumber
public final class InteractivePhoneFormatter {
private let formatter = NBAsYouTypeFormatter(regionCode: "US")!
public init() {
}
public func updateText(_ text: String) -> (String?, String) {
self.formatter.clear()
let string = self.formatter.inputString(text)
return (self.formatter.regionPrefix, string ?? "")
}
}

View file

@ -0,0 +1,42 @@
import Foundation
import libphonenumber
private let phoneNumberUtil = NBPhoneNumberUtil()
public func formatPhoneNumber(_ string: String) -> String {
do {
let number = try phoneNumberUtil.parse("+" + string, defaultRegion: nil)
return try phoneNumberUtil.format(number, numberFormat: .INTERNATIONAL)
} catch _ {
return string
}
}
public func isViablePhoneNumber(_ string: String) -> Bool {
return phoneNumberUtil.isViablePhoneNumber(string)
}
public class ParsedPhoneNumber: Equatable {
let rawPhoneNumber: NBPhoneNumber?
public init?(string: String) {
if let number = try? phoneNumberUtil.parse(string, defaultRegion: NB_UNKNOWN_REGION) {
self.rawPhoneNumber = number
} else {
return nil
}
}
public static func == (lhs: ParsedPhoneNumber, rhs: ParsedPhoneNumber) -> Bool {
var error: NSError?
let result = phoneNumberUtil.isNumberMatch(lhs.rawPhoneNumber, second: rhs.rawPhoneNumber, error: &error)
if error != nil {
return false
}
if result != .NO_MATCH && result != .NOT_A_NUMBER {
return true
} else {
return false
}
}
}