2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-30 22:25:12 +00:00

Show current privacy values in settings section.

This commit is contained in:
John Preston
2018-09-11 12:11:52 +03:00
parent 165511fb14
commit 12ebae01b0
8 changed files with 371 additions and 197 deletions

View File

@@ -14,9 +14,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/wrap/slide_wrap.h"
#include "history/history.h"
#include "boxes/peer_list_controllers.h"
#include "base/binary_guard.h"
#include "lang/lang_keys.h"
#include "apiwrap.h"
#include "auth_session.h"
#include "lang/lang_keys.h"
namespace {
@@ -77,9 +78,19 @@ std::unique_ptr<PrivacyExceptionsBoxController::Row> PrivacyExceptionsBoxControl
} // namespace
EditPrivacyBox::EditPrivacyBox(QWidget*, std::unique_ptr<Controller> controller) : BoxContent()
, _controller(std::move(controller))
EditPrivacyBox::EditPrivacyBox(
QWidget*,
std::unique_ptr<Controller> controller,
rpl::producer<Value> preloaded)
: _controller(std::move(controller))
, _loading(this, lang(lng_contacts_loading), Ui::FlatLabel::InitType::Simple, st::membersAbout) {
std::move(
preloaded
) | rpl::take(
1
) | rpl::start_with_next([=](Value &&data) {
dataReady(std::move(data));
}, lifetime());
}
void EditPrivacyBox::prepare() {
@@ -88,7 +99,11 @@ void EditPrivacyBox::prepare() {
setTitle([this] { return _controller->title(); });
addButton(langFactory(lng_cancel), [this] { closeBox(); });
loadData();
if (_loading) {
_prepared = true;
} else {
createWidgets();
}
setDimensions(st::boxWideWidth, countDefaultHeight(st::boxWideWidth));
}
@@ -215,13 +230,13 @@ QVector<MTPInputPrivacyRule> EditPrivacyBox::collectResult() {
constexpr auto kMaxRules = 3; // allow users, disallow users, option
auto result = QVector<MTPInputPrivacyRule>();
result.reserve(kMaxRules);
if (showExceptionLink(Exception::Always) && !_alwaysUsers.empty()) {
result.push_back(MTP_inputPrivacyValueAllowUsers(MTP_vector<MTPInputUser>(collectInputUsers(_alwaysUsers))));
if (showExceptionLink(Exception::Always) && !_value.always.empty()) {
result.push_back(MTP_inputPrivacyValueAllowUsers(MTP_vector<MTPInputUser>(collectInputUsers(_value.always))));
}
if (showExceptionLink(Exception::Never) && !_neverUsers.empty()) {
result.push_back(MTP_inputPrivacyValueDisallowUsers(MTP_vector<MTPInputUser>(collectInputUsers(_neverUsers))));
if (showExceptionLink(Exception::Never) && !_value.never.empty()) {
result.push_back(MTP_inputPrivacyValueDisallowUsers(MTP_vector<MTPInputUser>(collectInputUsers(_value.never))));
}
switch (_option) {
switch (_value.option) {
case Option::Everyone: result.push_back(MTP_inputPrivacyValueAllowAll()); break;
case Option::Contacts: result.push_back(MTP_inputPrivacyValueAllowContacts()); break;
case Option::Nobody: result.push_back(MTP_inputPrivacyValueDisallowAll()); break;
@@ -236,8 +251,8 @@ style::margins EditPrivacyBox::exceptionLinkMargins() const {
std::vector<not_null<UserData*>> &EditPrivacyBox::exceptionUsers(Exception exception) {
switch (exception) {
case Exception::Always: return _alwaysUsers;
case Exception::Never: return _neverUsers;
case Exception::Always: return _value.always;
case Exception::Never: return _value.never;
}
Unexpected("Invalid exception value.");
}
@@ -252,18 +267,18 @@ object_ptr<Ui::SlideWrap<Ui::LinkButton>> &EditPrivacyBox::exceptionLink(Excepti
bool EditPrivacyBox::showExceptionLink(Exception exception) const {
switch (exception) {
case Exception::Always: return (_option == Option::Contacts) || (_option == Option::Nobody);
case Exception::Never: return (_option == Option::Everyone) || (_option == Option::Contacts);
case Exception::Always: return (_value.option == Option::Contacts) || (_value.option == Option::Nobody);
case Exception::Never: return (_value.option == Option::Everyone) || (_value.option == Option::Contacts);
}
Unexpected("Invalid exception value.");
}
void EditPrivacyBox::createWidgets() {
_loading.destroy();
_optionGroup = std::make_shared<Ui::RadioenumGroup<Option>>(_option);
_optionGroup = std::make_shared<Ui::RadioenumGroup<Option>>(_value.option);
auto createOption = [this](object_ptr<Ui::Radioenum<Option>> &widget, Option option, const QString &label) {
if (_controller->hasOption(option) || (_option == option)) {
if (_controller->hasOption(option) || (_value.option == option)) {
widget.create(this, _optionGroup, option, label, st::defaultBoxCheckbox);
}
};
@@ -294,16 +309,18 @@ void EditPrivacyBox::createWidgets() {
clearButtons();
addButton(langFactory(lng_settings_save), [this] {
auto someAreDisallowed = (_option != Option::Everyone) || !_neverUsers.empty();
auto someAreDisallowed = (_value.option != Option::Everyone) || !_value.never.empty();
_controller->confirmSave(someAreDisallowed, crl::guard(this, [this] {
Auth().api().savePrivacy(_controller->key(), collectResult());
Auth().api().savePrivacy(
_controller->apiKey(),
collectResult());
closeBox();
}));
});
addButton(langFactory(lng_cancel), [this] { closeBox(); });
_optionGroup->setChangedCallback([this](Option value) {
_option = value;
_value.option = value;
_alwaysLink->toggle(
showExceptionLink(Exception::Always),
anim::type::normal);
@@ -323,54 +340,10 @@ void EditPrivacyBox::createWidgets() {
setDimensions(st::boxWideWidth, resizeGetHeight(st::boxWideWidth));
}
void EditPrivacyBox::loadData() {
request(MTPaccount_GetPrivacy(_controller->key())).done([this](const MTPaccount_PrivacyRules &result) {
Expects(result.type() == mtpc_account_privacyRules);
auto &rules = result.c_account_privacyRules();
App::feedUsers(rules.vusers);
// This is simplified version of privacy rules interpretation.
// But it should be fine for all the apps that use the same subset of features.
auto optionSet = false;
auto setOption = [this, &optionSet](Option option) {
if (optionSet) return;
optionSet = true;
_option = option;
};
auto feedRule = [this, &setOption](const MTPPrivacyRule &rule) {
switch (rule.type()) {
case mtpc_privacyValueAllowAll: setOption(Option::Everyone); break;
case mtpc_privacyValueAllowContacts: setOption(Option::Contacts); break;
case mtpc_privacyValueAllowUsers: {
auto &users = rule.c_privacyValueAllowUsers().vusers.v;
_alwaysUsers.reserve(_alwaysUsers.size() + users.size());
for (auto &userId : users) {
auto user = App::user(UserId(userId.v));
if (!base::contains(_neverUsers, user) && !base::contains(_alwaysUsers, user)) {
_alwaysUsers.push_back(user);
}
}
} break;
case mtpc_privacyValueDisallowContacts: // not supported, fall through
case mtpc_privacyValueDisallowAll: setOption(Option::Nobody); break;
case mtpc_privacyValueDisallowUsers: {
auto &users = rule.c_privacyValueDisallowUsers().vusers.v;
_neverUsers.reserve(_neverUsers.size() + users.size());
for (auto &userId : users) {
auto user = App::user(UserId(userId.v));
if (!base::contains(_alwaysUsers, user) && !base::contains(_neverUsers, user)) {
_neverUsers.push_back(user);
}
}
} break;
}
};
for (auto &rule : rules.vrules.v) {
feedRule(rule);
}
feedRule(MTP_privacyValueDisallowAll()); // disallow by default.
void EditPrivacyBox::dataReady(Value &&value) {
_value = std::move(value);
_loading.destroy();
if (_prepared) {
createWidgets();
}).send();
}
}

View File

@@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/abstract_box.h"
#include "mtproto/sender.h"
#include "apiwrap.h"
namespace Ui {
class FlatLabel;
@@ -23,11 +24,8 @@ class SlideWrap;
class EditPrivacyBox : public BoxContent, private MTP::Sender {
public:
enum class Option {
Everyone,
Contacts,
Nobody,
};
using Value = ApiWrap::Privacy;
using Option = Value::Option;
enum class Exception {
Always,
Never,
@@ -35,7 +33,10 @@ public:
class Controller {
public:
virtual MTPInputPrivacyKey key() = 0;
using Key = ApiWrap::Privacy::Key;
virtual Key key() = 0;
virtual MTPInputPrivacyKey apiKey() = 0;
virtual QString title() = 0;
virtual bool hasOption(Option option) {
@@ -71,7 +72,10 @@ public:
};
EditPrivacyBox(QWidget*, std::unique_ptr<Controller> controller);
EditPrivacyBox(
QWidget*,
std::unique_ptr<Controller> controller,
rpl::producer<Value> preloaded);
protected:
void prepare() override;
@@ -84,16 +88,18 @@ private:
bool showExceptionLink(Exception exception) const;
void createWidgets();
QVector<MTPInputPrivacyRule> collectResult();
void loadData();
void dataReady(Value &&value);
int countDefaultHeight(int newWidth);
void editExceptionUsers(Exception exception);
QString exceptionLinkText(Exception exception);
std::vector<not_null<UserData*>> &exceptionUsers(Exception exception);
object_ptr<Ui::SlideWrap<Ui::LinkButton>> &exceptionLink(Exception exception);
object_ptr<Ui::SlideWrap<Ui::LinkButton>> &exceptionLink(
Exception exception);
std::unique_ptr<Controller> _controller;
Option _option = Option::Everyone;
Value _value;
bool _prepared = false;
std::shared_ptr<Ui::RadioenumGroup<Option>> _optionGroup;
object_ptr<Ui::FlatLabel> _loading;
@@ -107,7 +113,4 @@ private:
object_ptr<Ui::SlideWrap<Ui::LinkButton>> _neverLink = { nullptr };
object_ptr<Ui::FlatLabel> _exceptionsDescription = { nullptr };
std::vector<not_null<UserData*>> _alwaysUsers;
std::vector<not_null<UserData*>> _neverUsers;
};