Improve self-destruct setting edit.

This commit is contained in:
John Preston 2018-09-17 20:31:01 +03:00
parent 130aa67ba0
commit fe9f02e485
6 changed files with 162 additions and 51 deletions

View File

@ -519,7 +519,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_settings_groups_invite_privacy" = "Group invite settings"; "lng_settings_groups_invite_privacy" = "Group invite settings";
"lng_settings_show_sessions" = "Show all sessions"; "lng_settings_show_sessions" = "Show all sessions";
"lng_settings_export_data" = "Export Telegram data"; "lng_settings_export_data" = "Export Telegram data";
"lng_settings_self_destruct" = "Account self-destruct settings"; "lng_settings_destroy_if" = "If away for...";
"lng_settings_change_phone" = "Change phone number"; "lng_settings_change_phone" = "Change phone number";
"lng_settings_reset" = "Terminate all other sessions"; "lng_settings_reset" = "Terminate all other sessions";

View File

@ -5039,6 +5039,7 @@ void ApiWrap::reloadPrivacy(Privacy::Key key) {
}).fail([=](const RPCError &error) { }).fail([=](const RPCError &error) {
_privacyRequestIds.erase(key); _privacyRequestIds.erase(key);
}).send(); }).send();
_privacyRequestIds.emplace(key, requestId);
} }
auto ApiWrap::parsePrivacy(const QVector<MTPPrivacyRule> &rules) auto ApiWrap::parsePrivacy(const QVector<MTPPrivacyRule> &rules)
@ -5113,6 +5114,44 @@ auto ApiWrap::privacyValue(Privacy::Key key) -> rpl::producer<Privacy> {
} }
} }
void ApiWrap::reloadSelfDestruct() {
if (_selfDestructRequestId) {
return;
}
_selfDestructRequestId = request(MTPaccount_GetAccountTTL(
)).done([=](const MTPAccountDaysTTL &result) {
_selfDestructRequestId = 0;
result.match([&](const MTPDaccountDaysTTL &data) {
setSelfDestructDays(data.vdays.v);
});
}).fail([=](const RPCError &error) {
_selfDestructRequestId = 0;
}).send();
}
rpl::producer<int> ApiWrap::selfDestructValue() const {
return _selfDestructDays
? _selfDestructChanges.events_starting_with_copy(*_selfDestructDays)
: (_selfDestructChanges.events() | rpl::type_erased());
}
void ApiWrap::saveSelfDestruct(int days) {
request(_selfDestructRequestId).cancel();
_selfDestructRequestId = request(MTPaccount_SetAccountTTL(
MTP_accountDaysTTL(MTP_int(days))
)).done([=](const MTPBool &result) {
_selfDestructRequestId = 0;
}).fail([=](const RPCError &result) {
_selfDestructRequestId = 0;
}).send();
setSelfDestructDays(days);
}
void ApiWrap::setSelfDestructDays(int days) {
_selfDestructDays = days;
_selfDestructChanges.fire_copy(days);
}
void ApiWrap::readServerHistory(not_null<History*> history) { void ApiWrap::readServerHistory(not_null<History*> history) {
if (history->unreadCount()) { if (history->unreadCount()) {
readServerHistoryForce(history); readServerHistoryForce(history);

View File

@ -360,6 +360,10 @@ public:
void reloadPrivacy(Privacy::Key key); void reloadPrivacy(Privacy::Key key);
rpl::producer<Privacy> privacyValue(Privacy::Key key); rpl::producer<Privacy> privacyValue(Privacy::Key key);
void reloadSelfDestruct();
rpl::producer<int> selfDestructValue() const;
void saveSelfDestruct(int days);
~ApiWrap(); ~ApiWrap();
private: private:
@ -549,6 +553,8 @@ private:
const QVector<MTPPrivacyRule> &rules); const QVector<MTPPrivacyRule> &rules);
void updatePrivacyLastSeens(const QVector<MTPPrivacyRule> &rules); void updatePrivacyLastSeens(const QVector<MTPPrivacyRule> &rules);
void setSelfDestructDays(int days);
not_null<AuthSession*> _session; not_null<AuthSession*> _session;
MessageDataRequests _messageDataRequests; MessageDataRequests _messageDataRequests;
@ -710,4 +716,8 @@ private:
base::flat_map<Privacy::Key, Privacy> _privacyValues; base::flat_map<Privacy::Key, Privacy> _privacyValues;
std::map<Privacy::Key, rpl::event_stream<Privacy>> _privacyChanges; std::map<Privacy::Key, rpl::event_stream<Privacy>> _privacyChanges;
mtpRequestId _selfDestructRequestId = 0;
base::optional<int> _selfDestructDays;
rpl::event_stream<int> _selfDestructChanges;
}; };

