mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-03-25 04:38:23 +00:00
Moved countries to singleton.
This commit is contained in:
parent
a230e83778
commit
86aaa9673d
@ -10,6 +10,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
namespace Countries {
|
||||
namespace {
|
||||
|
||||
auto SingleInstance = CountriesInstance();
|
||||
|
||||
const std::array<Info, 231> FallbackList = { {
|
||||
{ "Afghanistan", "AF", "93" },
|
||||
{ "Albania", "AL", "355" },
|
||||
@ -244,40 +246,37 @@ const std::array<Info, 231> FallbackList = { {
|
||||
{ "Zimbabwe", "ZW", "263" },
|
||||
} };
|
||||
|
||||
QHash<QString, const Info *> ByCode;
|
||||
QHash<QString, const Info *> ByISO2;
|
||||
|
||||
} // namespace
|
||||
|
||||
const std::array<Info, 231> &List() {
|
||||
const std::array<Info, 231> &CountriesInstance::list() {
|
||||
return FallbackList;
|
||||
}
|
||||
|
||||
const QHash<QString, const Info *> &InfoByCode() {
|
||||
if (ByCode.isEmpty()) {
|
||||
ByCode.reserve(FallbackList.size());
|
||||
const CountriesInstance::Map &CountriesInstance::byCode() {
|
||||
if (_byCode.empty()) {
|
||||
_byCode.reserve(FallbackList.size());
|
||||
for (const auto &entry : FallbackList) {
|
||||
ByCode.insert(entry.code, &entry);
|
||||
_byCode.insert(entry.code, &entry);
|
||||
}
|
||||
}
|
||||
return ByCode;
|
||||
return _byCode;
|
||||
}
|
||||
|
||||
const QHash<QString, const Info *> &InfoByISO2() {
|
||||
if (ByISO2.isEmpty()) {
|
||||
ByISO2.reserve(FallbackList.size());
|
||||
const CountriesInstance::Map &CountriesInstance::byISO2() {
|
||||
if (_byISO2.empty()) {
|
||||
_byISO2.reserve(FallbackList.size());
|
||||
for (const auto &entry : FallbackList) {
|
||||
ByISO2.insert(entry.iso2, &entry);
|
||||
_byISO2.insert(entry.iso2, &entry);
|
||||
}
|
||||
}
|
||||
return ByISO2;
|
||||
return _byISO2;
|
||||
}
|
||||
|
||||
QString ValidPhoneCode(QString fullCode) {
|
||||
const auto &byCode = InfoByCode();
|
||||
QString CountriesInstance::validPhoneCode(QString fullCode) {
|
||||
const auto &listByCode = byCode();
|
||||
while (fullCode.length()) {
|
||||
const auto i = byCode.constFind(fullCode);
|
||||
if (i != byCode.cend()) {
|
||||
const auto i = listByCode.constFind(fullCode);
|
||||
if (i != listByCode.cend()) {
|
||||
return (*i)->code;
|
||||
}
|
||||
fullCode.chop(1);
|
||||
@ -285,17 +284,25 @@ QString ValidPhoneCode(QString fullCode) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString CountryNameByISO2(const QString &iso) {
|
||||
const auto &byISO2 = InfoByISO2();
|
||||
const auto i = byISO2.constFind(iso);
|
||||
return (i != byISO2.cend()) ? QString::fromUtf8((*i)->name) : QString();
|
||||
QString CountriesInstance::countryNameByISO2(const QString &iso) {
|
||||
const auto &listByISO2 = byISO2();
|
||||
const auto i = listByISO2.constFind(iso);
|
||||
return (i != listByISO2.cend())
|
||||
? QString::fromUtf8((*i)->name)
|
||||
: QString();
|
||||
}
|
||||
|
||||
QString CountryISO2ByPhone(const QString &phone) {
|
||||
const auto &byCode = InfoByCode();
|
||||
const auto code = ValidPhoneCode(phone);
|
||||
const auto i = byCode.find(code);
|
||||
return (i != byCode.cend()) ? QString::fromUtf8((*i)->iso2) : QString();
|
||||
QString CountriesInstance::countryISO2ByPhone(const QString &phone) {
|
||||
const auto &listByCode = byCode();
|
||||
const auto code = validPhoneCode(phone);
|
||||
const auto i = listByCode.find(code);
|
||||
return (i != listByCode.cend())
|
||||
? QString::fromUtf8((*i)->iso2)
|
||||
: QString();
|
||||
}
|
||||
|
||||
CountriesInstance &Instance() {
|
||||
return SingleInstance;
|
||||
}
|
||||
|
||||
} // namespace Countries
|
||||
|
@ -17,13 +17,26 @@ struct Info {
|
||||
const char *alternativeName = nullptr;
|
||||
};
|
||||
|
||||
[[nodiscard]] const std::array<Info, 231> &List();
|
||||
class CountriesInstance final {
|
||||
public:
|
||||
using Map = QHash<QString, const Info *>;
|
||||
|
||||
[[nodiscard]] const QHash<QString, const Info *> &InfoByCode();
|
||||
[[nodiscard]] const QHash<QString, const Info *> &InfoByISO2();
|
||||
CountriesInstance() = default;
|
||||
[[nodiscard]] const std::array<Info, 231> &list();
|
||||
|
||||
[[nodiscard]] QString ValidPhoneCode(QString fullCode);
|
||||
[[nodiscard]] QString CountryNameByISO2(const QString &iso);
|
||||
[[nodiscard]] QString CountryISO2ByPhone(const QString &phone);
|
||||
[[nodiscard]] const Map &byCode();
|
||||
[[nodiscard]] const Map &byISO2();
|
||||
|
||||
[[nodiscard]] QString validPhoneCode(QString fullCode);
|
||||
[[nodiscard]] QString countryNameByISO2(const QString &iso);
|
||||
[[nodiscard]] QString countryISO2ByPhone(const QString &phone);
|
||||
|
||||
private:
|
||||
Map _byCode;
|
||||
Map _byISO2;
|
||||
|
||||
};
|
||||
|
||||
CountriesInstance &Instance();
|
||||
|
||||
} // namespace Countries
|
||||
|
@ -54,7 +54,7 @@ using namespace ::Intro::details;
|
||||
if (const auto parent
|
||||
= Core::App().domain().maybeLastOrSomeAuthedAccount()) {
|
||||
if (const auto session = parent->maybeSession()) {
|
||||
const auto iso = Countries::CountryISO2ByPhone(
|
||||
const auto iso = Countries::Instance().countryISO2ByPhone(
|
||||
session->user()->phone());
|
||||
if (!iso.isEmpty()) {
|
||||
return iso;
|
||||
|
@ -123,7 +123,7 @@ EditDocumentScheme GetDocumentScheme(
|
||||
using ValueClass = Scheme::ValueClass;
|
||||
const auto DontFormat = nullptr;
|
||||
const auto CountryFormat = [](const QString &value) {
|
||||
const auto result = Countries::CountryNameByISO2(value);
|
||||
const auto result = Countries::Instance().countryNameByISO2(value);
|
||||
return result.isEmpty() ? value : result;
|
||||
};
|
||||
const auto GenderFormat = [](const QString &value) {
|
||||
@ -322,7 +322,8 @@ EditDocumentScheme GetDocumentScheme(
|
||||
if (!language.isEmpty()) {
|
||||
return tr::lng_passport_native_name_language_about(tr::now);
|
||||
}
|
||||
const auto name = Countries::CountryNameByISO2(countryCode);
|
||||
const auto name = Countries::Instance().countryNameByISO2(
|
||||
countryCode);
|
||||
Assert(!name.isEmpty());
|
||||
return tr::lng_passport_native_name_about(
|
||||
tr::now,
|
||||
|
@ -520,7 +520,7 @@ void PanelEditDocument::createDetailsRow(
|
||||
object_ptr<Ui::BoxContent> box) {
|
||||
controller->show(std::move(box));
|
||||
};
|
||||
const auto isoByPhone = Countries::CountryISO2ByPhone(
|
||||
const auto isoByPhone = Countries::Instance().countryISO2ByPhone(
|
||||
_controller->bot()->session().user()->phone());
|
||||
|
||||
const auto [it, ok] = _details.emplace(
|
||||
|
@ -304,7 +304,7 @@ void AbstractTextRow<Input>::finishInnerAnimating() {
|
||||
}
|
||||
|
||||
QString CountryString(const QString &code) {
|
||||
const auto name = Countries::CountryNameByISO2(code);
|
||||
const auto name = Countries::Instance().countryNameByISO2(code);
|
||||
return name.isEmpty() ? tr::lng_passport_country_choose(tr::now) : name;
|
||||
}
|
||||
|
||||
@ -383,7 +383,7 @@ void CountryRow::errorAnimationCallback() {
|
||||
|
||||
void CountryRow::chooseCountry() {
|
||||
const auto top = _value.current();
|
||||
const auto name = Countries::CountryNameByISO2(top);
|
||||
const auto name = Countries::Instance().countryNameByISO2(top);
|
||||
const auto country = !name.isEmpty()
|
||||
? top
|
||||
: !_defaultCountry.isEmpty()
|
||||
|
@ -465,7 +465,7 @@ QString Form::defaultPhone() const {
|
||||
}
|
||||
|
||||
QString Form::defaultCountry() const {
|
||||
return Countries::CountryISO2ByPhone(defaultPhone());
|
||||
return Countries::Instance().countryISO2ByPhone(defaultPhone());
|
||||
}
|
||||
|
||||
void Form::fillPaymentMethodInformation() {
|
||||
|
@ -189,7 +189,7 @@ struct SimpleFieldState {
|
||||
|
||||
[[nodiscard]] QString Parse(const FieldConfig &config) {
|
||||
if (config.type == FieldType::Country) {
|
||||
return Countries::CountryNameByISO2(config.value);
|
||||
return Countries::Instance().countryNameByISO2(config.value);
|
||||
} else if (config.type == FieldType::Money) {
|
||||
const auto amount = config.value.toLongLong();
|
||||
if (!amount) {
|
||||
@ -490,7 +490,8 @@ void Field::setupCountry() {
|
||||
QObject::connect(_masked, &MaskedInputField::focused, [=] {
|
||||
setFocus();
|
||||
|
||||
const auto name = Countries::CountryNameByISO2(_countryIso2);
|
||||
const auto name = Countries::Instance().countryNameByISO2(
|
||||
_countryIso2);
|
||||
const auto country = !name.isEmpty()
|
||||
? _countryIso2
|
||||
: !_config.defaultCountry.isEmpty()
|
||||
@ -503,7 +504,7 @@ void Field::setupCountry() {
|
||||
raw->countryChosen(
|
||||
) | rpl::start_with_next([=](QString iso2) {
|
||||
_countryIso2 = iso2;
|
||||
_masked->setText(Countries::CountryNameByISO2(iso2));
|
||||
_masked->setText(Countries::Instance().countryNameByISO2(iso2));
|
||||
_masked->hideError();
|
||||
raw->closeBox();
|
||||
if (!iso2.isEmpty()) {
|
||||
|
@ -508,7 +508,7 @@ void FormSummary::setupSections(not_null<VerticalLayout*> layout) {
|
||||
push(_information.shippingAddress.address2);
|
||||
push(_information.shippingAddress.city);
|
||||
push(_information.shippingAddress.state);
|
||||
push(Countries::CountryNameByISO2(
|
||||
push(Countries::Instance().countryNameByISO2(
|
||||
_information.shippingAddress.countryIso2));
|
||||
push(_information.shippingAddress.postcode);
|
||||
add(
|
||||
|
@ -174,7 +174,7 @@ CountrySelectBox::Inner::Inner(
|
||||
, _rowHeight(st::countryRowHeight) {
|
||||
setAttribute(Qt::WA_OpaquePaintEvent);
|
||||
|
||||
const auto &byISO2 = Countries::InfoByISO2();
|
||||
const auto &byISO2 = Countries::Instance().byISO2();
|
||||
|
||||
if (byISO2.contains(iso)) {
|
||||
LastValidISO = iso;
|
||||
@ -188,7 +188,7 @@ CountrySelectBox::Inner::Inner(
|
||||
if (lastValid) {
|
||||
_list.emplace_back(lastValid);
|
||||
}
|
||||
for (const auto &entry : Countries::List()) {
|
||||
for (const auto &entry : Countries::Instance().list()) {
|
||||
if (&entry != lastValid) {
|
||||
_list.emplace_back(&entry);
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ void CountryInput::onChooseCode(const QString &code) {
|
||||
Ui::hideLayer();
|
||||
_chosenIso = QString();
|
||||
if (code.length()) {
|
||||
const auto &byCode = Countries::InfoByCode();
|
||||
const auto &byCode = Countries::Instance().byCode();
|
||||
const auto i = byCode.constFind(code);
|
||||
if (i != byCode.cend()) {
|
||||
const auto info = *i;
|
||||
@ -132,7 +132,7 @@ void CountryInput::onChooseCode(const QString &code) {
|
||||
bool CountryInput::chooseCountry(const QString &iso) {
|
||||
Ui::hideLayer();
|
||||
|
||||
const auto &byISO2 = Countries::InfoByISO2();
|
||||
const auto &byISO2 = Countries::Instance().byISO2();
|
||||
const auto i = byISO2.constFind(iso);
|
||||
const auto info = (i != byISO2.cend()) ? (*i) : nullptr;
|
||||
|
||||
|
@ -83,7 +83,7 @@ void CountryCodeInput::correctValue(
|
||||
}
|
||||
}
|
||||
if (!addToNumber.isEmpty()) {
|
||||
auto validCode = Countries::ValidPhoneCode(newText.mid(1));
|
||||
auto validCode = Countries::Instance().validPhoneCode(newText.mid(1));
|
||||
addToNumber = newText.mid(1 + validCode.length()) + addToNumber;
|
||||
newText = '+' + validCode;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user