From 558e1d96fd663abc87319baee2c858b480647127 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 29 Aug 2021 22:15:35 +0300 Subject: [PATCH] Simplified extracting of calling code from phone number. --- Telegram/SourceFiles/boxes/add_contact_box.cpp | 4 ++-- Telegram/SourceFiles/boxes/change_phone_box.cpp | 3 ++- Telegram/SourceFiles/countries/countries_instance.cpp | 7 +++++++ Telegram/SourceFiles/countries/countries_instance.h | 4 ++++ .../passport/passport_panel_edit_contact.cpp | 4 ++-- Telegram/SourceFiles/payments/ui/payments_field.cpp | 2 +- Telegram/SourceFiles/ui/special_fields.cpp | 11 ----------- Telegram/SourceFiles/ui/special_fields.h | 2 -- 8 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Telegram/SourceFiles/boxes/add_contact_box.cpp b/Telegram/SourceFiles/boxes/add_contact_box.cpp index 5b79312a2..eb7ad3ab9 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.cpp +++ b/Telegram/SourceFiles/boxes/add_contact_box.cpp @@ -12,7 +12,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/flat_set.h" #include "base/openssl_help.h" #include "boxes/confirm_box.h" -#include "boxes/confirm_phone_box.h" // ExtractPhonePrefix. #include "boxes/peer_list_controllers.h" #include "boxes/peers/add_participants_box.h" #include "boxes/peers/edit_participant_box.h" @@ -20,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/file_utilities.h" #include "core/application.h" #include "chat_helpers/emoji_suggestions_widget.h" +#include "countries/countries_instance.h" // Countries::ExtractPhoneCode. #include "window/window_session_controller.h" #include "ui/widgets/checkbox.h" #include "ui/widgets/buttons.h" @@ -267,7 +267,7 @@ AddContactBox::AddContactBox( this, st::defaultInputField, tr::lng_contact_phone(), - Ui::ExtractPhonePrefix(session->user()->phone()), + Countries::ExtractPhoneCode(session->user()->phone()), phone) , _invertOrder(langFirstNameGoesSecond()) { if (!phone.isEmpty()) { diff --git a/Telegram/SourceFiles/boxes/change_phone_box.cpp b/Telegram/SourceFiles/boxes/change_phone_box.cpp index 5de47051d..edf56e2ec 100644 --- a/Telegram/SourceFiles/boxes/change_phone_box.cpp +++ b/Telegram/SourceFiles/boxes/change_phone_box.cpp @@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/special_fields.h" #include "boxes/confirm_phone_box.h" #include "boxes/confirm_box.h" +#include "countries/countries_instance.h" // Countries::ExtractPhoneCode. #include "main/main_session.h" #include "data/data_session.h" #include "data/data_user.h" @@ -151,7 +152,7 @@ void ChangePhoneBox::EnterPhone::prepare() { this, st::defaultInputField, tr::lng_change_phone_new_title(), - Ui::ExtractPhonePrefix(_session->user()->phone()), + Countries::ExtractPhoneCode(_session->user()->phone()), phoneValue); _phone->resize(st::boxWidth - 2 * st::boxPadding.left(), _phone->height()); diff --git a/Telegram/SourceFiles/countries/countries_instance.cpp b/Telegram/SourceFiles/countries/countries_instance.cpp index aa771dc0f..f4a7df815 100644 --- a/Telegram/SourceFiles/countries/countries_instance.cpp +++ b/Telegram/SourceFiles/countries/countries_instance.cpp @@ -344,6 +344,9 @@ FormatResult CountriesInstance::format(FormatArgs args) { if (bestCountryPtr == nullptr) { return FormatResult{ .formatted = phoneNumber }; } + if (args.onlyCode) { + return FormatResult{ .code = bestCallingCodePtr->callingCode }; + } const auto codeSize = int(bestCallingCodePtr->callingCode.size()); @@ -450,4 +453,8 @@ CountriesInstance &Instance() { return SingleInstance; } +QString ExtractPhoneCode(const QString &phone) { + return Instance().format({ .phone = phone, .onlyCode = true }).code; +} + } // namespace Countries diff --git a/Telegram/SourceFiles/countries/countries_instance.h b/Telegram/SourceFiles/countries/countries_instance.h index 1b9ddec8c..b57b172fc 100644 --- a/Telegram/SourceFiles/countries/countries_instance.h +++ b/Telegram/SourceFiles/countries/countries_instance.h @@ -27,6 +27,7 @@ struct Info { struct FormatResult { QString formatted; QVector groups; + QString code; }; struct FormatArgs { @@ -34,6 +35,7 @@ struct FormatArgs { bool onlyGroups = false; bool skipCode = false; bool incomplete = false; + bool onlyCode = false; }; class CountriesInstance final { @@ -63,4 +65,6 @@ private: CountriesInstance &Instance(); +[[nodiscard]] QString ExtractPhoneCode(const QString &phone); + } // namespace Countries diff --git a/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp b/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp index 5275496d3..dd09661c3 100644 --- a/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp @@ -21,8 +21,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/text/text_utilities.h" // Ui::Text::ToUpper #include "ui/special_fields.h" #include "boxes/abstract_box.h" -#include "boxes/confirm_phone_box.h" #include "data/data_user.h" +#include "countries/countries_instance.h" // Countries::ExtractPhoneCode. #include "main/main_session.h" #include "lang/lang_keys.h" #include "styles/style_passport.h" @@ -278,7 +278,7 @@ void PanelEditContact::setupControls( wrap.data(), fieldStyle, std::move(fieldPlaceholder), - Ui::ExtractPhonePrefix( + Countries::ExtractPhoneCode( _controller->bot()->session().user()->phone()), data); } else { diff --git a/Telegram/SourceFiles/payments/ui/payments_field.cpp b/Telegram/SourceFiles/payments/ui/payments_field.cpp index db8fe64cb..2d95cbe3c 100644 --- a/Telegram/SourceFiles/payments/ui/payments_field.cpp +++ b/Telegram/SourceFiles/payments/ui/payments_field.cpp @@ -405,7 +405,7 @@ struct SimpleFieldState { wrap.get(), st::paymentsField, std::move(config.placeholder), - ExtractPhonePrefix(config.defaultPhone), + Countries::ExtractPhoneCode(config.defaultPhone), Parse(config)); case FieldType::Money: return CreateMoneyField( diff --git a/Telegram/SourceFiles/ui/special_fields.cpp b/Telegram/SourceFiles/ui/special_fields.cpp index 247a5f307..6681be1c9 100644 --- a/Telegram/SourceFiles/ui/special_fields.cpp +++ b/Telegram/SourceFiles/ui/special_fields.cpp @@ -288,17 +288,6 @@ void UsernameInput::correctValue( setCorrectedText(now, nowCursor, now.mid(from, len), newPos); } -QString ExtractPhonePrefix(const QString &phone) { - const auto pattern = Countries::Instance().format({ - .phone = phone, - .onlyGroups = true, - }).groups; - if (!pattern.isEmpty()) { - return phone.mid(0, pattern[0]); - } - return QString(); -} - PhoneInput::PhoneInput( QWidget *parent, const style::InputField &st, diff --git a/Telegram/SourceFiles/ui/special_fields.h b/Telegram/SourceFiles/ui/special_fields.h index fae5093a4..39be5c162 100644 --- a/Telegram/SourceFiles/ui/special_fields.h +++ b/Telegram/SourceFiles/ui/special_fields.h @@ -93,8 +93,6 @@ private: }; -[[nodiscard]] QString ExtractPhonePrefix(const QString &phone); - class PhoneInput : public MaskedInputField { public: PhoneInput(