Refactored code.

- Removed std::optional from EditPeerHistoryVisibilityBox.
 - Added std::optional for savedCallback in EditPeerTypeBox.
 - Guarded boxCallbacks.
This commit is contained in:
23rd 2019-03-20 19:45:21 +03:00 committed by John Preston
parent ecccf673a9
commit ca9db9fd3f
5 changed files with 37 additions and 55 deletions

View File

@ -51,7 +51,7 @@ void FillContent(
not_null<Ui::VerticalLayout*> parent,
not_null<PeerData*> peer,
std::shared_ptr<Ui::RadioenumGroup<HistoryVisibility>> historyVisibility,
std::optional<HistoryVisibility> savedValue = std::nullopt) {
HistoryVisibility savedValue) {
const auto canEdit = [&] {
if (const auto chat = peer->asChat()) {
return chat->canEditPreHistoryHidden();
@ -66,13 +66,7 @@ void FillContent(
const auto channel = peer->asChannel();
const auto defaultValue = savedValue.value_or(
(!channel || channel->hiddenPreHistory())
? HistoryVisibility::Hidden
: HistoryVisibility::Visible
);
historyVisibility->setValue(defaultValue);
historyVisibility->setValue(savedValue);
const auto result = parent->add(
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
@ -105,13 +99,13 @@ EditPeerHistoryVisibilityBox::EditPeerHistoryVisibilityBox(
QWidget*,
not_null<PeerData*> peer,
FnMut<void(HistoryVisibility)> savedCallback,
std::optional<HistoryVisibility> historyVisibilitySavedValue)
HistoryVisibility historyVisibilitySavedValue)
: _peer(peer)
, _savedCallback(std::move(savedCallback))
, _historyVisibilitySavedValue(historyVisibilitySavedValue)
, _historyVisibility(
std::make_shared<Ui::RadioenumGroup<HistoryVisibility>>(
_historyVisibilitySavedValue.value_or(HistoryVisibility::Hidden))) {
_historyVisibilitySavedValue)) {
}
void EditPeerHistoryVisibilityBox::prepare() {

View File

@ -10,20 +10,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/abstract_box.h"
#include "ui/widgets/checkbox.h"
namespace style {
struct InfoProfileCountButton;
} // namespace style
namespace Ui {
class VerticalLayout;
} // namespace Ui
namespace Info {
namespace Profile {
class Button;
} // namespace Profile
} // namespace Info
enum class HistoryVisibility {
Visible,
Hidden,
@ -35,7 +21,7 @@ public:
QWidget*,
not_null<PeerData*> peer,
FnMut<void(HistoryVisibility)> savedCallback,
std::optional<HistoryVisibility> historyVisibilitySavedValue = {});
HistoryVisibility historyVisibilitySavedValue);
protected:
void prepare() override;
@ -46,7 +32,7 @@ private:
not_null<PeerData*> _peer;
FnMut<void(HistoryVisibility)> _savedCallback;
std::optional<HistoryVisibility> _historyVisibilitySavedValue;
HistoryVisibility _historyVisibilitySavedValue;
std::shared_ptr<Ui::RadioenumGroup<HistoryVisibility>> _historyVisibility;
};

View File

@ -103,11 +103,11 @@ Info::Profile::Button *AddButtonWithText(
nullptr);
}
Info::Profile::Button *AddButtonDelete(
void AddButtonDelete(
not_null<Ui::VerticalLayout*> parent,
rpl::producer<QString> &&text,
Fn<void()> callback) {
return EditPeerInfoBox::CreateButton(
EditPeerInfoBox::CreateButton(
parent,
std::move(text),
rpl::single(QString()),
@ -210,10 +210,10 @@ private:
void subscribeToMigration();
void migrate(not_null<ChannelData*> channel);
std::optional<Privacy> _privacySavedValue = std::nullopt;
std::optional<HistoryVisibility> _historyVisibilitySavedValue = std::nullopt;
std::optional<QString> _usernameSavedValue = std::nullopt;
std::optional<bool> _signaturesSavedValue = std::nullopt;
std::optional<Privacy> _privacySavedValue = {};
std::optional<HistoryVisibility> _historyVisibilitySavedValue = {};
std::optional<QString> _usernameSavedValue = {};
std::optional<bool> _signaturesSavedValue = {};
not_null<BoxContent*> _box;
not_null<PeerData*> _peer;
@ -457,12 +457,13 @@ void Controller::refreshHistoryVisibility(bool instant = false) {
};
void Controller::showEditPeerTypeBox(std::optional<LangKey> error) {
const auto boxCallback = [=](Privacy checked, QString publicLink) {
const auto boxCallback = crl::guard(this, [=](
Privacy checked, QString publicLink) {
_updadePrivacyType.fire(std::move(checked));
_privacySavedValue = checked;
_usernameSavedValue = publicLink;
refreshHistoryVisibility();
};
});
Ui::show(
Box<EditPeerTypeBox>(
_peer,
@ -501,12 +502,9 @@ void Controller::fillPrivacyTypeButton() {
void Controller::fillInviteLinkButton() {
Expects(_controls.buttonsLayout != nullptr);
const auto boxCallback = [=](Privacy checked, QString publicLink) {
};
const auto buttonCallback = [=] {
Ui::show(
Box<EditPeerTypeBox>(_peer, boxCallback),
Box<EditPeerTypeBox>(_peer),
LayerOption::KeepOther);
};
@ -554,16 +552,16 @@ void Controller::fillHistoryVisibilityButton() {
const auto updateHistoryVisibility =
std::make_shared<rpl::event_stream<HistoryVisibility>>();
const auto boxCallback = [=](HistoryVisibility checked) {
const auto boxCallback = crl::guard(this, [=](HistoryVisibility checked) {
updateHistoryVisibility->fire(std::move(checked));
_historyVisibilitySavedValue = checked;
};
});
const auto buttonCallback = [=] {
Ui::show(
Box<EditPeerHistoryVisibilityBox>(
_peer,
boxCallback,
_historyVisibilitySavedValue),
*_historyVisibilitySavedValue),
LayerOption::KeepOther);
};
AddButtonWithText(

View File

@ -234,10 +234,11 @@ void Controller::fillPrivaciesButtons(
return;
}
const auto result = parent->add(object_ptr<Ui::PaddingWrap<Ui::VerticalLayout>>(
parent,
object_ptr<Ui::VerticalLayout>(parent),
st::editPeerPrivaciesMargins));
const auto result = parent->add(
object_ptr<Ui::PaddingWrap<Ui::VerticalLayout>>(
parent,
object_ptr<Ui::VerticalLayout>(parent),
st::editPeerPrivaciesMargins));
const auto container = result->entity();
const auto isPublic = _peer->isChannel()
@ -288,7 +289,8 @@ object_ptr<Ui::RpWidget> Controller::createUsernameEdit() {
Expects(_wrap != nullptr);
const auto channel = _peer->asChannel();
const auto username = _usernameSavedValue.value_or(channel ? channel->username : QString());
const auto username =
_usernameSavedValue.value_or(channel ? channel->username : QString());
auto result = object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
_wrap,
@ -695,7 +697,7 @@ bool Controller::inviteLinkShown() {
EditPeerTypeBox::EditPeerTypeBox(
QWidget*,
not_null<PeerData*> peer,
FnMut<void(Privacy, QString)> savedCallback,
std::optional<FnMut<void(Privacy, QString)>> savedCallback,
std::optional<Privacy> privacySaved,
std::optional<QString> usernameSaved,
std::optional<LangKey> usernameError)
@ -735,7 +737,7 @@ void EditPeerTypeBox::prepare() {
setTitle(langFactory(controller->getTitle()));
if (!controller->isInviteLink()) {
if (!controller->isInviteLink() && _savedCallback.has_value()) {
addButton(langFactory(lng_settings_save), [=] {
const auto v = controller->getPrivacy();
if (!controller->isAllowSave() && (v == Privacy::Public)) {
@ -743,7 +745,7 @@ void EditPeerTypeBox::prepare() {
return;
}
auto local = std::move(_savedCallback);
auto local = std::move(*_savedCallback);
local(v,
(v == Privacy::Public)
? controller->getUsernameInput()
@ -751,7 +753,9 @@ void EditPeerTypeBox::prepare() {
closeBox();
});
}
addButton(langFactory(controller->isInviteLink() ? lng_close : lng_cancel), [=] { closeBox(); });
addButton(
langFactory(controller->isInviteLink() ? lng_close : lng_cancel),
[=] { closeBox(); });
setDimensionsToContent(st::boxWideWidth, content);
}

View File

@ -42,10 +42,10 @@ public:
EditPeerTypeBox(
QWidget*,
not_null<PeerData*> p,
FnMut<void(Privacy, QString)> savedCallback,
std::optional<Privacy> privacySaved = std::nullopt,
std::optional<QString> usernameSaved = std::nullopt,
std::optional<LangKey> usernameError = std::nullopt);
std::optional<FnMut<void(Privacy, QString)>> savedCallback = {},
std::optional<Privacy> privacySaved = {},
std::optional<QString> usernameSaved = {},
std::optional<LangKey> usernameError = {});
protected:
void prepare() override;
@ -53,7 +53,7 @@ protected:
private:
not_null<PeerData*> _peer;
FnMut<void(Privacy, QString)> _savedCallback;
std::optional<FnMut<void(Privacy, QString)>> _savedCallback;
std::optional<Privacy> _privacySavedValue;
std::optional<QString> _usernameSavedValue;