From 77d1d9bd3a3223d84ea06305a1ac7b36cbf25859 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 28 Nov 2021 11:25:25 +0300 Subject: [PATCH] Moved out getting of phone pattern groups from PhoneInput fields. --- .../SourceFiles/boxes/add_contact_box.cpp | 3 ++- .../SourceFiles/boxes/change_phone_box.cpp | 3 ++- .../countries/countries_instance.cpp | 8 ++++++ .../countries/countries_instance.h | 1 + Telegram/SourceFiles/intro/intro_phone.cpp | 6 ++++- Telegram/SourceFiles/intro/intro_qr.h | 7 ----- .../passport/passport_panel_edit_contact.cpp | 3 ++- .../payments/ui/payments_field.cpp | 3 ++- Telegram/SourceFiles/ui/special_fields.cpp | 26 +++++++++---------- Telegram/SourceFiles/ui/special_fields.h | 15 +++++++++-- 10 files changed, 47 insertions(+), 28 deletions(-) diff --git a/Telegram/SourceFiles/boxes/add_contact_box.cpp b/Telegram/SourceFiles/boxes/add_contact_box.cpp index d0b63284f..43fa0abb4 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.cpp +++ b/Telegram/SourceFiles/boxes/add_contact_box.cpp @@ -261,7 +261,8 @@ AddContactBox::AddContactBox( st::defaultInputField, tr::lng_contact_phone(), Countries::ExtractPhoneCode(session->user()->phone()), - phone) + phone, + [](const QString &s) { return Countries::Groups(s); }) , _invertOrder(langFirstNameGoesSecond()) { if (!phone.isEmpty()) { _phone->setDisabled(true); diff --git a/Telegram/SourceFiles/boxes/change_phone_box.cpp b/Telegram/SourceFiles/boxes/change_phone_box.cpp index dc8194cbe..2a589ddde 100644 --- a/Telegram/SourceFiles/boxes/change_phone_box.cpp +++ b/Telegram/SourceFiles/boxes/change_phone_box.cpp @@ -156,7 +156,8 @@ void ChangePhoneBox::EnterPhone::prepare() { st::defaultInputField, tr::lng_change_phone_new_title(), Countries::ExtractPhoneCode(_controller->session().user()->phone()), - phoneValue); + phoneValue, + [](const QString &s) { return Countries::Groups(s); }); _phone->resize( st::boxWidth - 2 * st::boxPadding.left(), diff --git a/Telegram/SourceFiles/countries/countries_instance.cpp b/Telegram/SourceFiles/countries/countries_instance.cpp index 8e6aaf785..88c42c213 100644 --- a/Telegram/SourceFiles/countries/countries_instance.cpp +++ b/Telegram/SourceFiles/countries/countries_instance.cpp @@ -470,4 +470,12 @@ QString ExtractPhoneCode(const QString &phone) { return Instance().format({ .phone = phone, .onlyCode = true }).code; } +QVector Groups(const QString &phone) { + return Instance().format({ + .phone = phone, + .onlyGroups = true, + .incomplete = true, + }).groups; +} + } // namespace Countries diff --git a/Telegram/SourceFiles/countries/countries_instance.h b/Telegram/SourceFiles/countries/countries_instance.h index 6052377b2..d8d7562e8 100644 --- a/Telegram/SourceFiles/countries/countries_instance.h +++ b/Telegram/SourceFiles/countries/countries_instance.h @@ -70,5 +70,6 @@ private: CountriesInstance &Instance(); [[nodiscard]] QString ExtractPhoneCode(const QString &phone); +[[nodiscard]] QVector Groups(const QString &phone); } // namespace Countries diff --git a/Telegram/SourceFiles/intro/intro_phone.cpp b/Telegram/SourceFiles/intro/intro_phone.cpp index 8237f2e9b..adb9c55a7 100644 --- a/Telegram/SourceFiles/intro/intro_phone.cpp +++ b/Telegram/SourceFiles/intro/intro_phone.cpp @@ -23,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/boxes/confirm_box.h" #include "boxes/phone_banned_box.h" #include "core/application.h" +#include "countries/countries_instance.h" // Countries::Groups namespace Intro { namespace details { @@ -44,7 +45,10 @@ PhoneWidget::PhoneWidget( : Step(parent, account, data) , _country(this, st::introCountry) , _code(this, st::introCountryCode) -, _phone(this, st::introPhone) +, _phone( + this, + st::introPhone, + [](const QString &s) { return Countries::Groups(s); }) , _checkRequestTimer([=] { checkRequest(); }) { _phone->frontBackspaceEvent( ) | rpl::start_with_next([=](not_null e) { diff --git a/Telegram/SourceFiles/intro/intro_qr.h b/Telegram/SourceFiles/intro/intro_qr.h index 99d19a316..7cc166a4b 100644 --- a/Telegram/SourceFiles/intro/intro_qr.h +++ b/Telegram/SourceFiles/intro/intro_qr.h @@ -11,13 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "intro/intro_step.h" #include "base/timer.h" -namespace Ui { -class PhonePartInput; -class CountryCodeInput; -class RoundButton; -class FlatLabel; -} // namespace Ui - namespace Intro { namespace details { diff --git a/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp b/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp index c0505fe49..b9e3246b0 100644 --- a/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp @@ -281,7 +281,8 @@ void PanelEditContact::setupControls( std::move(fieldPlaceholder), Countries::ExtractPhoneCode( _controller->bot()->session().user()->phone()), - data); + data, + [](const QString &s) { return Countries::Groups(s); }); } else { _field = Ui::CreateChild( wrap.data(), diff --git a/Telegram/SourceFiles/payments/ui/payments_field.cpp b/Telegram/SourceFiles/payments/ui/payments_field.cpp index 522a7aa3b..6c90059d3 100644 --- a/Telegram/SourceFiles/payments/ui/payments_field.cpp +++ b/Telegram/SourceFiles/payments/ui/payments_field.cpp @@ -407,7 +407,8 @@ struct SimpleFieldState { st::paymentsField, std::move(config.placeholder), Countries::ExtractPhoneCode(config.defaultPhone), - Parse(config)); + Parse(config), + [](const QString &s) { return Countries::Groups(s); }); case FieldType::Money: return CreateMoneyField( wrap, diff --git a/Telegram/SourceFiles/ui/special_fields.cpp b/Telegram/SourceFiles/ui/special_fields.cpp index 61cb4198a..8a741b5df 100644 --- a/Telegram/SourceFiles/ui/special_fields.cpp +++ b/Telegram/SourceFiles/ui/special_fields.cpp @@ -96,8 +96,12 @@ void CountryCodeInput::correctValue( } } -PhonePartInput::PhonePartInput(QWidget *parent, const style::InputField &st) -: MaskedInputField(parent, st/*, tr::lng_phone_ph(tr::now)*/) { +PhonePartInput::PhonePartInput( + QWidget *parent, + const style::InputField &st, + PhonePartInput::GroupsCallback groupsCallback) +: MaskedInputField(parent, st/*, tr::lng_phone_ph(tr::now)*/) +, _groupsCallback(std::move(groupsCallback)) { } void PhonePartInput::paintAdditionalPlaceholder(Painter &p) { @@ -208,11 +212,7 @@ void PhonePartInput::addedToNumber(const QString &added) { } void PhonePartInput::chooseCode(const QString &code) { - _pattern = Countries::Instance().format({ - .phone = code, - .onlyGroups = true, - .incomplete = true, - }).groups; + _pattern = _groupsCallback(code); if (!_pattern.isEmpty() && _pattern.at(0) == code.size()) { _pattern.pop_front(); } else { @@ -297,9 +297,11 @@ PhoneInput::PhoneInput( const style::InputField &st, rpl::producer placeholder, const QString &defaultValue, - QString value) + QString value, + PhoneInput::GroupsCallback groupsCallback) : MaskedInputField(parent, st, std::move(placeholder), value) -, _defaultValue(defaultValue) { +, _defaultValue(defaultValue) +, _groupsCallback(std::move(groupsCallback)) { if (value.isEmpty()) { clearText(); } else { @@ -344,11 +346,7 @@ void PhoneInput::correctValue( int &nowCursor) { auto digits = now; digits.replace(QRegularExpression("[^\\d]"), QString()); - _pattern = Countries::Instance().format({ - .phone = digits, - .onlyGroups = true, - .incomplete = true, - }).groups; + _pattern = _groupsCallback(digits); QString newPlaceholder; if (_pattern.isEmpty()) { diff --git a/Telegram/SourceFiles/ui/special_fields.h b/Telegram/SourceFiles/ui/special_fields.h index 39be5c162..78327d87e 100644 --- a/Telegram/SourceFiles/ui/special_fields.h +++ b/Telegram/SourceFiles/ui/special_fields.h @@ -42,7 +42,12 @@ private: class PhonePartInput : public MaskedInputField { public: - PhonePartInput(QWidget *parent, const style::InputField &st); + using GroupsCallback = Fn(const QString &)>; + + PhonePartInput( + QWidget *parent, + const style::InputField &st, + GroupsCallback groupsCallback); [[nodiscard]] auto frontBackspaceEvent() const -> rpl::producer> { @@ -66,6 +71,7 @@ private: QVector _pattern; QString _additionalPlaceholder; rpl::event_stream> _frontBackspaceEvent; + GroupsCallback _groupsCallback; }; @@ -95,12 +101,15 @@ private: class PhoneInput : public MaskedInputField { public: + using GroupsCallback = Fn(const QString &)>; + PhoneInput( QWidget *parent, const style::InputField &st, rpl::producer placeholder, const QString &defaultValue, - QString value); + QString value, + GroupsCallback groupsCallback); void clearText(); @@ -119,6 +128,8 @@ private: QVector _pattern; QString _additionalPlaceholder; + GroupsCallback _groupsCallback; + }; } // namespace Ui