2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Moved SentCodeField to td_ui.

This commit is contained in:
23rd
2021-10-18 18:48:46 +03:00
parent 94d5d20281
commit c15ba7d23a
13 changed files with 267 additions and 210 deletions

View File

@@ -129,11 +129,11 @@ private:
QString _hash;
int _codeLength = 0;
int _callTimeout = 0;
object_ptr<SentCodeField> _code = { nullptr };
object_ptr<Ui::SentCodeField> _code = { nullptr };
object_ptr<Ui::FadeWrap<Ui::FlatLabel>> _error = { nullptr };
object_ptr<Ui::FlatLabel> _callLabel = { nullptr };
mtpRequestId _requestId = 0;
SentCodeCall _call;
Ui::SentCodeCall _call;
};
@@ -289,7 +289,7 @@ void ChangePhoneBox::EnterCode::prepare() {
setDimensions(st::boxWidth, countHeight());
if (_callTimeout > 0) {
_call.setStatus({ SentCodeCall::State::Waiting, _callTimeout });
_call.setStatus({ Ui::SentCodeCall::State::Waiting, _callTimeout });
updateCall();
}

View File

@@ -70,131 +70,6 @@ void ShowPhoneBannedError(const QString &phone) {
[=] { SendToBannedHelp(phone); close(); }));
}
SentCodeField::SentCodeField(
QWidget *parent,
const style::InputField &st,
rpl::producer<QString> placeholder,
const QString &val)
: Ui::InputField(parent, st, std::move(placeholder), 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();
const auto now = getLastText();
auto oldPos = textCursor().position();
auto newPos = -1;
auto oldLen = now.size();
auto digitCount = 0;
for (const auto &ch : now) {
if (ch.isDigit()) {
++digitCount;
}
}
if (_autoSubmitLength > 0 && digitCount > _autoSubmitLength) {
digitCount = _autoSubmitLength;
}
auto strict = (_autoSubmitLength > 0)
&& (digitCount == _autoSubmitLength);
newText.reserve(oldLen);
int i = 0;
for (const auto &ch : now) {
if (i++ == oldPos) {
newPos = newText.length();
}
if (ch.isDigit()) {
if (!digitCount--) {
break;
}
newText += ch;
if (strict && !digitCount) {
break;
}
} else if (ch == '-') {
newText += ch;
}
}
if (newPos < 0) {
newPos = newText.length();
}
if (newText != now) {
setText(newText);
setCursorPosition(newPos);
}
_fixing = false;
if (_changedCallback) {
_changedCallback();
}
if (strict && _submitCallback) {
_submitCallback();
}
}
SentCodeCall::SentCodeCall(
FnMut<void()> callCallback,
Fn<void()> updateCallback)
: _call(std::move(callCallback))
, _update(std::move(updateCallback)) {
_timer.setCallback([=] {
if (_status.state == State::Waiting) {
if (--_status.timeout <= 0) {
_status.state = State::Calling;
_timer.cancel();
if (_call) {
_call();
}
}
}
if (_update) {
_update();
}
});
}
void SentCodeCall::setStatus(const Status &status) {
_status = status;
if (_status.state == State::Waiting) {
_timer.callEach(1000);
}
}
QString SentCodeCall::getText() const {
switch (_status.state) {
case State::Waiting: {
if (_status.timeout >= 3600) {
return tr::lng_code_call(tr::now, lt_minutes, qsl("%1:%2").arg(_status.timeout / 3600).arg((_status.timeout / 60) % 60, 2, 10, QChar('0')), lt_seconds, qsl("%1").arg(_status.timeout % 60, 2, 10, QChar('0')));
}
return tr::lng_code_call(tr::now, lt_minutes, QString::number(_status.timeout / 60), lt_seconds, qsl("%1").arg(_status.timeout % 60, 2, 10, QChar('0')));
} break;
case State::Calling: return tr::lng_code_calling(tr::now);
case State::Called: return tr::lng_code_called(tr::now);
}
return QString();
}
void ConfirmPhoneBox::Start(
not_null<Main::Session*> session,
const QString &phone,
@@ -262,7 +137,7 @@ void ConfirmPhoneBox::sendCodeDone(const MTPauth_SentCode &result) {
_phoneHash = qs(data.vphone_code_hash());
if (const auto nextType = data.vnext_type()) {
if (nextType->type() == mtpc_auth_codeTypeCall) {
_call.setStatus({ SentCodeCall::State::Waiting, data.vtimeout().value_or(60) });
_call.setStatus({ Ui::SentCodeCall::State::Waiting, data.vtimeout().value_or(60) });
}
}
launch();

View File

@@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/abstract_box.h"
#include "base/timer.h"
#include "ui/widgets/input_fields.h"
#include "ui/widgets/sent_code_field.h"
#include "mtproto/sender.h"
namespace Ui {
@@ -23,72 +24,6 @@ class Session;
void ShowPhoneBannedError(const QString &phone);
class SentCodeField : public Ui::InputField {
public:
SentCodeField(
QWidget *parent,
const style::InputField &st,
rpl::producer<QString> placeholder = nullptr,
const QString &val = QString());
void setAutoSubmit(int length, Fn<void()> submitCallback);
void setChangedCallback(Fn<void()> changedCallback);
QString getDigitsOnly() const;
private:
void fix();
// Flag for not calling onTextChanged() recursively.
bool _fixing = false;
int _autoSubmitLength = 0;
Fn<void()> _submitCallback;
Fn<void()> _changedCallback;
};
class SentCodeCall {
public:
SentCodeCall(
FnMut<void()> callCallback,
Fn<void()> updateCallback);
enum class State {
Waiting,
Calling,
Called,
Disabled,
};
struct Status {
Status() {
}
Status(State state, int timeout) : state(state), timeout(timeout) {
}
State state = State::Disabled;
int timeout = 0;
};
void setStatus(const Status &status);
void callDone() {
if (_status.state == State::Calling) {
_status.state = State::Called;
if (_update) {
_update();
}
}
}
QString getText() const;
private:
Status _status;
base::Timer _timer;
FnMut<void()> _call;
Fn<void()> _update;
};
class ConfirmPhoneBox final : public Ui::BoxContent {
public:
static void Start(
@@ -149,9 +84,9 @@ private:
mtpRequestId _checkCodeRequestId = 0;
object_ptr<Ui::FlatLabel> _about = { nullptr };
object_ptr<SentCodeField> _code = { nullptr };
object_ptr<Ui::SentCodeField> _code = { nullptr };
QString _error;
SentCodeCall _call;
Ui::SentCodeCall _call;
};

View File

@@ -10,7 +10,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/bytes.h"
#include "lang/lang_keys.h"
#include "boxes/confirm_box.h"
#include "boxes/confirm_phone_box.h"
#include "base/unixtime.h"
#include "mainwindow.h"
#include "apiwrap.h"
@@ -24,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/widgets/buttons.h"
#include "ui/widgets/input_fields.h"
#include "ui/widgets/labels.h"
#include "ui/widgets/sent_code_field.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/wrap/fade_wrap.h"
#include "passport/passport_encryption.h"