diff --git a/Telegram/SourceFiles/boxes/change_phone_box.cpp b/Telegram/SourceFiles/boxes/change_phone_box.cpp index a68a1780c..c9ad47155 100644 --- a/Telegram/SourceFiles/boxes/change_phone_box.cpp +++ b/Telegram/SourceFiles/boxes/change_phone_box.cpp @@ -254,7 +254,7 @@ void ChangePhoneBox::EnterCode::submit() { } hideError(); - auto code = _code->getLastText().trimmed(); + const auto code = _code->getDigitsOnly(); _requestId = MTP::send(MTPaccount_ChangePhone( MTP_string(_phone), MTP_string(_hash), diff --git a/Telegram/SourceFiles/boxes/confirm_phone_box.cpp b/Telegram/SourceFiles/boxes/confirm_phone_box.cpp index a04a5dc25..2315bd05f 100644 --- a/Telegram/SourceFiles/boxes/confirm_phone_box.cpp +++ b/Telegram/SourceFiles/boxes/confirm_phone_box.cpp @@ -21,17 +21,43 @@ object_ptr CurrentConfirmPhoneBox = { nullptr }; } // namespace +SentCodeField::SentCodeField( + QWidget *parent, + const style::InputField &st, + Fn placeholderFactory, + const QString &val) +: Ui::InputField(parent, st, std::move(placeholderFactory), val) { + connect(this, &Ui::InputField::changed, [this] { fix(); }); +} + +void SentCodeField::setAutoSubmit(int length, Fn submitCallback) { + _autoSubmitLength = length; + _submitCallback = std::move(submitCallback); +} + +void SentCodeField::setChangedCallback(Fn changedCallback) { + _changedCallback = std::move(changedCallback); +} + +QString SentCodeField::getDigitsOnly() const { + return QString( + getLastText() + ).remove( + QRegularExpression("[^\\d]") + ); +} + void SentCodeField::fix() { if (_fixing) return; _fixing = true; auto newText = QString(); - auto now = getLastText(); + const auto now = getLastText(); auto oldPos = textCursor().position(); auto newPos = -1; auto oldLen = now.size(); auto digitCount = 0; - for_const (auto ch, now) { + for (const auto ch : now) { if (ch.isDigit()) { ++digitCount; } @@ -40,11 +66,12 @@ void SentCodeField::fix() { if (_autoSubmitLength > 0 && digitCount > _autoSubmitLength) { digitCount = _autoSubmitLength; } - auto strict = (_autoSubmitLength > 0 && digitCount == _autoSubmitLength); + auto strict = (_autoSubmitLength > 0) + && (digitCount == _autoSubmitLength); newText.reserve(oldLen); int i = 0; - for_const (auto ch, now) { + for (const auto ch : now) { if (i++ == oldPos) { newPos = newText.length(); } @@ -56,14 +83,15 @@ void SentCodeField::fix() { if (strict && !digitCount) { break; } + } else if (ch == '-') { + newText += ch; } } if (newPos < 0) { newPos = newText.length(); } if (newText != now) { - now = newText; - setText(now); + setText(newText); setCursorPosition(newPos); } _fixing = false; @@ -76,7 +104,9 @@ void SentCodeField::fix() { } } -SentCodeCall::SentCodeCall(FnMut callCallback, Fn updateCallback) +SentCodeCall::SentCodeCall( + FnMut callCallback, + Fn updateCallback) : _call(std::move(callCallback)) , _update(std::move(updateCallback)) { _timer.setCallback([=] { @@ -220,7 +250,7 @@ void ConfirmPhoneBox::sendCode() { if (_sendCodeRequestId) { return; } - auto code = _code->getLastText(); + const auto code = _code->getDigitsOnly(); if (code.isEmpty()) { _code->showError(); return; @@ -231,7 +261,10 @@ void ConfirmPhoneBox::sendCode() { showError(QString()); - _sendCodeRequestId = MTP::send(MTPaccount_ConfirmPhone(MTP_string(_phoneHash), MTP_string(_code->getLastText())), rpcDone(&ConfirmPhoneBox::confirmDone), rpcFail(&ConfirmPhoneBox::confirmFail)); + _sendCodeRequestId = MTP::send( + MTPaccount_ConfirmPhone(MTP_string(_phoneHash), MTP_string(code)), + rpcDone(&ConfirmPhoneBox::confirmDone), + rpcFail(&ConfirmPhoneBox::confirmFail)); } void ConfirmPhoneBox::confirmDone(const MTPBool &result) { diff --git a/Telegram/SourceFiles/boxes/confirm_phone_box.h b/Telegram/SourceFiles/boxes/confirm_phone_box.h index 36084bbf0..9eb443f45 100644 --- a/Telegram/SourceFiles/boxes/confirm_phone_box.h +++ b/Telegram/SourceFiles/boxes/confirm_phone_box.h @@ -18,17 +18,15 @@ class FlatLabel; class SentCodeField : public Ui::InputField { public: - SentCodeField(QWidget *parent, const style::InputField &st, Fn placeholderFactory = Fn(), const QString &val = QString()) : Ui::InputField(parent, st, std::move(placeholderFactory), val) { - connect(this, &Ui::InputField::changed, [this] { fix(); }); - } + SentCodeField( + QWidget *parent, + const style::InputField &st, + Fn placeholderFactory = nullptr, + const QString &val = QString()); - void setAutoSubmit(int length, Fn submitCallback) { - _autoSubmitLength = length; - _submitCallback = std::move(submitCallback); - } - void setChangedCallback(Fn changedCallback) { - _changedCallback = std::move(changedCallback); - } + void setAutoSubmit(int length, Fn submitCallback); + void setChangedCallback(Fn changedCallback); + QString getDigitsOnly() const; private: void fix(); diff --git a/Telegram/SourceFiles/intro/introcode.cpp b/Telegram/SourceFiles/intro/introcode.cpp index 979bc0819..e8c63f0d0 100644 --- a/Telegram/SourceFiles/intro/introcode.cpp +++ b/Telegram/SourceFiles/intro/introcode.cpp @@ -19,7 +19,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Intro { -CodeInput::CodeInput(QWidget *parent, const style::InputField &st, Fn placeholderFactory) : Ui::MaskedInputField(parent, st, std::move(placeholderFactory)) { +CodeInput::CodeInput( + QWidget *parent, + const style::InputField &st, + Fn placeholderFactory) +: Ui::MaskedInputField(parent, st, std::move(placeholderFactory)) { } void CodeInput::setDigitsCountMax(int digitsCount) { @@ -48,6 +52,8 @@ void CodeInput::correctValue(const QString &was, int wasCursor, QString &now, in if (strict && !digitCount) { break; } + } else if (ch == '-') { + newText += ch; } if (i == oldPos) { newPos = newText.length(); @@ -65,8 +71,6 @@ void CodeInput::correctValue(const QString &was, int wasCursor, QString &now, in nowCursor = newPos; setCursorPosition(nowCursor); } - - if (strict) emit codeEntered(); } CodeWidget::CodeWidget(QWidget *parent, Widget::Data *data) : Step(parent, data) @@ -255,9 +259,7 @@ bool CodeWidget::codeSubmitFail(const RPCError &error) { void CodeWidget::onInputChange() { hideError(); - if (_code->getLastText().length() == getData()->codeLength) { - submit(); - } + submit(); } void CodeWidget::onSendCall() { @@ -317,13 +319,23 @@ void CodeWidget::gotPassword(const MTPaccount_Password &result) { } void CodeWidget::submit() { - if (_sentRequest) return; + const auto text = QString( + _code->getLastText() + ).remove( + QRegularExpression("[^\\d]") + ).mid(0, getData()->codeLength); + + if (_sentRequest + || _sentCode == text + || text.size() != getData()->codeLength) { + return; + } hideError(); _checkRequest->start(1000); - _sentCode = _code->getLastText(); + _sentCode = text; getData()->pwdRequest = Core::CloudPasswordCheckRequest(); getData()->hasRecovery = false; getData()->pwdHint = QString(); diff --git a/Telegram/SourceFiles/intro/introcode.h b/Telegram/SourceFiles/intro/introcode.h index 27cd2b371..5df47b8cd 100644 --- a/Telegram/SourceFiles/intro/introcode.h +++ b/Telegram/SourceFiles/intro/introcode.h @@ -19,16 +19,11 @@ class FlatLabel; namespace Intro { class CodeInput final : public Ui::MaskedInputField { - Q_OBJECT - public: CodeInput(QWidget *parent, const style::InputField &st, Fn placeholderFactory); void setDigitsCountMax(int digitsCount); -signals: - void codeEntered(); - protected: void correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) override; diff --git a/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp b/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp index 3c9be95cb..d4b64b37e 100644 --- a/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_edit_contact.cpp @@ -170,7 +170,7 @@ void VerifyBox::setupControls( }, lifetime()); _submit = [=] { - submit(_code->getLastText()); + submit(_code->getDigitsOnly()); }; if (codeLength > 0) { _code->setAutoSubmit(codeLength, _submit);