From 86aaa9673db73c2d2a968cc00da560995bac8507 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 26 Aug 2021 18:15:49 +0300 Subject: [PATCH] Moved countries to singleton. --- .../countries/countries_instance.cpp | 61 +++++++++++-------- .../countries/countries_instance.h | 25 ++++++-- Telegram/SourceFiles/intro/intro_widget.cpp | 2 +- .../passport/passport_panel_controller.cpp | 5 +- .../passport/passport_panel_edit_document.cpp | 2 +- .../passport/ui/passport_details_row.cpp | 4 +- .../SourceFiles/payments/payments_form.cpp | 2 +- .../payments/ui/payments_field.cpp | 7 ++- .../payments/ui/payments_form_summary.cpp | 2 +- .../ui/boxes/country_select_box.cpp | 4 +- Telegram/SourceFiles/ui/countryinput.cpp | 4 +- Telegram/SourceFiles/ui/special_fields.cpp | 2 +- 12 files changed, 71 insertions(+), 49 deletions(-) diff --git a/Telegram/SourceFiles/countries/countries_instance.cpp b/Telegram/SourceFiles/countries/countries_instance.cpp index c8f4bc787..44648a26e 100644 --- a/Telegram/SourceFiles/countries/countries_instance.cpp +++ b/Telegram/SourceFiles/countries/countries_instance.cpp @@ -10,6 +10,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Countries { namespace { +auto SingleInstance = CountriesInstance(); + const std::array FallbackList = { { { "Afghanistan", "AF", "93" }, { "Albania", "AL", "355" }, @@ -244,40 +246,37 @@ const std::array FallbackList = { { { "Zimbabwe", "ZW", "263" }, } }; -QHash ByCode; -QHash ByISO2; - } // namespace -const std::array &List() { +const std::array &CountriesInstance::list() { return FallbackList; } -const QHash &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 &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 diff --git a/Telegram/SourceFiles/countries/countries_instance.h b/Telegram/SourceFiles/countries/countries_instance.h index d572cb011..29304cd28 100644 --- a/Telegram/SourceFiles/countries/countries_instance.h +++ b/Telegram/SourceFiles/countries/countries_instance.h @@ -17,13 +17,26 @@ struct Info { const char *alternativeName = nullptr; }; -[[nodiscard]] const std::array &List(); +class CountriesInstance final { +public: + using Map = QHash; -[[nodiscard]] const QHash &InfoByCode(); -[[nodiscard]] const QHash &InfoByISO2(); + CountriesInstance() = default; + [[nodiscard]] const std::array &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 diff --git a/Telegram/SourceFiles/intro/intro_widget.cpp b/Telegram/SourceFiles/intro/intro_widget.cpp index cafd43e92..74ce8fc90 100644 --- a/Telegram/SourceFiles/intro/intro_widget.cpp +++ b/Telegram/SourceFiles/intro/intro_widget.cpp @@ -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; diff --git a/Telegram/SourceFiles/passport/passport_panel_controller.cpp b/Telegram/SourceFiles/passport/passport_panel_controller.cpp index bf320e6ab..65242b9f6 100644 --- a/Telegram/SourceFiles/passport/passport_panel_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_controller.cpp @@ -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, diff --git a/Telegram/SourceFiles/passport/passport_panel_edit_document.cpp b/Telegram/SourceFiles/passport/passport_panel_edit_document.cpp index cbc7ed861..ba82c0957 100644 --- a/Telegram/SourceFiles/passport/passport_panel_edit_document.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_edit_document.cpp @@ -520,7 +520,7 @@ void PanelEditDocument::createDetailsRow( object_ptr 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( diff --git a/Telegram/SourceFiles/passport/ui/passport_details_row.cpp b/Telegram/SourceFiles/passport/ui/passport_details_row.cpp index f6cbc5568..a7499b686 100644 --- a/Telegram/SourceFiles/passport/ui/passport_details_row.cpp +++ b/Telegram/SourceFiles/passport/ui/passport_details_row.cpp @@ -304,7 +304,7 @@ void AbstractTextRow::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() diff --git a/Telegram/SourceFiles/payments/payments_form.cpp b/Telegram/SourceFiles/payments/payments_form.cpp index 1e47dac46..b13417fc9 100644 --- a/Telegram/SourceFiles/payments/payments_form.cpp +++ b/Telegram/SourceFiles/payments/payments_form.cpp @@ -465,7 +465,7 @@ QString Form::defaultPhone() const { } QString Form::defaultCountry() const { - return Countries::CountryISO2ByPhone(defaultPhone()); + return Countries::Instance().countryISO2ByPhone(defaultPhone()); } void Form::fillPaymentMethodInformation() { diff --git a/Telegram/SourceFiles/payments/ui/payments_field.cpp b/Telegram/SourceFiles/payments/ui/payments_field.cpp index 172904436..db8fe64cb 100644 --- a/Telegram/SourceFiles/payments/ui/payments_field.cpp +++ b/Telegram/SourceFiles/payments/ui/payments_field.cpp @@ -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()) { diff --git a/Telegram/SourceFiles/payments/ui/payments_form_summary.cpp b/Telegram/SourceFiles/payments/ui/payments_form_summary.cpp index 270e50970..310a3b08d 100644 --- a/Telegram/SourceFiles/payments/ui/payments_form_summary.cpp +++ b/Telegram/SourceFiles/payments/ui/payments_form_summary.cpp @@ -508,7 +508,7 @@ void FormSummary::setupSections(not_null 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( diff --git a/Telegram/SourceFiles/ui/boxes/country_select_box.cpp b/Telegram/SourceFiles/ui/boxes/country_select_box.cpp index de57eea15..eb1b9d4a8 100644 --- a/Telegram/SourceFiles/ui/boxes/country_select_box.cpp +++ b/Telegram/SourceFiles/ui/boxes/country_select_box.cpp @@ -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); } diff --git a/Telegram/SourceFiles/ui/countryinput.cpp b/Telegram/SourceFiles/ui/countryinput.cpp index 0affb62d7..7e3f0d8bc 100644 --- a/Telegram/SourceFiles/ui/countryinput.cpp +++ b/Telegram/SourceFiles/ui/countryinput.cpp @@ -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; diff --git a/Telegram/SourceFiles/ui/special_fields.cpp b/Telegram/SourceFiles/ui/special_fields.cpp index a127d8252..ba474855c 100644 --- a/Telegram/SourceFiles/ui/special_fields.cpp +++ b/Telegram/SourceFiles/ui/special_fields.cpp @@ -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; }