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/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()) {

View file

@ -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());

View file

@ -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

View file

@ -27,6 +27,7 @@ struct Info {
struct FormatResult {
QString formatted;
QVector<int> 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

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/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 {

View file

@ -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(

View file

@ -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,

View file

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