From ca9db9fd3f96636f4f10cc8896cec2f6d91890ce Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 20 Mar 2019 19:45:21 +0300 Subject: [PATCH] Refactored code. - Removed std::optional from EditPeerHistoryVisibilityBox. - Added std::optional for savedCallback in EditPeerTypeBox. - Guarded boxCallbacks. --- .../edit_peer_history_visibility_box.cpp | 14 +++------- .../peers/edit_peer_history_visibility_box.h | 18 ++---------- .../boxes/peers/edit_peer_info_box.cpp | 28 +++++++++---------- .../boxes/peers/edit_peer_type_box.cpp | 22 +++++++++------ .../boxes/peers/edit_peer_type_box.h | 10 +++---- 5 files changed, 37 insertions(+), 55 deletions(-) diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_history_visibility_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_history_visibility_box.cpp index 95d03a154..38d5c33a8 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_history_visibility_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_history_visibility_box.cpp @@ -51,7 +51,7 @@ void FillContent( not_null parent, not_null peer, std::shared_ptr> historyVisibility, - std::optional 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>( @@ -105,13 +99,13 @@ EditPeerHistoryVisibilityBox::EditPeerHistoryVisibilityBox( QWidget*, not_null peer, FnMut savedCallback, - std::optional historyVisibilitySavedValue) + HistoryVisibility historyVisibilitySavedValue) : _peer(peer) , _savedCallback(std::move(savedCallback)) , _historyVisibilitySavedValue(historyVisibilitySavedValue) , _historyVisibility( std::make_shared>( - _historyVisibilitySavedValue.value_or(HistoryVisibility::Hidden))) { + _historyVisibilitySavedValue)) { } void EditPeerHistoryVisibilityBox::prepare() { diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_history_visibility_box.h b/Telegram/SourceFiles/boxes/peers/edit_peer_history_visibility_box.h index 1cfc923d4..984a061e8 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_history_visibility_box.h +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_history_visibility_box.h @@ -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 peer, FnMut savedCallback, - std::optional historyVisibilitySavedValue = {}); + HistoryVisibility historyVisibilitySavedValue); protected: void prepare() override; @@ -46,7 +32,7 @@ private: not_null _peer; FnMut _savedCallback; - std::optional _historyVisibilitySavedValue; + HistoryVisibility _historyVisibilitySavedValue; std::shared_ptr> _historyVisibility; }; diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp index 0778737c8..b1a857dfc 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp @@ -103,11 +103,11 @@ Info::Profile::Button *AddButtonWithText( nullptr); } -Info::Profile::Button *AddButtonDelete( +void AddButtonDelete( not_null parent, rpl::producer &&text, Fn callback) { - return EditPeerInfoBox::CreateButton( + EditPeerInfoBox::CreateButton( parent, std::move(text), rpl::single(QString()), @@ -210,10 +210,10 @@ private: void subscribeToMigration(); void migrate(not_null channel); - std::optional _privacySavedValue = std::nullopt; - std::optional _historyVisibilitySavedValue = std::nullopt; - std::optional _usernameSavedValue = std::nullopt; - std::optional _signaturesSavedValue = std::nullopt; + std::optional _privacySavedValue = {}; + std::optional _historyVisibilitySavedValue = {}; + std::optional _usernameSavedValue = {}; + std::optional _signaturesSavedValue = {}; not_null _box; not_null _peer; @@ -457,12 +457,13 @@ void Controller::refreshHistoryVisibility(bool instant = false) { }; void Controller::showEditPeerTypeBox(std::optional 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( _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(_peer, boxCallback), + Box(_peer), LayerOption::KeepOther); }; @@ -554,16 +552,16 @@ void Controller::fillHistoryVisibilityButton() { const auto updateHistoryVisibility = std::make_shared>(); - 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( _peer, boxCallback, - _historyVisibilitySavedValue), + *_historyVisibilitySavedValue), LayerOption::KeepOther); }; AddButtonWithText( diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.cpp index 6e66c3d64..9f5cd3af7 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.cpp @@ -234,10 +234,11 @@ void Controller::fillPrivaciesButtons( return; } - const auto result = parent->add(object_ptr>( - parent, - object_ptr(parent), - st::editPeerPrivaciesMargins)); + const auto result = parent->add( + object_ptr>( + parent, + object_ptr(parent), + st::editPeerPrivaciesMargins)); const auto container = result->entity(); const auto isPublic = _peer->isChannel() @@ -288,7 +289,8 @@ object_ptr 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>( _wrap, @@ -695,7 +697,7 @@ bool Controller::inviteLinkShown() { EditPeerTypeBox::EditPeerTypeBox( QWidget*, not_null peer, - FnMut savedCallback, + std::optional> savedCallback, std::optional privacySaved, std::optional usernameSaved, std::optional 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); } diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.h b/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.h index e399f99ad..42922da3a 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.h +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.h @@ -42,10 +42,10 @@ public: EditPeerTypeBox( QWidget*, not_null p, - FnMut savedCallback, - std::optional privacySaved = std::nullopt, - std::optional usernameSaved = std::nullopt, - std::optional usernameError = std::nullopt); + std::optional> savedCallback = {}, + std::optional privacySaved = {}, + std::optional usernameSaved = {}, + std::optional usernameError = {}); protected: void prepare() override; @@ -53,7 +53,7 @@ protected: private: not_null _peer; - FnMut _savedCallback; + std::optional> _savedCallback; std::optional _privacySavedValue; std::optional _usernameSavedValue;