View File

@ -10,59 +10,109 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "ui/widgets/checkbox.h" #include "ui/widgets/checkbox.h"
#include "ui/widgets/labels.h" #include "ui/widgets/labels.h"
#include "apiwrap.h"
#include "auth_session.h"
#include "styles/style_boxes.h" #include "styles/style_boxes.h"
SelfDestructionBox::SelfDestructionBox(
QWidget*,
rpl::producer<int> preloaded)
: _ttlValues{ 30, 90, 180, 365 }
, _loading(
this,
lang(lng_contacts_loading),
Ui::FlatLabel::InitType::Simple,
st::membersAbout) {
std::move(
preloaded
) | rpl::take(
1
) | rpl::start_with_next([=](int days) {
gotCurrent(days);
}, lifetime());
}
void SelfDestructionBox::gotCurrent(int days) {
Expects(!_ttlValues.empty());
_loading.destroy();
auto daysAdjusted = _ttlValues[0];
for (const auto value : _ttlValues) {
if (qAbs(days - value) < qAbs(days - daysAdjusted)) {
daysAdjusted = value;
}
}
_ttlGroup = std::make_shared<Ui::RadiobuttonGroup>(daysAdjusted);
if (_prepared) {
showContent();
}
}
void SelfDestructionBox::showContent() {
auto y = st::boxOptionListPadding.top();
_description.create(
this,
lang(lng_self_destruct_description),
Ui::FlatLabel::InitType::Simple,
st::boxLabel);
_description->moveToLeft(st::boxPadding.left(), y);
y += _description->height() + st::boxMediumSkip;
const auto count = int(_ttlValues.size());
for (const auto value : _ttlValues) {
const auto button = Ui::CreateChild<Ui::Radiobutton>(
this,
_ttlGroup,
value,
DaysLabel(value),
st::langsButton);
button->moveToLeft(st::boxPadding.left(), y);
y += button->heightNoMargins() + st::boxOptionListSkip;
}
showChildren();
clearButtons();
addButton(langFactory(lng_settings_save), [=] {
Auth().api().saveSelfDestruct(_ttlGroup->value());
closeBox();
});
addButton(langFactory(lng_cancel), [=] { closeBox(); });
}
QString SelfDestructionBox::DaysLabel(int days) {
return (days > 364)
? lng_self_destruct_years(lt_count, days / 365)
: lng_self_destruct_months(lt_count, qMax(days / 30, 1));
}
void SelfDestructionBox::prepare() { void SelfDestructionBox::prepare() {
setTitle(langFactory(lng_self_destruct_title)); setTitle(langFactory(lng_self_destruct_title));
_ttlValues = { 30, 90, 180, 365 }; auto fake = object_ptr<Ui::FlatLabel>(
this,
auto fake = object_ptr<Ui::FlatLabel>(this, lang(lng_self_destruct_description), Ui::FlatLabel::InitType::Simple, st::boxLabel); lang(lng_self_destruct_description),
auto boxHeight = st::boxOptionListPadding.top() Ui::FlatLabel::InitType::Simple,
st::boxLabel);
const auto boxHeight = st::boxOptionListPadding.top()
+ fake->height() + st::boxMediumSkip + fake->height() + st::boxMediumSkip
+ _ttlValues.size() * (st::defaultRadio.diameter + st::boxOptionListSkip) - st::boxOptionListSkip + (_ttlValues.size()
* (st::defaultRadio.diameter + st::boxOptionListSkip))
- st::boxOptionListSkip
+ st::boxOptionListPadding.bottom() + st::boxPadding.bottom(); + st::boxOptionListPadding.bottom() + st::boxPadding.bottom();
fake.destroy(); fake.destroy();
setDimensions(st::boxWidth, boxHeight); setDimensions(st::boxWidth, boxHeight);
auto loading = object_ptr<Ui::FlatLabel>(this, lang(lng_contacts_loading), Ui::FlatLabel::InitType::Simple, st::membersAbout);
loading->moveToLeft((st::boxWidth - loading->width()) / 2, boxHeight / 3);
addButton(langFactory(lng_cancel), [this] { closeBox(); }); addButton(langFactory(lng_cancel), [this] { closeBox(); });
request(MTPaccount_GetAccountTTL()).done([this, loading = std::move(loading)](const MTPAccountDaysTTL &result) mutable { if (_loading) {
Expects(result.type() == mtpc_accountDaysTTL); _loading->moveToLeft(
Expects(!_ttlValues.empty()); (st::boxWidth - _loading->width()) / 2,
boxHeight / 3);
loading.destroy(); _prepared = true;
auto y = st::boxOptionListPadding.top(); } else {
_description.create(this, lang(lng_self_destruct_description), Ui::FlatLabel::InitType::Simple, st::boxLabel); showContent();
_description->moveToLeft(st::boxPadding.left(), y); }
y += _description->height() + st::boxMediumSkip;
auto current = result.c_accountDaysTTL().vdays.v;
auto currentAdjusted = _ttlValues[0];
for (auto days : _ttlValues) {
if (qAbs(current - days) < qAbs(current - currentAdjusted)) {
currentAdjusted = days;
}
}
auto group = std::make_shared<Ui::RadiobuttonGroup>(currentAdjusted);
auto count = int(_ttlValues.size());
_options.reserve(count);
for (auto days : _ttlValues) {
_options.emplace_back(this, group, days, (days > 364) ? lng_self_destruct_years(lt_count, days / 365) : lng_self_destruct_months(lt_count, qMax(days / 30, 1)), st::langsButton);
_options.back()->moveToLeft(st::boxPadding.left(), y);
y += _options.back()->heightNoMargins() + st::boxOptionListSkip;
}
showChildren();
clearButtons();
addButton(langFactory(lng_settings_save), [this, group] {
MTP::send(MTPaccount_SetAccountTTL(MTP_accountDaysTTL(MTP_int(group->value()))));
closeBox();
});
addButton(langFactory(lng_cancel), [this] { closeBox(); });
}).send();
} }

