Allow '-' character in code inputs.

This commit is contained in:
John Preston 2018-11-19 16:15:59 +04:00
parent c522e047c6
commit 770f4a78aa
6 changed files with 72 additions and 34 deletions

View File

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

View File

@ -21,17 +21,43 @@ object_ptr<ConfirmPhoneBox> CurrentConfirmPhoneBox = { nullptr };
} // namespace
SentCodeField::SentCodeField(
QWidget *parent,
const style::InputField &st,
Fn<QString()> 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<void()> submitCallback) {
_autoSubmitLength = length;
_submitCallback = std::move(submitCallback);
}
void SentCodeField::setChangedCallback(Fn<void()> 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<void()> callCallback, Fn<void()> updateCallback)
SentCodeCall::SentCodeCall(
FnMut<void()> callCallback,
Fn<void()> 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) {

View File

@ -18,17 +18,15 @@ class FlatLabel;
class SentCodeField : public Ui::InputField {
public:
SentCodeField(QWidget *parent, const style::InputField &st, Fn<QString()> placeholderFactory = Fn<QString()>(), 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<QString()> placeholderFactory = nullptr,
const QString &val = QString());
void setAutoSubmit(int length, Fn<void()> submitCallback) {
_autoSubmitLength = length;
_submitCallback = std::move(submitCallback);
}
void setChangedCallback(Fn<void()> changedCallback) {
_changedCallback = std::move(changedCallback);
}
void setAutoSubmit(int length, Fn<void()> submitCallback);
void setChangedCallback(Fn<void()> changedCallback);
QString getDigitsOnly() const;
private:
void fix();

View File

@ -19,7 +19,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Intro {
CodeInput::CodeInput(QWidget *parent, const style::InputField &st, Fn<QString()> placeholderFactory) : Ui::MaskedInputField(parent, st, std::move(placeholderFactory)) {
CodeInput::CodeInput(
QWidget *parent,
const style::InputField &st,
Fn<QString()> 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();

View File

@ -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<QString()> placeholderFactory);
void setDigitsCountMax(int digitsCount);
signals:
void codeEntered();
protected:
void correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) override;

View File

@ -170,7 +170,7 @@ void VerifyBox::setupControls(
}, lifetime());
_submit = [=] {
submit(_code->getLastText());
submit(_code->getDigitsOnly());
};
if (codeLength > 0) {
_code->setAutoSubmit(codeLength, _submit);