Refactored code.

- Slightly improved design.
 - Added "const" in EditPeerTypeBox & EditPeerInfoBox.
This commit is contained in:
23rd 2019-03-18 21:12:08 +03:00 committed by John Preston
parent e024d9bbb0
commit bd7ba3acb1
4 changed files with 133 additions and 169 deletions

View File

@ -145,8 +145,8 @@ private:
Ui::InputField *description = nullptr;
Ui::UserpicButton *photo = nullptr;
rpl::lifetime initialPhotoImageWaiting;
Ui::SlideWrap<Ui::RpWidget> *historyVisibilityWrap = nullptr;
Ui::VerticalLayout *buttonsLayout = nullptr;
Ui::SlideWrap<Ui::RpWidget> *historyVisibilityWrap = nullptr;
};
struct Saving {
std::optional<QString> username;
@ -160,22 +160,17 @@ private:
object_ptr<Ui::RpWidget> createTitleEdit();
object_ptr<Ui::RpWidget> createPhotoEdit();
object_ptr<Ui::RpWidget> createDescriptionEdit();
object_ptr<Ui::RpWidget> createManageGroupButtons();
object_ptr<Ui::RpWidget> createStickersEdit();
object_ptr<Ui::RpWidget> createDeleteButton();
void refreshHistoryVisibility(bool instant);
void showEditPeerTypeBox(std::optional<LangKey> error = std::nullopt);
void fillPrivacyTypeButton();
void fillInviteLinkButton();
void fillSignaturesButton();
void fillHistoryVisibilityButton();
void fillManageSection(not_null<Window::Navigation*> navigation, not_null<PeerData*> peer);
object_ptr<Ui::RpWidget> createUsernameEdit();
object_ptr<Ui::RpWidget> createInviteLinkCreate();
object_ptr<Ui::RpWidget> createInviteLinkEdit();
object_ptr<Ui::RpWidget> createStickersEdit();
object_ptr<Ui::RpWidget> createDeleteButton();
object_ptr<Ui::RpWidget> createManageGroupButtons();
void observeInviteLink();
void fillManageSection();
void submitTitle();
void submitDescription();
@ -252,7 +247,6 @@ void Controller::subscribeToMigration() {
void Controller::migrate(not_null<ChannelData*> channel) {
_peer = channel;
// observeInviteLink();
_peer->updateFull();
}
@ -263,9 +257,7 @@ object_ptr<Ui::VerticalLayout> Controller::createContent() {
_wrap->add(createPhotoAndTitleEdit());
_wrap->add(createDescriptionEdit());
_wrap->add(createManageGroupButtons());
_wrap->add(createStickersEdit());
_wrap->add(createDeleteButton());
@ -294,12 +286,12 @@ object_ptr<Ui::RpWidget> Controller::createPhotoAndTitleEdit() {
}
auto result = object_ptr<Ui::RpWidget>(_wrap);
auto container = result.data();
const auto container = result.data();
auto photoWrap = Ui::AttachParentChild(
const auto photoWrap = Ui::AttachParentChild(
container,
createPhotoEdit());
auto titleEdit = Ui::AttachParentChild(
const auto titleEdit = Ui::AttachParentChild(
container,
createTitleEdit());
photoWrap->heightValue(
@ -308,7 +300,7 @@ object_ptr<Ui::RpWidget> Controller::createPhotoAndTitleEdit() {
}, photoWrap->lifetime());
container->widthValue(
) | rpl::start_with_next([titleEdit](int width) {
auto left = st::editPeerPhotoMargins.left()
const auto left = st::editPeerPhotoMargins.left()
+ st::defaultUserpicButton.size.width();
titleEdit->resizeToWidth(width - left);
titleEdit->moveToLeft(left, 0, width);
@ -393,6 +385,84 @@ object_ptr<Ui::RpWidget> Controller::createDescriptionEdit() {
return std::move(result);
}
object_ptr<Ui::RpWidget> Controller::createManageGroupButtons() {
Expects(_wrap != nullptr);
auto result = object_ptr<Ui::PaddingWrap<Ui::VerticalLayout>>(
_wrap,
object_ptr<Ui::VerticalLayout>(_wrap),
st::editPeerBottomButtonsLayoutMargins);
_controls.buttonsLayout = result->entity();
fillManageSection();
return std::move(result);
}
object_ptr<Ui::RpWidget> Controller::createStickersEdit() {
Expects(_wrap != nullptr);
const auto channel = _peer->asChannel();
if (!channel || !channel->canEditStickers()) {
return nullptr;
}
auto result = object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
_wrap,
object_ptr<Ui::VerticalLayout>(_wrap),
st::editPeerInvitesMargins);
const auto container = result->entity();
container->add(object_ptr<Ui::FlatLabel>(
container,
Lang::Viewer(lng_group_stickers),
st::editPeerSectionLabel));
container->add(object_ptr<Ui::FixedHeightWidget>(
container,
st::editPeerInviteLinkSkip));
container->add(object_ptr<Ui::FlatLabel>(
container,
Lang::Viewer(lng_group_stickers_description),
st::editPeerPrivacyLabel));
container->add(object_ptr<Ui::FixedHeightWidget>(
container,
st::editPeerInviteLinkSkip));
container->add(object_ptr<Ui::LinkButton>(
_wrap,
lang(lng_group_stickers_add),
st::editPeerInviteLinkButton)
)->addClickHandler([=] {
Ui::show(Box<StickersBox>(channel), LayerOption::KeepOther);
});
return std::move(result);
}
object_ptr<Ui::RpWidget> Controller::createDeleteButton() {
Expects(_wrap != nullptr);
const auto channel = _peer->asChannel();
if (!channel || !channel->canDelete()) {
return nullptr;
}
const auto text = lang(_isGroup
? lng_profile_delete_group
: lng_profile_delete_channel);
auto result = object_ptr<Ui::PaddingWrap<Ui::LinkButton>>(
_wrap,
object_ptr<Ui::LinkButton>(
_wrap,
text,
st::editPeerDeleteButton),
st::editPeerDeleteButtonMargins);
result->entity()->addClickHandler([this] {
deleteWithConfirmation();
});
return std::move(result);
}
void Controller::refreshHistoryVisibility(bool instant = false) {
if (!_controls.historyVisibilityWrap) {
return;
@ -486,14 +556,14 @@ void Controller::fillSignaturesButton() {
void Controller::fillHistoryVisibilityButton() {
Expects(_controls.buttonsLayout != nullptr);
auto wrapLayout = _controls.buttonsLayout->add(object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
const auto wrapLayout = _controls.buttonsLayout->add(object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
_controls.buttonsLayout,
object_ptr<Ui::VerticalLayout>(_controls.buttonsLayout),
st::boxOptionListPadding)); // Empty margins.
_controls.historyVisibilityWrap = wrapLayout;
const auto channel = _peer->asChannel();
auto container = wrapLayout->entity();
const auto container = wrapLayout->entity();
_historyVisibilitySavedValue = (!channel || channel->hiddenPreHistory())
? HistoryVisibility::Hidden
@ -531,13 +601,12 @@ void Controller::fillHistoryVisibilityButton() {
refreshHistoryVisibility(true);
}
void Controller::fillManageSection(
not_null<Window::Navigation*> navigation,
not_null<PeerData*> peer) {
void Controller::fillManageSection() {
Expects(_controls.buttonsLayout != nullptr);
const auto navigation = App::wnd()->controller();
const auto chat = peer->asChat();
const auto channel = peer->asChannel();
const auto chat = _peer->asChat();
const auto channel = _peer->asChannel();
const auto isChannel = (!chat);
if (!chat && !channel) return;
@ -604,29 +673,30 @@ void Controller::fillManageSection(
fillHistoryVisibilityButton();
}
if (canEditPreHistoryHidden || canEditSignatures || canEditInviteLink) {
// Perhaps should fix extra 1-pixel line for design.
AddSkip(_controls.buttonsLayout);
AddSkip(_controls.buttonsLayout,
st::editPeerTopButtonsLayoutSkip,
st::editPeerTopButtonsLayoutSkipCustomBottom);
}
if (canEditPermissions) {
AddButtonWithCount(
_controls.buttonsLayout,
Lang::Viewer(lng_manage_peer_permissions),
Info::Profile::RestrictionsCountValue(peer)
Info::Profile::RestrictionsCountValue(_peer)
| ToPositiveNumberStringRestrictions(),
[=] { ShowEditPermissions(peer); },
[=] { ShowEditPermissions(_peer); },
st::infoIconPermissions);
}
if (canViewAdmins) {
AddButtonWithCount(
_controls.buttonsLayout,
Lang::Viewer(lng_manage_peer_administrators),
Info::Profile::AdminsCountValue(peer)
Info::Profile::AdminsCountValue(_peer)
| ToPositiveNumberString(),
[=] {
ParticipantsBoxController::Start(
navigation,
peer,
_peer,
ParticipantsBoxController::Role::Admins);
},
st::infoIconAdministrators);
@ -635,12 +705,12 @@ void Controller::fillManageSection(
AddButtonWithCount(
_controls.buttonsLayout,
Lang::Viewer(lng_manage_peer_members),
Info::Profile::MembersCountValue(peer)
Info::Profile::MembersCountValue(_peer)
| ToPositiveNumberString(),
[=] {
ParticipantsBoxController::Start(
navigation,
peer,
_peer,
ParticipantsBoxController::Role::Members);
},
st::infoIconMembers);
@ -654,7 +724,7 @@ void Controller::fillManageSection(
[=] {
ParticipantsBoxController::Start(
navigation,
peer,
_peer,
ParticipantsBoxController::Role::Kicked);
},
st::infoIconBlacklist);
@ -670,85 +740,8 @@ void Controller::fillManageSection(
st::infoIconRecentActions);
}
AddSkip(_controls.buttonsLayout);
}
object_ptr<Ui::RpWidget> Controller::createManageGroupButtons() {
Expects(_wrap != nullptr);
auto result = object_ptr<Ui::PaddingWrap<Ui::VerticalLayout>>(
_wrap,
object_ptr<Ui::VerticalLayout>(_wrap),
st::editPeerBottomButtonsLayoutMargins);
_controls.buttonsLayout = result->entity();
fillManageSection(App::wnd()->controller(), _peer);
return std::move(result);
}
object_ptr<Ui::RpWidget> Controller::createStickersEdit() {
Expects(_wrap != nullptr);
auto channel = _peer->asChannel();
if (!channel || !channel->canEditStickers()) {
return nullptr;
}
auto result = object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
_wrap,
object_ptr<Ui::VerticalLayout>(_wrap),
st::editPeerInvitesMargins);
auto container = result->entity();
container->add(object_ptr<Ui::FlatLabel>(
container,
Lang::Viewer(lng_group_stickers),
st::editPeerSectionLabel));
container->add(object_ptr<Ui::FixedHeightWidget>(
container,
st::editPeerInviteLinkSkip));
container->add(object_ptr<Ui::FlatLabel>(
container,
Lang::Viewer(lng_group_stickers_description),
st::editPeerPrivacyLabel));
container->add(object_ptr<Ui::FixedHeightWidget>(
container,
st::editPeerInviteLinkSkip));
container->add(object_ptr<Ui::LinkButton>(
_wrap,
lang(lng_group_stickers_add),
st::editPeerInviteLinkButton)
)->addClickHandler([=] {
Ui::show(Box<StickersBox>(channel), LayerOption::KeepOther);
});
return std::move(result);
}
object_ptr<Ui::RpWidget> Controller::createDeleteButton() {
Expects(_wrap != nullptr);
auto channel = _peer->asChannel();
if (!channel || !channel->canDelete()) {
return nullptr;
}
auto text = lang(_isGroup
? lng_profile_delete_group
: lng_profile_delete_channel);
auto result = object_ptr<Ui::PaddingWrap<Ui::LinkButton>>(
_wrap,
object_ptr<Ui::LinkButton>(
_wrap,
text,
st::editPeerDeleteButton),
st::editPeerDeleteButtonMargins);
result->entity()->addClickHandler([this] {
deleteWithConfirmation();
});
return std::move(result);
AddSkip(_controls.buttonsLayout,
st::editPeerTopButtonsLayoutSkipCustomTop);
}
void Controller::submitTitle() {
@ -792,7 +785,7 @@ bool Controller::validateUsername(Saving &to) const {
to.username = QString();
return true;
}
auto username = _usernameSavedValue.value_or(
const auto username = _usernameSavedValue.value_or(
_peer->isChannel()
? _peer->asChannel()->username
: QString()
@ -808,7 +801,7 @@ bool Controller::validateTitle(Saving &to) const {
if (!_controls.title) {
return true;
}
auto title = _controls.title->getLastText().trimmed();
const auto title = _controls.title->getLastText().trimmed();
if (title.isEmpty()) {
_controls.title->showError();
_box->scrollToWidget(_controls.title);
@ -852,7 +845,7 @@ void Controller::save() {
if (!_saveStagesQueue.empty()) {
return;
}
if (auto saving = validate()) {
if (const auto saving = validate()) {
_savingData = *saving;
pushSaveStage([this] { saveUsername(); });
pushSaveStage([this] { saveTitle(); });
@ -1120,7 +1113,7 @@ EditPeerInfoBox::EditPeerInfoBox(
}
void EditPeerInfoBox::prepare() {
auto controller = Ui::CreateChild<Controller>(this, this, _peer);
const auto controller = Ui::CreateChild<Controller>(this, this, _peer);
_focusRequests.events(
) | rpl::start_with_next(
[=] { controller->setFocus(); },

View File

@ -134,9 +134,6 @@ private:
bool inviteLinkShown();
QString inviteLinkText();
void subscribeToMigration();
void migrate(not_null<ChannelData*> channel);
not_null<PeerData*> _peer;
std::optional<Privacy> _privacySavedValue = std::nullopt;
std::optional<QString> _usernameSavedValue = std::nullopt;
@ -171,20 +168,6 @@ Controller::Controller(
, _isAllowSave(!_usernameSavedValue.value_or(QString()).isEmpty())
, _wrap(container)
, _checkUsernameTimer([=] { checkUsernameAvailability(); }) {
subscribeToMigration();
_peer->updateFull();
}
void Controller::subscribeToMigration() {
SubscribeToMigration(
_peer,
_lifetime,
[=](not_null<ChannelData*> channel) { migrate(channel); });
}
void Controller::migrate(not_null<ChannelData*> channel) {
_peer = channel;
observeInviteLink();
_peer->updateFull();
}
@ -208,7 +191,6 @@ void Controller::createContent() {
if (_controls.privacy->value() == Privacy::Private) {
checkUsernameAvailability();
}
if (_usernameError.has_value()) {
showUsernameError(Lang::Viewer(_usernameError.value()));
// Not focused actually.
@ -263,7 +245,7 @@ void Controller::fillPrivaciesButtons(
parent,
object_ptr<Ui::VerticalLayout>(parent),
st::editPeerPrivaciesMargins));
auto container = result->entity();
const auto container = result->entity();
const auto isPublic = _peer->isChannel()
&& _peer->asChannel()->isPublic();
@ -288,12 +270,6 @@ void Controller::fillPrivaciesButtons(
_controls.privacy->setChangedCallback([=](Privacy value) {
privacyChanged(value);
});
if (!isPublic) {
// checkUsernameAvailability();
}
// return std::move(result);
}
void Controller::setFocusUsername() {
@ -327,7 +303,7 @@ object_ptr<Ui::RpWidget> Controller::createUsernameEdit() {
st::editPeerUsernameMargins);
_controls.usernameWrap = result.data();
auto container = result->entity();
const auto container = result->entity();
container->add(object_ptr<Ui::PaddingWrap<Ui::FlatLabel>>(
container,
object_ptr<Ui::FlatLabel>(
@ -336,7 +312,7 @@ object_ptr<Ui::RpWidget> Controller::createUsernameEdit() {
st::editPeerSectionLabel),
st::editPeerUsernameTitleLabelMargins));
auto placeholder = container->add(object_ptr<Ui::RpWidget>(
const auto placeholder = container->add(object_ptr<Ui::RpWidget>(
container));
placeholder->setAttribute(Qt::WA_TransparentForMouseEvents);
_controls.usernameInput = Ui::AttachParentChild(
@ -372,19 +348,19 @@ object_ptr<Ui::RpWidget> Controller::createUsernameEdit() {
&Ui::UsernameInput::changed,
[this] { usernameChanged(); });
auto shown = (_controls.privacy->value() == Privacy::Public);
const auto shown = (_controls.privacy->value() == Privacy::Public);
result->toggle(shown, anim::type::instant);
return std::move(result);
}
void Controller::privacyChanged(Privacy value) {
auto toggleEditUsername = [&] {
const auto toggleEditUsername = [&] {
_controls.usernameWrap->toggle(
(value == Privacy::Public),
anim::type::instant);
};
auto refreshVisibilities = [&] {
const auto refreshVisibilities = [&] {
// Now first we need to hide that was shown.
// Otherwise box will change own Y position.
@ -425,8 +401,8 @@ void Controller::checkUsernameAvailability() {
if (!_controls.usernameInput) {
return;
}
auto initial = (_controls.privacy->value() != Privacy::Public);
auto checking = initial
const auto initial = (_controls.privacy->value() != Privacy::Public);
const auto checking = initial
? qsl(".bad.")
: getUsernameInput();
if (checking.size() < kMinUsernameLength) {
@ -482,7 +458,7 @@ void Controller::checkUsernameAvailability() {
void Controller::askUsernameRevoke() {
_controls.privacy->setValue(Privacy::Private);
auto revokeCallback = crl::guard(this, [this] {
const auto revokeCallback = crl::guard(this, [this] {
_usernameState = UsernameState::Normal;
_controls.privacy->setValue(Privacy::Public);
checkUsernameAvailability();
@ -494,13 +470,13 @@ void Controller::askUsernameRevoke() {
void Controller::usernameChanged() {
_isAllowSave = false;
auto username = getUsernameInput();
const auto username = getUsernameInput();
if (username.isEmpty()) {
_controls.usernameResult = nullptr;
_checkUsernameTimer.cancel();
return;
}
auto bad = ranges::find_if(username, [](QChar ch) {
const auto bad = ranges::find_if(username, [](QChar ch) {
return (ch < 'A' || ch > 'Z')
&& (ch < 'a' || ch > 'z')
&& (ch < '0' || ch > '9')
@ -540,7 +516,7 @@ void Controller::showUsernameResult(
_controls.usernameWrap,
_usernameResultTexts.events() | rpl::flatten_latest(),
*st);
auto label = _controls.usernameResult.get();
const auto label = _controls.usernameResult.get();
label->show();
label->widthValue(
) | rpl::start_with_next([label] {
@ -563,8 +539,8 @@ void Controller::revokeInviteLink() {
}
void Controller::exportInviteLink(const QString &confirmation) {
auto boxPointer = std::make_shared<QPointer<ConfirmBox>>();
auto callback = crl::guard(this, [=] {
const auto boxPointer = std::make_shared<QPointer<ConfirmBox>>();
const auto callback = crl::guard(this, [=] {
if (const auto strong = *boxPointer) {
strong->closeBox();
}
@ -591,7 +567,6 @@ void Controller::observeInviteLink() {
if (!_controls.editInviteLinkWrap) {
return;
}
// return; //
Notify::PeerUpdateValue(
_peer,
Notify::PeerUpdate::Flag::InviteLinkChanged
@ -614,7 +589,7 @@ object_ptr<Ui::RpWidget> Controller::createInviteLinkEdit() {
st::editPeerInvitesMargins);
_controls.editInviteLinkWrap = result.data();
auto container = result->entity();
const auto container = result->entity();
if (!_isInviteLink) {
container->add(object_ptr<Ui::FlatLabel>(
container,
@ -652,11 +627,11 @@ object_ptr<Ui::RpWidget> Controller::createInviteLinkEdit() {
}
void Controller::refreshEditInviteLink() {
auto link = inviteLinkText();
const auto link = inviteLinkText();
auto text = TextWithEntities();
if (!link.isEmpty()) {
text.text = link;
auto remove = qstr("https://");
const auto remove = qstr("https://");
if (text.text.startsWith(remove)) {
text.text.remove(0, remove.size());
}
@ -687,7 +662,7 @@ object_ptr<Ui::RpWidget> Controller::createInviteLinkCreate() {
_wrap,
object_ptr<Ui::VerticalLayout>(_wrap),
st::editPeerInvitesMargins);
auto container = result->entity();
const auto container = result->entity();
if (!_isInviteLink) {
container->add(object_ptr<Ui::FlatLabel>(
@ -746,7 +721,7 @@ void EditPeerTypeBox::prepare() {
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
auto controller = Ui::CreateChild<Controller>(
const auto controller = Ui::CreateChild<Controller>(
this,
content,
_peer,
@ -781,8 +756,3 @@ void EditPeerTypeBox::prepare() {
setDimensionsToContent(st::boxWideWidth, content);
}
void EditPeerTypeBox::setupContent() {
}

View File

@ -52,8 +52,6 @@ protected:
void prepare() override;
private:
void setupContent();
not_null<PeerData*> _peer;
FnMut<void(Privacy, QString)> _savedCallback;

View File

@ -645,9 +645,12 @@ editPeerTopButtonsLayoutMargins: margins(0px, 12px, 0px, 6px);
editPeerTopButtonsLayoutSkip: 13px;
editPeerTopButtonsLayoutSkipToBottom: 12px;
editPeerTopButtonsLayoutSkipCustomTop: 14px;
editPeerTopButtonsLayoutSkipCustomBottom: 11px;
editPeerHistoryVisibilityTopSkip: 8px;
editPeerDeleteButtonMargins: margins(23px, 16px, 23px, 16px);
editPeerDeleteButtonMargins: margins(25px, 11px, 23px, 16px);
editPeerDeleteButton: sessionTerminateAllButton;
editPeerPhotoMargins: margins(23px, 16px, 23px, 8px);
editPeerTitle: defaultInputField;