View File

@ -17,19 +17,22 @@ class FlatLabel;
} // namespace Ui } // namespace Ui
class SelfDestructionBox : public BoxContent, private MTP::Sender { class SelfDestructionBox : public BoxContent, private MTP::Sender {
Q_OBJECT
public: public:
SelfDestructionBox(QWidget*) { SelfDestructionBox(QWidget*, rpl::producer<int> preloaded);
}
static QString DaysLabel(int days);
protected: protected:
void prepare() override; void prepare() override;
private: private:
void gotCurrent(int days);
void showContent();
bool _prepared = false;
std::vector<int> _ttlValues; std::vector<int> _ttlValues;
object_ptr<Ui::FlatLabel> _description = { nullptr }; object_ptr<Ui::FlatLabel> _description = { nullptr };
object_ptr<Ui::FlatLabel> _loading;
std::shared_ptr<Ui::RadiobuttonGroup> _ttlGroup; std::shared_ptr<Ui::RadiobuttonGroup> _ttlGroup;
std::vector<object_ptr<Ui::Radiobutton>> _options;
}; };

View File

@ -380,12 +380,21 @@ void SetupSelfDestruction(not_null<Ui::VerticalLayout*> container) {
AddSkip(container); AddSkip(container);
AddSubsectionTitle(container, lng_settings_destroy_title); AddSubsectionTitle(container, lng_settings_destroy_title);
AddButton( Auth().api().reloadSelfDestruct();
const auto label = [] {
return Auth().api().selfDestructValue(
) | rpl::map(
SelfDestructionBox::DaysLabel
);
};
AddButtonWithLabel(
container, container,
lng_settings_self_destruct, lng_settings_destroy_if,
label(),
st::settingsButton st::settingsButton
)->addClickHandler([] { )->addClickHandler([] {
Ui::show(Box<SelfDestructionBox>()); Ui::show(Box<SelfDestructionBox>(Auth().api().selfDestructValue()));
}); });
AddSkip(container); AddSkip(container);