Various fixes

This commit is contained in:
Ilya Laktyushin
2020-08-19 19:12:38 +03:00
parent 9bcbd429aa
commit 425a95514c
54 changed files with 2129 additions and 942 deletions

View File

@@ -2,12 +2,14 @@ import Foundation
import UIKit
import Display
import AsyncDisplayKit
import SwiftSignalKit
import TelegramPresentationData
import TelegramStringFormatting
import SearchBarNode
import AppBundle
import TelegramCore
private func loadCountryCodes() -> [(String, Int)] {
private func loadCountryCodes() -> [Country] {
guard let filePath = getAppBundle().path(forResource: "PhoneCountries", ofType: "txt") else {
return []
}
@@ -21,10 +23,12 @@ private func loadCountryCodes() -> [(String, Int)] {
let delimiter = ";"
let endOfLine = "\n"
var result: [(String, Int)] = []
var result: [Country] = []
var currentLocation = data.startIndex
let locale = Locale(identifier: "en-US")
while true {
guard let codeRange = data.range(of: delimiter, options: [], range: currentLocation ..< data.endIndex) else {
break
@@ -40,8 +44,9 @@ private func loadCountryCodes() -> [(String, Int)] {
let maybeNameRange = data.range(of: endOfLine, options: [], range: idRange.upperBound ..< data.endIndex)
let countryName = locale.localizedString(forIdentifier: countryId) ?? ""
if let countryCodeInt = Int(countryCode) {
result.append((countryId, countryCodeInt))
result.append(Country(code: countryId, defaultName: countryName, name: countryName, countryCodes: [Country.CountryCode(code: countryCode, prefixes: [], patterns: [])]))
}
if let maybeNameRange = maybeNameRange {
@@ -54,7 +59,14 @@ private func loadCountryCodes() -> [(String, Int)] {
return result
}
private let countryCodes: [(String, Int)] = loadCountryCodes()
private var countryCodes: [Country] = loadCountryCodes()
public func loadServerCountryCodes(network: Network) {
let _ = (getCountriesList(network: network, langCode: "")
|> deliverOnMainQueue).start(next: { countries in
countryCodes = countries
})
}
private final class AuthorizationSequenceCountrySelectionNavigationContentNode: NavigationBarContentNode {
private let theme: PresentationTheme
@@ -117,8 +129,8 @@ private final class AuthorizationSequenceCountrySelectionNavigationContentNode:
public final class AuthorizationSequenceCountrySelectionController: ViewController {
public static func lookupCountryNameById(_ id: String, strings: PresentationStrings) -> String? {
for (itemId, _) in countryCodes {
if id == itemId {
for country in countryCodes {
if id == country.code {
let locale = localeWithStrings(strings)
if let countryName = locale.localizedString(forRegionCode: id) {
return countryName
@@ -131,9 +143,22 @@ public final class AuthorizationSequenceCountrySelectionController: ViewControll
}
public static func lookupCountryIdByCode(_ code: Int) -> String? {
for (itemId, itemCode) in countryCodes {
if itemCode == code {
return itemId
for country in countryCodes {
for countryCode in country.countryCodes {
if countryCode.code == "\(code)" {
return country.code
}
}
}
return nil
}
public static func lookupPatternByCode(_ code: Int) -> String? {
for country in countryCodes {
for countryCode in country.countryCodes {
if countryCode.code == "\(code)" {
return countryCode.patterns.first
}
}
}
return nil