Simplified extracting of calling code from phone number.

This commit is contained in:
23rd 2021-08-29 22:15:35 +03:00 committed by John Preston
parent 44c188024e
commit 558e1d96fd
8 changed files with 18 additions and 19 deletions

View file

@ -12,7 +12,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/flat_set.h" #include "base/flat_set.h"
#include "base/openssl_help.h" #include "base/openssl_help.h"
#include "boxes/confirm_box.h" #include "boxes/confirm_box.h"
#include "boxes/confirm_phone_box.h" // ExtractPhonePrefix.
#include "boxes/peer_list_controllers.h" #include "boxes/peer_list_controllers.h"
#include "boxes/peers/add_participants_box.h" #include "boxes/peers/add_participants_box.h"
#include "boxes/peers/edit_participant_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/file_utilities.h"
#include "core/application.h" #include "core/application.h"
#include "chat_helpers/emoji_suggestions_widget.h" #include "chat_helpers/emoji_suggestions_widget.h"
#include "countries/countries_instance.h" // Countries::ExtractPhoneCode.
#include "window/window_session_controller.h" #include "window/window_session_controller.h"
#include "ui/widgets/checkbox.h" #include "ui/widgets/checkbox.h"
#include "ui/widgets/buttons.h" #include "ui/widgets/buttons.h"
@ -267,7 +267,7 @@ AddContactBox::AddContactBox(
this, this,
st::defaultInputField, st::defaultInputField,
tr::lng_contact_phone(), tr::lng_contact_phone(),
Ui::ExtractPhonePrefix(session->user()->phone()), Countries::ExtractPhoneCode(session->user()->phone()),
phone) phone)
, _invertOrder(langFirstNameGoesSecond()) { , _invertOrder(langFirstNameGoesSecond()) {
if (!phone.isEmpty()) { if (!phone.isEmpty()) {

View file

@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/special_fields.h" #include "ui/special_fields.h"
#include "boxes/confirm_phone_box.h" #include "boxes/confirm_phone_box.h"
#include "boxes/confirm_box.h" #include "boxes/confirm_box.h"
#include "countries/countries_instance.h" // Countries::ExtractPhoneCode.
#include "main/main_session.h" #include "main/main_session.h"
#include "data/data_session.h" #include "data/data_session.h"
#include "data/data_user.h" #include "data/data_user.h"
@ -151,7 +152,7 @@ void ChangePhoneBox::EnterPhone::prepare() {
this, this,
st::defaultInputField, st::defaultInputField,
tr::lng_change_phone_new_title(), tr::lng_change_phone_new_title(),
Ui::ExtractPhonePrefix(_session->user()->phone()), Countries::ExtractPhoneCode(_session->user()->phone()),
phoneValue); phoneValue);
_phone->resize(st::boxWidth - 2 * st::boxPadding.left(), _phone->height()); _phone->resize(st::boxWidth - 2 * st::boxPadding.left(), _phone->height());

View file

@ -344,6 +344,9 @@ FormatResult CountriesInstance::format(FormatArgs args) {
if (bestCountryPtr == nullptr) { if (bestCountryPtr == nullptr) {
return FormatResult{ .formatted = phoneNumber }; return FormatResult{ .formatted = phoneNumber };
} }
if (args.onlyCode) {
return FormatResult{ .code = bestCallingCodePtr->callingCode };
}
const auto codeSize = int(bestCallingCodePtr->callingCode.size()); const auto codeSize = int(bestCallingCodePtr->callingCode.size());
@ -450,4 +453,8 @@ CountriesInstance &Instance() {
return SingleInstance; return SingleInstance;
} }
QString ExtractPhoneCode(const QString &phone) {
return Instance().format({ .phone = phone, .onlyCode = true }).code;
}
} // namespace Countries } // namespace Countries

View file

@ -27,6 +27,7 @@ struct Info {
struct FormatResult { struct FormatResult {
QString formatted; QString formatted;
QVector<int> groups; QVector<int> groups;
QString code;
}; };
struct FormatArgs { struct FormatArgs {
@ -34,6 +35,7 @@ struct FormatArgs {
bool onlyGroups = false; bool onlyGroups = false;
bool skipCode = false; bool skipCode = false;
bool incomplete = false; bool incomplete = false;
bool onlyCode = false;
}; };
class CountriesInstance final { class CountriesInstance final {
@ -63,4 +65,6 @@ private:
CountriesInstance &Instance(); CountriesInstance &Instance();
[[nodiscard]] QString ExtractPhoneCode(const QString &phone);
} // namespace Countries } // namespace Countries

View file

@ -21,8 +21,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/text/text_utilities.h" // Ui::Text::ToUpper #include "ui/text/text_utilities.h" // Ui::Text::ToUpper
#include "ui/special_fields.h" #include "ui/special_fields.h"
#include "boxes/abstract_box.h" #include "boxes/abstract_box.h"
#include "boxes/confirm_phone_box.h"
#include "data/data_user.h" #include "data/data_user.h"
#include "countries/countries_instance.h" // Countries::ExtractPhoneCode.
#include "main/main_session.h" #include "main/main_session.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "styles/style_passport.h" #include "styles/style_passport.h"
@ -278,7 +278,7 @@ void PanelEditContact::setupControls(
wrap.data(), wrap.data(),
fieldStyle, fieldStyle,
std::move(fieldPlaceholder), std::move(fieldPlaceholder),
Ui::ExtractPhonePrefix( Countries::ExtractPhoneCode(
_controller->bot()->session().user()->phone()), _controller->bot()->session().user()->phone()),
data); data);
} else { } else {

View file

@ -405,7 +405,7 @@ struct SimpleFieldState {
wrap.get(), wrap.get(),
st::paymentsField, st::paymentsField,
std::move(config.placeholder), std::move(config.placeholder),
ExtractPhonePrefix(config.defaultPhone), Countries::ExtractPhoneCode(config.defaultPhone),
Parse(config)); Parse(config));
case FieldType::Money: case FieldType::Money:
return CreateMoneyField( return CreateMoneyField(

View file

@ -288,17 +288,6 @@ void UsernameInput::correctValue(
setCorrectedText(now, nowCursor, now.mid(from, len), newPos); 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( PhoneInput::PhoneInput(
QWidget *parent, QWidget *parent,
const style::InputField &st, const style::InputField &st,

View file

@ -93,8 +93,6 @@ private:
}; };
[[nodiscard]] QString ExtractPhonePrefix(const QString &phone);
class PhoneInput : public MaskedInputField { class PhoneInput : public MaskedInputField {
public: public:
PhoneInput( PhoneInput(