Handle last suggestion being added.

This commit is contained in:
John Preston 2020-03-10 16:51:23 +04:00
parent b8c11f3d8c
commit 2fb2fa9661
1 changed files with 44 additions and 30 deletions

View File

@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/widgets/labels.h" #include "ui/widgets/labels.h"
#include "ui/widgets/buttons.h" #include "ui/widgets/buttons.h"
#include "ui/text/text_utilities.h" #include "ui/text/text_utilities.h"
#include "ui/wrap/slide_wrap.h"
#include "settings/settings_common.h" #include "settings/settings_common.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "apiwrap.h" #include "apiwrap.h"
@ -297,7 +298,7 @@ void ManageFiltersPrepare::showBoxWithSuggested() {
void ManageFiltersPrepare::CreateBox( void ManageFiltersPrepare::CreateBox(
not_null<Ui::GenericBox*> box, not_null<Ui::GenericBox*> box,
not_null<Window::SessionController*> window, not_null<Window::SessionController*> window,
const std::vector<Suggested> &suggested) { const std::vector<Suggested> &suggestions) {
struct FilterRow { struct FilterRow {
not_null<FilterRowButton*> button; not_null<FilterRowButton*> button;
Data::ChatFilter filter; Data::ChatFilter filter;
@ -349,45 +350,58 @@ void ManageFiltersPrepare::CreateBox(
tr::lng_filters_create() | Ui::Text::ToUpper(), tr::lng_filters_create() | Ui::Text::ToUpper(),
st::settingsUpdate); st::settingsUpdate);
Settings::AddSkip(content); Settings::AddSkip(content);
if (suggested.empty()) { const auto emptyAbout = content->add(
content->add( object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
content,
object_ptr<Ui::FlatLabel>( object_ptr<Ui::FlatLabel>(
content, content,
tr::lng_filters_about(), tr::lng_filters_about(),
st::boxDividerLabel), st::boxDividerLabel),
st::settingsDividerLabelPadding); st::settingsDividerLabelPadding)
} else { )->setDuration(0);
Settings::AddDividerText(content, tr::lng_filters_about()); const auto nonEmptyAbout = content->add(
Settings::AddSkip(content); object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
Settings::AddSubsectionTitle(content, tr::lng_filters_recommended()); content,
object_ptr<Ui::VerticalLayout>(content))
)->setDuration(0);
const auto aboutRows = nonEmptyAbout->entity();
Settings::AddDividerText(aboutRows, tr::lng_filters_about());
Settings::AddSkip(aboutRows);
Settings::AddSubsectionTitle(aboutRows, tr::lng_filters_recommended());
for (const auto &suggestion : suggested) { const auto suggested = box->lifetime().make_state<rpl::variable<int>>();
const auto filter = suggestion.filter; for (const auto &suggestion : suggestions) {
const auto already = [&] { const auto filter = suggestion.filter;
for (const auto &entry : list) { const auto already = [&] {
if (entry.flags() == filter.flags() for (const auto &entry : list) {
&& entry.always() == filter.always() if (entry.flags() == filter.flags()
&& entry.never() == filter.never()) { && entry.always() == filter.always()
return true; && entry.never() == filter.never()) {
} return true;
} }
return false;
}();
if (already) {
continue;
} }
const auto button = content->add(object_ptr<FilterRowButton>( return false;
content, }();
filter, if (already) {
suggestion.description)); continue;
button->addRequests(
) | rpl::start_with_next([=] {
addFilter(filter);
delete button;
}, button->lifetime());
} }
*suggested = suggested->current() + 1;
const auto button = aboutRows->add(object_ptr<FilterRowButton>(
aboutRows,
filter,
suggestion.description));
button->addRequests(
) | rpl::start_with_next([=] {
addFilter(filter);
*suggested = suggested->current() - 1;
delete button;
}, button->lifetime());
} }
using namespace rpl::mappers;
emptyAbout->toggleOn(suggested->value() | rpl::map(_1 == 0));
nonEmptyAbout->toggleOn(suggested->value() | rpl::map(_1 > 0));
const auto prepareGoodIdsForNewFilters = [=] { const auto prepareGoodIdsForNewFilters = [=] {
const auto &list = session->data().chatsFilters().list(); const auto &list = session->data().chatsFilters().list();