Add warning for unsaved Bio section closing.

This commit is contained in:
John Preston 2018-09-11 10:46:07 +03:00
parent fbf3c005ff
commit 165511fb14
5 changed files with 30 additions and 4 deletions

View File

@ -349,6 +349,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_settings_username_label" = "Username";
"lng_settings_phone_label" = "Phone number";
"lng_settings_username_add" = "Add username";
"lng_settings_close_sure" = "Are you sure you want to close this page? You didn't save your changes.";
"lng_backgrounds_header" = "Choose your new chat background";
"lng_theme_sure_keep" = "Keep this theme?";

View File

@ -277,6 +277,10 @@ rpl::producer<bool> Controller::canSaveChanges() const {
return _canSaveChanges.value();
}
bool Controller::canSaveChangesNow() const {
return _canSaveChanges.current();
}
Controller::~Controller() = default;
} // namespace Info

View File

@ -190,6 +190,7 @@ public:
void setCanSaveChanges(rpl::producer<bool> can);
rpl::producer<bool> canSaveChanges() const;
bool canSaveChangesNow() const;
void saveSearchState(not_null<ContentMemento*> memento);

View File

@ -25,6 +25,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "window/window_slide_animation.h"
#include "window/window_peer_menu.h"
#include "boxes/peer_list_box.h"
#include "boxes/confirm_box.h"
#include "auth_session.h"
#include "data/data_session.h"
#include "mainwidget.h"
@ -350,8 +351,8 @@ void WrapWidget::createTopBar() {
if (wrapValue == Wrap::Narrow || hasStackHistory()) {
_topBar->enableBackButton();
_topBar->backRequest(
) | rpl::start_with_next([this] {
_controller->showBackFromStack();
) | rpl::start_with_next([=] {
checkBeforeClose([=] { _controller->showBackFromStack(); });
}, _topBar->lifetime());
} else if (wrapValue == Wrap::Side) {
auto close = _topBar->addButton(
@ -368,7 +369,9 @@ void WrapWidget::createTopBar() {
_topBar,
st::infoLayerTopBarClose));
close->addClickHandler([this] {
_controller->parentController()->hideSpecialLayer();
checkBeforeClose([=] {
_controller->parentController()->hideSpecialLayer();
});
});
} else if (requireTopBarSearch()) {
auto search = _controller->searchFieldController();
@ -398,6 +401,22 @@ void WrapWidget::createTopBar() {
_topBar->show();
}
void WrapWidget::checkBeforeClose(Fn<void()> close) {
const auto confirmed = [=] {
const auto copy = close;
Ui::hideLayer();
copy();
};
if (_controller->canSaveChangesNow()) {
Ui::show(Box<ConfirmBox>(
lang(lng_settings_close_sure),
lang(lng_close),
confirmed));
} else {
confirmed();
}
}
void WrapWidget::addTopBarMenuButton() {
Expects(_topBar != nullptr);
@ -950,7 +969,7 @@ void WrapWidget::resizeEvent(QResizeEvent *e) {
void WrapWidget::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Escape) {
if (hasStackHistory() || wrap() != Wrap::Layer) {
_controller->showBackFromStack();
checkBeforeClose([=] { _controller->showBackFromStack(); });
return;
}
}

View File

@ -150,6 +150,7 @@ private:
void injectActiveFeedProfile(not_null<Data::Feed*> feed);
void injectActiveProfileMemento(
std::unique_ptr<ContentMemento> memento);
void checkBeforeClose(Fn<void()> close);
void restoreHistoryStack(
std::vector<std::unique_ptr<ContentMemento>> stack);
bool hasStackHistory() const {