From 3ba2a7931ecfb44dfdf3aea5b0f1718fa566064a Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 18 Sep 2018 19:32:07 +0300 Subject: [PATCH] Autosave bio in settings edit section. --- Telegram/SourceFiles/info/info.style | 2 - .../SourceFiles/settings/settings_chat.cpp | 33 +++++-------- .../settings/settings_information.cpp | 47 +++++++++++++++---- .../settings/settings_information.h | 7 +-- Telegram/SourceFiles/ui/widgets/widgets.style | 4 +- 5 files changed, 54 insertions(+), 39 deletions(-) diff --git a/Telegram/SourceFiles/info/info.style b/Telegram/SourceFiles/info/info.style index 1a7de6387..b155d152c 100644 --- a/Telegram/SourceFiles/info/info.style +++ b/Telegram/SourceFiles/info/info.style @@ -395,8 +395,6 @@ infoBlockHeaderLabel: FlatLabel(infoProfileStatusLabel) { infoBlockHeaderPosition: point(79px, 22px); infoProfileToggle: Toggle(defaultToggle) { - diameter: 16px; - width: 14px; untoggledFg: menuIconFg; } infoProfileToggleOver: Toggle(infoProfileToggle) { diff --git a/Telegram/SourceFiles/settings/settings_chat.cpp b/Telegram/SourceFiles/settings/settings_chat.cpp index 962fa6924..7ca9f5b8e 100644 --- a/Telegram/SourceFiles/settings/settings_chat.cpp +++ b/Telegram/SourceFiles/settings/settings_chat.cpp @@ -467,25 +467,18 @@ void SetupLocalStorage(not_null container) { } void SetupDataStorage(not_null container) { + using namespace rpl::mappers; + AddDivider(container); AddSkip(container, st::settingsSectionSkip); AddSubsectionTitle(container, lng_settings_data_storage); - auto wrap = object_ptr(container); - const auto inner = wrap.data(); - container->add(object_ptr( + const auto ask = AddButton( container, - std::move(wrap), - QMargins(0, 0, 0, st::settingsCheckbox.margin.bottom()))); - - const auto ask = inner->add( - object_ptr( - inner, - lang(lng_download_path_ask), - Global::AskDownloadPath(), - st::settingsCheckbox), - st::settingsCheckboxPadding); + lng_download_path_ask, + st::settingsButton + )->toggleOn(rpl::single(Global::AskDownloadPath())); #ifndef OS_WIN_STORE const auto showpath = Ui::AttachAsChild( @@ -513,13 +506,13 @@ void SetupDataStorage(not_null container) { path->entity()->addClickHandler([] { Ui::show(Box()); }); - path->toggleOn( - showpath->events_starting_with_copy(!Global::AskDownloadPath())); + path->toggleOn(ask->toggledValue() | rpl::map(!_1)); #endif // OS_WIN_STORE - base::ObservableViewer( - ask->checkedChanged - ) | rpl::start_with_next([=](bool checked) { + ask->toggledValue( + ) | rpl::filter([](bool checked) { + return (checked != Global::AskDownloadPath()); + }) | rpl::start_with_next([=](bool checked) { Global::SetAskDownloadPath(checked); Local::writeUserSettings(); @@ -527,7 +520,7 @@ void SetupDataStorage(not_null container) { showpath->fire_copy(!checked); #endif // OS_WIN_STORE - }, inner->lifetime()); + }, ask->lifetime()); AddButton( container, @@ -696,8 +689,8 @@ void Chat::setupContent() { SetupStickersEmoji(content); SetupMessages(content); SetupChatBackground(content); - SetupThemeOptions(content); SetupDataStorage(content); + SetupThemeOptions(content); Ui::ResizeFitChild(this, content); } diff --git a/Telegram/SourceFiles/settings/settings_information.cpp b/Telegram/SourceFiles/settings/settings_information.cpp index 89632bcd3..2f5aa69c6 100644 --- a/Telegram/SourceFiles/settings/settings_information.cpp +++ b/Telegram/SourceFiles/settings/settings_information.cpp @@ -32,6 +32,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Settings { namespace { +constexpr auto kSaveBioTimeout = 1000; + void SetupPhoto( not_null container, not_null controller, @@ -348,6 +350,30 @@ BioManager SetupBio( } }, bio->lifetime()); + const auto generation = Ui::AttachAsChild(bio, 0); + changed->events( + ) | rpl::start_with_next([=](bool changed) { + if (changed) { + const auto saved = *generation = std::abs(*generation) + 1; + App::CallDelayed(kSaveBioTimeout, bio, [=] { + if (*generation == saved) { + save(nullptr); + *generation = 0; + } + }); + } else if (*generation > 0) { + *generation = -*generation; + } + }, bio->lifetime()); + + // We need 'bio' to still exist here as InputField, so we add this + // to 'container' lifetime, not to the 'bio' lifetime. + container->lifetime().add([=] { + if (*generation > 0) { + save(nullptr); + } + }); + bio->setMaxLength(kMaxBioLength); bio->setSubmitSettings(Ui::InputField::SubmitSettings::Both); auto cursor = bio->textCursor(); @@ -387,22 +413,23 @@ Information::Information( setupContent(controller); } -rpl::producer Information::sectionCanSaveChanges() { - return _canSaveChanges.value(); -} - -void Information::sectionSaveChanges(FnMut done) { - _save(std::move(done)); -} +//rpl::producer Information::sectionCanSaveChanges() { +// return _canSaveChanges.value(); +//} +// +//void Information::sectionSaveChanges(FnMut done) { +// _save(std::move(done)); +//} void Information::setupContent(not_null controller) { const auto content = Ui::CreateChild(this); SetupPhoto(content, controller, _self); SetupRows(content, _self); - auto manager = SetupBio(content, _self); - _canSaveChanges = std::move(manager.canSave); - _save = std::move(manager.save); + SetupBio(content, _self); + //auto manager = SetupBio(content, _self); + //_canSaveChanges = std::move(manager.canSave); + //_save = std::move(manager.save); Ui::ResizeFitChild(this, content); } diff --git a/Telegram/SourceFiles/settings/settings_information.h b/Telegram/SourceFiles/settings/settings_information.h index d53e81d9d..67cef5a05 100644 --- a/Telegram/SourceFiles/settings/settings_information.h +++ b/Telegram/SourceFiles/settings/settings_information.h @@ -18,15 +18,12 @@ public: not_null controller, not_null self); - rpl::producer sectionCanSaveChanges() override; - void sectionSaveChanges(FnMut done) override; - private: void setupContent(not_null controller); not_null _self; - rpl::variable _canSaveChanges; - Fn done)> _save; + //rpl::variable _canSaveChanges; + //Fn done)> _save; }; diff --git a/Telegram/SourceFiles/ui/widgets/widgets.style b/Telegram/SourceFiles/ui/widgets/widgets.style index 5c1dd395d..3c0b09756 100644 --- a/Telegram/SourceFiles/ui/widgets/widgets.style +++ b/Telegram/SourceFiles/ui/widgets/widgets.style @@ -754,8 +754,8 @@ defaultToggle: Toggle { duration: 120; border: 2px; shift: 1px; - diameter: 20px; - width: 16px; + diameter: 16px; + width: 14px; xsize: 0px; vsize: 0px; vshift: 0px;