2020-03-10 12:41:12 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
2020-03-24 08:58:47 +00:00
|
|
|
#include "settings/settings_folders.h"
|
2020-03-10 12:41:12 +00:00
|
|
|
|
2020-03-13 11:07:27 +00:00
|
|
|
#include "boxes/filters/edit_filter_box.h"
|
2020-03-10 12:41:12 +00:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_folder.h"
|
2020-03-13 06:56:03 +00:00
|
|
|
#include "data/data_peer.h"
|
2020-03-24 08:58:47 +00:00
|
|
|
#include "data/data_chat_filters.h"
|
2020-03-13 06:56:03 +00:00
|
|
|
#include "history/history.h"
|
2020-03-10 12:41:12 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "window/window_session_controller.h"
|
|
|
|
#include "window/window_controller.h"
|
|
|
|
#include "ui/layers/generic_box.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
2020-03-13 06:56:03 +00:00
|
|
|
#include "ui/widgets/input_fields.h"
|
2020-03-10 12:41:12 +00:00
|
|
|
#include "ui/text/text_utilities.h"
|
2020-03-10 12:51:23 +00:00
|
|
|
#include "ui/wrap/slide_wrap.h"
|
2020-03-13 06:56:03 +00:00
|
|
|
#include "ui/painter.h"
|
2020-03-24 10:49:50 +00:00
|
|
|
#include "ui/filter_icons.h"
|
2020-03-10 12:41:12 +00:00
|
|
|
#include "settings/settings_common.h"
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "apiwrap.h"
|
2020-03-24 08:58:47 +00:00
|
|
|
#include "app.h"
|
2020-03-10 12:41:12 +00:00
|
|
|
#include "styles/style_settings.h"
|
|
|
|
#include "styles/style_layers.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
#include "styles/style_chat_helpers.h"
|
2020-03-13 06:56:03 +00:00
|
|
|
#include "styles/style_window.h"
|
2020-03-10 12:41:12 +00:00
|
|
|
|
2020-03-24 08:58:47 +00:00
|
|
|
namespace Settings {
|
2020-03-10 12:41:12 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kRefreshSuggestedTimeout = 7200 * crl::time(1000);
|
|
|
|
constexpr auto kFiltersLimit = 10;
|
2020-03-13 06:56:03 +00:00
|
|
|
|
|
|
|
using Flag = Data::ChatFilter::Flag;
|
|
|
|
using Flags = Data::ChatFilter::Flags;
|
2020-03-10 12:41:12 +00:00
|
|
|
|
|
|
|
class FilterRowButton final : public Ui::RippleButton {
|
|
|
|
public:
|
|
|
|
FilterRowButton(
|
|
|
|
not_null<QWidget*> parent,
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const Data::ChatFilter &filter);
|
|
|
|
FilterRowButton(
|
|
|
|
not_null<QWidget*> parent,
|
|
|
|
const Data::ChatFilter &filter,
|
|
|
|
const QString &description);
|
|
|
|
|
|
|
|
void setRemoved(bool removed);
|
2020-03-13 06:56:03 +00:00
|
|
|
void updateData(const Data::ChatFilter &filter);
|
2020-03-25 13:28:59 +00:00
|
|
|
void updateCount(const Data::ChatFilter &filter);
|
2020-03-10 12:41:12 +00:00
|
|
|
|
|
|
|
[[nodiscard]] rpl::producer<> removeRequests() const;
|
|
|
|
[[nodiscard]] rpl::producer<> restoreRequests() const;
|
|
|
|
[[nodiscard]] rpl::producer<> addRequests() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class State {
|
|
|
|
Suggested,
|
|
|
|
Removed,
|
|
|
|
Normal,
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterRowButton(
|
|
|
|
not_null<QWidget*> parent,
|
2020-03-13 06:56:03 +00:00
|
|
|
Main::Session *session,
|
2020-03-10 12:41:12 +00:00
|
|
|
const Data::ChatFilter &filter,
|
|
|
|
const QString &description,
|
|
|
|
State state);
|
|
|
|
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
|
|
|
|
void setup(const Data::ChatFilter &filter, const QString &status);
|
|
|
|
void setState(State state, bool force = false);
|
|
|
|
void updateButtonsVisibility();
|
|
|
|
|
2020-03-13 06:56:03 +00:00
|
|
|
Main::Session *_session = nullptr;
|
|
|
|
|
2020-03-10 12:41:12 +00:00
|
|
|
Ui::IconButton _remove;
|
|
|
|
Ui::RoundButton _restore;
|
|
|
|
Ui::RoundButton _add;
|
|
|
|
|
|
|
|
Ui::Text::String _title;
|
|
|
|
QString _status;
|
2020-03-24 10:49:50 +00:00
|
|
|
Ui::FilterIcon _icon = Ui::FilterIcon();
|
2020-03-10 12:41:12 +00:00
|
|
|
|
|
|
|
State _state = State::Normal;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2020-03-24 08:58:47 +00:00
|
|
|
struct FilterRow {
|
|
|
|
not_null<FilterRowButton*> button;
|
|
|
|
Data::ChatFilter filter;
|
|
|
|
bool removed = false;
|
|
|
|
bool added = false;
|
2020-03-25 13:28:59 +00:00
|
|
|
bool postponedCountUpdate = false;
|
2020-03-24 08:58:47 +00:00
|
|
|
};
|
|
|
|
|
2020-03-10 12:41:12 +00:00
|
|
|
[[nodiscard]] int CountFilterChats(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const Data::ChatFilter &filter) {
|
|
|
|
auto result = 0;
|
|
|
|
const auto addList = [&](not_null<Dialogs::MainList*> list) {
|
|
|
|
for (const auto &entry : list->indexed()->all()) {
|
|
|
|
if (const auto history = entry->history()) {
|
|
|
|
if (filter.contains(history)) {
|
|
|
|
++result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
addList(session->data().chatsList());
|
|
|
|
const auto folderId = Data::Folder::kId;
|
|
|
|
if (const auto folder = session->data().folderLoaded(folderId)) {
|
|
|
|
addList(folder->chatsList());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] int ComputeCount(
|
|
|
|
not_null<Main::Session*> session,
|
2020-03-13 06:56:03 +00:00
|
|
|
const Data::ChatFilter &filter,
|
|
|
|
bool check = false) {
|
2020-03-10 12:41:12 +00:00
|
|
|
const auto &list = session->data().chatsFilters().list();
|
|
|
|
const auto id = filter.id();
|
2020-03-13 06:56:03 +00:00
|
|
|
const auto i = ranges::find(list, id, &Data::ChatFilter::id);
|
|
|
|
if (i != end(list)
|
|
|
|
&& (!check
|
|
|
|
|| (i->flags() == filter.flags()
|
|
|
|
&& i->always() == filter.always()
|
|
|
|
&& i->never() == filter.never()))) {
|
2020-03-10 12:41:12 +00:00
|
|
|
const auto chats = session->data().chatsFilters().chatsList(id);
|
|
|
|
return chats->indexed()->size();
|
|
|
|
}
|
|
|
|
return CountFilterChats(session, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] QString ComputeCountString(
|
|
|
|
not_null<Main::Session*> session,
|
2020-03-13 06:56:03 +00:00
|
|
|
const Data::ChatFilter &filter,
|
|
|
|
bool check = false) {
|
|
|
|
const auto count = ComputeCount(session, filter, check);
|
2020-03-10 12:41:12 +00:00
|
|
|
return count
|
|
|
|
? tr::lng_filters_chats_count(tr::now, lt_count_short, count)
|
|
|
|
: tr::lng_filters_no_chats(tr::now);
|
|
|
|
}
|
|
|
|
|
|
|
|
FilterRowButton::FilterRowButton(
|
|
|
|
not_null<QWidget*> parent,
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const Data::ChatFilter &filter)
|
|
|
|
: FilterRowButton(
|
|
|
|
parent,
|
2020-03-13 06:56:03 +00:00
|
|
|
session,
|
2020-03-10 12:41:12 +00:00
|
|
|
filter,
|
|
|
|
ComputeCountString(session, filter),
|
|
|
|
State::Normal) {
|
|
|
|
}
|
|
|
|
|
|
|
|
FilterRowButton::FilterRowButton(
|
|
|
|
not_null<QWidget*> parent,
|
|
|
|
const Data::ChatFilter &filter,
|
|
|
|
const QString &description)
|
2020-03-13 06:56:03 +00:00
|
|
|
: FilterRowButton(parent, nullptr, filter, description, State::Suggested) {
|
2020-03-10 12:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FilterRowButton::FilterRowButton(
|
|
|
|
not_null<QWidget*> parent,
|
2020-03-13 06:56:03 +00:00
|
|
|
Main::Session *session,
|
2020-03-10 12:41:12 +00:00
|
|
|
const Data::ChatFilter &filter,
|
|
|
|
const QString &status,
|
|
|
|
State state)
|
|
|
|
: RippleButton(parent, st::defaultRippleAnimation)
|
2020-03-13 06:56:03 +00:00
|
|
|
, _session(session)
|
2020-03-10 12:41:12 +00:00
|
|
|
, _remove(this, st::filtersRemove)
|
|
|
|
, _restore(this, tr::lng_filters_restore(), st::stickersUndoRemove)
|
|
|
|
, _add(this, tr::lng_filters_recommended_add(), st::stickersTrendingAdd)
|
|
|
|
, _state(state) {
|
|
|
|
setup(filter, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterRowButton::setRemoved(bool removed) {
|
|
|
|
setState(removed ? State::Removed : State::Normal);
|
|
|
|
}
|
|
|
|
|
2020-03-13 06:56:03 +00:00
|
|
|
void FilterRowButton::updateData(const Data::ChatFilter &filter) {
|
|
|
|
Expects(_session != nullptr);
|
|
|
|
|
|
|
|
_title.setText(st::contactsNameStyle, filter.title());
|
2020-03-24 10:49:50 +00:00
|
|
|
_icon = Ui::ComputeFilterIcon(filter);
|
2020-03-25 13:28:59 +00:00
|
|
|
updateCount(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterRowButton::updateCount(const Data::ChatFilter &filter) {
|
|
|
|
_status = ComputeCountString(_session, filter, true);
|
2020-03-13 06:56:03 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:41:12 +00:00
|
|
|
void FilterRowButton::setState(State state, bool force) {
|
|
|
|
if (!force && _state == state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_state = state;
|
|
|
|
setPointerCursor(_state == State::Normal);
|
|
|
|
setDisabled(_state != State::Normal);
|
|
|
|
updateButtonsVisibility();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterRowButton::setup(
|
|
|
|
const Data::ChatFilter &filter,
|
|
|
|
const QString &status) {
|
|
|
|
resize(width(), st::defaultPeerListItem.height);
|
|
|
|
|
|
|
|
_title.setText(st::contactsNameStyle, filter.title());
|
|
|
|
_status = status;
|
2020-03-24 10:49:50 +00:00
|
|
|
_icon = Ui::ComputeFilterIcon(filter);
|
2020-03-10 12:41:12 +00:00
|
|
|
|
|
|
|
setState(_state, true);
|
|
|
|
|
|
|
|
sizeValue(
|
|
|
|
) | rpl::start_with_next([=](QSize size) {
|
|
|
|
const auto right = st::contactsPadding.right()
|
|
|
|
+ st::contactsCheckPosition.x();
|
|
|
|
const auto width = size.width();
|
|
|
|
const auto height = size.height();
|
|
|
|
_restore.moveToRight(right, (height - _restore.height()) / 2, width);
|
|
|
|
_add.moveToRight(right, (height - _add.height()) / 2, width);
|
|
|
|
const auto skipped = right - st::stickersRemoveSkip;
|
|
|
|
_remove.moveToRight(skipped, (height - _remove.height()) / 2, width);
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterRowButton::updateButtonsVisibility() {
|
|
|
|
_remove.setVisible(_state == State::Normal);
|
|
|
|
_restore.setVisible(_state == State::Removed);
|
|
|
|
_add.setVisible(_state == State::Suggested);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> FilterRowButton::removeRequests() const {
|
|
|
|
return _remove.clicks() | rpl::map([] { return rpl::empty_value(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> FilterRowButton::restoreRequests() const {
|
|
|
|
return _restore.clicks() | rpl::map([] { return rpl::empty_value(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> FilterRowButton::addRequests() const {
|
|
|
|
return _add.clicks() | rpl::map([] { return rpl::empty_value(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterRowButton::paintEvent(QPaintEvent *e) {
|
|
|
|
auto p = Painter(this);
|
|
|
|
|
2020-03-24 10:49:50 +00:00
|
|
|
const auto over = isOver() || isDown();
|
2020-03-10 12:41:12 +00:00
|
|
|
if (_state == State::Normal) {
|
2020-03-24 10:49:50 +00:00
|
|
|
if (over) {
|
2020-03-10 12:41:12 +00:00
|
|
|
p.fillRect(e->rect(), st::windowBgOver);
|
|
|
|
}
|
|
|
|
RippleButton::paintRipple(p, 0, 0);
|
|
|
|
} else if (_state == State::Removed) {
|
|
|
|
p.setOpacity(st::stickersRowDisabledOpacity);
|
|
|
|
}
|
|
|
|
|
2020-03-24 10:49:50 +00:00
|
|
|
const auto left = (_state == State::Suggested)
|
|
|
|
? st::settingsSubsectionTitlePadding.left()
|
|
|
|
: st::settingsFilterIconSkip;
|
2020-03-10 12:41:12 +00:00
|
|
|
const auto buttonsLeft = std::min(
|
|
|
|
_add.x(),
|
|
|
|
std::min(_remove.x(), _restore.x()));
|
|
|
|
const auto availableWidth = buttonsLeft - left;
|
|
|
|
|
|
|
|
p.setPen(st::contactsNameFg);
|
|
|
|
_title.drawLeftElided(
|
|
|
|
p,
|
|
|
|
left,
|
|
|
|
st::contactsPadding.top() + st::contactsNameTop,
|
|
|
|
availableWidth,
|
|
|
|
width());
|
|
|
|
|
|
|
|
p.setFont(st::contactsStatusFont);
|
|
|
|
p.setPen(st::contactsStatusFg);
|
|
|
|
p.drawTextLeft(
|
|
|
|
left,
|
|
|
|
st::contactsPadding.top() + st::contactsStatusTop,
|
|
|
|
width(),
|
|
|
|
_status);
|
2020-03-24 10:49:50 +00:00
|
|
|
|
|
|
|
if (_state != State::Suggested) {
|
|
|
|
const auto icon = Ui::LookupFilterIcon(_icon).normal;
|
|
|
|
icon->paint(
|
|
|
|
p,
|
|
|
|
st::settingsFilterIconLeft,
|
|
|
|
(height() - icon->height()) / 2,
|
|
|
|
width(),
|
|
|
|
(over ? st::menuIconFgOver : st::menuIconFg)->c);
|
|
|
|
}
|
2020-03-10 12:41:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 08:58:47 +00:00
|
|
|
[[nodiscard]] Fn<void()> SetupFoldersContent(
|
|
|
|
not_null<Window::SessionController*> controller,
|
|
|
|
not_null<Ui::VerticalLayout*> container) {
|
|
|
|
auto &lifetime = container->lifetime();
|
2020-03-10 12:41:12 +00:00
|
|
|
|
2020-03-24 08:58:47 +00:00
|
|
|
const auto session = &controller->session();
|
|
|
|
AddSkip(container, st::settingsSectionSkip);
|
|
|
|
AddSubsectionTitle(container, tr::lng_filters_subtitle());
|
2020-03-10 12:41:12 +00:00
|
|
|
|
2020-03-24 08:58:47 +00:00
|
|
|
const auto rows = lifetime.make_state<std::vector<FilterRow>>();
|
|
|
|
const auto rowsCount = lifetime.make_state<rpl::variable<int>>();
|
2020-03-10 12:41:12 +00:00
|
|
|
const auto find = [=](not_null<FilterRowButton*> button) {
|
|
|
|
const auto i = ranges::find(*rows, button, &FilterRow::button);
|
|
|
|
Assert(i != end(*rows));
|
|
|
|
return &*i;
|
|
|
|
};
|
2020-03-13 06:56:03 +00:00
|
|
|
const auto showLimitReached = [=] {
|
2020-03-10 12:41:12 +00:00
|
|
|
const auto removed = ranges::count_if(*rows, &FilterRow::removed);
|
2020-03-13 06:56:03 +00:00
|
|
|
if (rows->size() < kFiltersLimit + removed) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-24 08:58:47 +00:00
|
|
|
controller->window().showToast(tr::lng_filters_limit(tr::now));
|
2020-03-13 06:56:03 +00:00
|
|
|
return true;
|
2020-03-10 12:41:12 +00:00
|
|
|
};
|
2020-03-25 13:28:59 +00:00
|
|
|
const auto wrap = container->add(object_ptr<Ui::VerticalLayout>(
|
|
|
|
container));
|
2020-03-10 12:41:12 +00:00
|
|
|
const auto addFilter = [=](const Data::ChatFilter &filter) {
|
|
|
|
const auto button = wrap->add(
|
|
|
|
object_ptr<FilterRowButton>(wrap, session, filter));
|
|
|
|
button->removeRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
button->setRemoved(true);
|
|
|
|
find(button)->removed = true;
|
|
|
|
}, button->lifetime());
|
|
|
|
button->restoreRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
2020-03-13 06:56:03 +00:00
|
|
|
if (showLimitReached()) {
|
|
|
|
return;
|
2020-03-10 12:41:12 +00:00
|
|
|
}
|
2020-03-13 06:56:03 +00:00
|
|
|
button->setRemoved(false);
|
|
|
|
find(button)->removed = false;
|
2020-03-10 12:41:12 +00:00
|
|
|
}, button->lifetime());
|
2020-03-13 06:56:03 +00:00
|
|
|
button->setClickedCallback([=] {
|
|
|
|
const auto found = find(button);
|
|
|
|
if (found->removed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto doneCallback = [=](const Data::ChatFilter &result) {
|
|
|
|
find(button)->filter = result;
|
|
|
|
button->updateData(result);
|
|
|
|
};
|
2020-03-24 08:58:47 +00:00
|
|
|
controller->window().show(Box(
|
2020-03-13 11:07:27 +00:00
|
|
|
EditFilterBox,
|
2020-03-24 08:58:47 +00:00
|
|
|
controller,
|
2020-03-13 06:56:03 +00:00
|
|
|
found->filter,
|
|
|
|
crl::guard(button, doneCallback)));
|
|
|
|
});
|
2020-03-10 12:41:12 +00:00
|
|
|
rows->push_back({ button, filter });
|
2020-03-18 13:00:53 +00:00
|
|
|
*rowsCount = rows->size();
|
2020-03-18 10:07:11 +00:00
|
|
|
|
2020-03-25 13:28:59 +00:00
|
|
|
const auto filters = &controller->session().data().chatsFilters();
|
|
|
|
const auto id = filter.id();
|
|
|
|
if (ranges::contains(filters->list(), id, &Data::ChatFilter::id)) {
|
|
|
|
filters->chatsList(id)->fullSize().changes(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
const auto found = find(button);
|
|
|
|
if (found->postponedCountUpdate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
found->postponedCountUpdate = true;
|
|
|
|
Ui::PostponeCall(button, [=] {
|
|
|
|
const auto &list = filters->list();
|
|
|
|
const auto i = ranges::find(
|
|
|
|
list,
|
|
|
|
id,
|
|
|
|
&Data::ChatFilter::id);
|
|
|
|
if (i == end(list)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto found = find(button);
|
|
|
|
const auto &real = *i;
|
|
|
|
const auto &now = found->filter;
|
|
|
|
if ((i->flags() != found->filter.flags())
|
|
|
|
|| (i->always() != found->filter.always())
|
|
|
|
|| (i->never() != found->filter.never())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
button->updateCount(found->filter);
|
|
|
|
found->postponedCountUpdate = false;
|
|
|
|
});
|
|
|
|
}, button->lifetime());
|
|
|
|
}
|
|
|
|
|
2020-03-24 08:58:47 +00:00
|
|
|
wrap->resizeToWidth(container->width());
|
2020-03-10 12:41:12 +00:00
|
|
|
};
|
|
|
|
const auto &list = session->data().chatsFilters().list();
|
|
|
|
for (const auto &filter : list) {
|
|
|
|
addFilter(filter);
|
|
|
|
}
|
|
|
|
|
2020-03-13 06:56:03 +00:00
|
|
|
AddButton(
|
2020-03-24 08:58:47 +00:00
|
|
|
container,
|
2020-03-10 12:41:12 +00:00
|
|
|
tr::lng_filters_create() | Ui::Text::ToUpper(),
|
2020-03-13 06:56:03 +00:00
|
|
|
st::settingsUpdate
|
|
|
|
)->setClickedCallback([=] {
|
|
|
|
if (showLimitReached()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto doneCallback = [=](const Data::ChatFilter &result) {
|
|
|
|
addFilter(result);
|
|
|
|
};
|
2020-03-24 08:58:47 +00:00
|
|
|
controller->window().show(Box(
|
2020-03-13 11:07:27 +00:00
|
|
|
EditFilterBox,
|
2020-03-24 08:58:47 +00:00
|
|
|
controller,
|
2020-03-13 06:56:03 +00:00
|
|
|
Data::ChatFilter(),
|
2020-03-24 08:58:47 +00:00
|
|
|
crl::guard(container, doneCallback)));
|
2020-03-13 06:56:03 +00:00
|
|
|
});
|
2020-03-24 08:58:47 +00:00
|
|
|
AddSkip(container);
|
|
|
|
const auto emptyAbout = container->add(
|
2020-03-10 12:51:23 +00:00
|
|
|
object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
|
2020-03-24 08:58:47 +00:00
|
|
|
container,
|
2020-03-10 12:41:12 +00:00
|
|
|
object_ptr<Ui::FlatLabel>(
|
2020-03-24 08:58:47 +00:00
|
|
|
container,
|
2020-03-10 12:41:12 +00:00
|
|
|
tr::lng_filters_about(),
|
|
|
|
st::boxDividerLabel),
|
2020-03-10 12:51:23 +00:00
|
|
|
st::settingsDividerLabelPadding)
|
|
|
|
)->setDuration(0);
|
2020-03-24 08:58:47 +00:00
|
|
|
const auto nonEmptyAbout = container->add(
|
2020-03-10 12:51:23 +00:00
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
2020-03-24 08:58:47 +00:00
|
|
|
container,
|
|
|
|
object_ptr<Ui::VerticalLayout>(container))
|
2020-03-10 12:51:23 +00:00
|
|
|
)->setDuration(0);
|
|
|
|
const auto aboutRows = nonEmptyAbout->entity();
|
2020-03-13 06:56:03 +00:00
|
|
|
AddDividerText(aboutRows, tr::lng_filters_about());
|
|
|
|
AddSkip(aboutRows);
|
|
|
|
AddSubsectionTitle(aboutRows, tr::lng_filters_recommended());
|
2020-03-10 12:51:23 +00:00
|
|
|
|
2020-03-24 08:58:47 +00:00
|
|
|
const auto changed = lifetime.make_state<bool>();
|
|
|
|
const auto suggested = lifetime.make_state<rpl::variable<int>>();
|
|
|
|
rpl::single(
|
|
|
|
rpl::empty_value()
|
|
|
|
) | rpl::then(
|
|
|
|
session->data().chatsFilters().suggestedUpdated()
|
|
|
|
) | rpl::map([=] {
|
|
|
|
return session->data().chatsFilters().suggestedFilters();
|
|
|
|
}) | rpl::filter([=](const std::vector<Data::SuggestedFilter> &list) {
|
|
|
|
return !list.empty();
|
|
|
|
}) | rpl::take(
|
|
|
|
1
|
|
|
|
) | rpl::start_with_next([=](
|
|
|
|
const std::vector<Data::SuggestedFilter> &suggestions) {
|
|
|
|
for (const auto &suggestion : suggestions) {
|
|
|
|
const auto &filter = suggestion.filter;
|
|
|
|
if (ranges::contains(*rows, filter, &FilterRow::filter)) {
|
|
|
|
continue;
|
2020-03-13 06:56:03 +00:00
|
|
|
}
|
2020-03-24 08:58:47 +00:00
|
|
|
*suggested = suggested->current() + 1;
|
|
|
|
const auto button = aboutRows->add(object_ptr<FilterRowButton>(
|
|
|
|
aboutRows,
|
|
|
|
filter,
|
|
|
|
suggestion.description));
|
|
|
|
button->addRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (showLimitReached()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addFilter(filter);
|
|
|
|
*suggested = suggested->current() - 1;
|
|
|
|
delete button;
|
|
|
|
}, button->lifetime());
|
|
|
|
}
|
|
|
|
aboutRows->resizeToWidth(container->width());
|
|
|
|
AddSkip(aboutRows, st::settingsSectionSkip);
|
|
|
|
}, aboutRows->lifetime());
|
2020-03-10 12:41:12 +00:00
|
|
|
|
2020-03-10 12:51:23 +00:00
|
|
|
using namespace rpl::mappers;
|
2020-03-18 13:00:53 +00:00
|
|
|
auto showSuggestions = rpl::combine(
|
|
|
|
suggested->value(),
|
|
|
|
rowsCount->value()
|
|
|
|
) | rpl::map(_1 > 0 && _2 < kFiltersLimit);
|
|
|
|
emptyAbout->toggleOn(rpl::duplicate(
|
|
|
|
showSuggestions
|
|
|
|
) | rpl::map(!_1));
|
|
|
|
nonEmptyAbout->toggleOn(std::move(showSuggestions));
|
2020-03-10 12:51:23 +00:00
|
|
|
|
2020-03-10 12:41:12 +00:00
|
|
|
const auto prepareGoodIdsForNewFilters = [=] {
|
|
|
|
const auto &list = session->data().chatsFilters().list();
|
|
|
|
|
2020-03-18 10:07:11 +00:00
|
|
|
auto localId = 1;
|
2020-03-10 12:41:12 +00:00
|
|
|
const auto chooseNextId = [&] {
|
2020-03-18 10:07:11 +00:00
|
|
|
++localId;
|
2020-03-10 12:41:12 +00:00
|
|
|
while (ranges::contains(list, localId, &Data::ChatFilter::id)) {
|
|
|
|
++localId;
|
|
|
|
}
|
|
|
|
return localId;
|
|
|
|
};
|
2020-03-18 10:25:41 +00:00
|
|
|
auto result = base::flat_map<not_null<FilterRowButton*>, FilterId>();
|
2020-03-10 12:41:12 +00:00
|
|
|
for (auto &row : *rows) {
|
|
|
|
const auto id = row.filter.id();
|
|
|
|
if (row.removed) {
|
|
|
|
continue;
|
|
|
|
} else if (!ranges::contains(list, id, &Data::ChatFilter::id)) {
|
2020-03-18 10:25:41 +00:00
|
|
|
result.emplace(row.button, chooseNextId());
|
2020-03-10 12:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2020-03-24 08:58:47 +00:00
|
|
|
return [=] {
|
2020-03-10 12:41:12 +00:00
|
|
|
auto ids = prepareGoodIdsForNewFilters();
|
|
|
|
|
2020-03-13 06:56:03 +00:00
|
|
|
using Requests = std::vector<MTPmessages_UpdateDialogFilter>;
|
|
|
|
auto addRequests = Requests(), removeRequests = Requests();
|
2020-03-10 12:41:12 +00:00
|
|
|
auto &realFilters = session->data().chatsFilters();
|
|
|
|
const auto &list = realFilters.list();
|
2020-03-20 12:48:52 +00:00
|
|
|
auto order = std::vector<FilterId>();
|
|
|
|
order.reserve(rows->size());
|
2020-03-10 12:41:12 +00:00
|
|
|
for (const auto &row : *rows) {
|
|
|
|
const auto id = row.filter.id();
|
|
|
|
const auto removed = row.removed;
|
2020-03-13 06:56:03 +00:00
|
|
|
const auto i = ranges::find(list, id, &Data::ChatFilter::id);
|
|
|
|
if (removed && i == end(list)) {
|
|
|
|
continue;
|
|
|
|
} else if (!removed && i != end(list) && *i == row.filter) {
|
2020-03-20 12:48:52 +00:00
|
|
|
order.push_back(id);
|
2020-03-10 12:41:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-03-18 10:25:41 +00:00
|
|
|
const auto newId = ids.take(row.button).value_or(id);
|
2020-03-18 09:19:58 +00:00
|
|
|
const auto tl = removed
|
|
|
|
? MTPDialogFilter()
|
|
|
|
: row.filter.tl(newId);
|
2020-03-10 12:41:12 +00:00
|
|
|
const auto request = MTPmessages_UpdateDialogFilter(
|
|
|
|
MTP_flags(removed
|
|
|
|
? MTPmessages_UpdateDialogFilter::Flag(0)
|
|
|
|
: MTPmessages_UpdateDialogFilter::Flag::f_filter),
|
|
|
|
MTP_int(newId),
|
|
|
|
tl);
|
|
|
|
if (removed) {
|
2020-03-13 06:56:03 +00:00
|
|
|
removeRequests.push_back(request);
|
2020-03-10 12:41:12 +00:00
|
|
|
} else {
|
2020-03-13 06:56:03 +00:00
|
|
|
addRequests.push_back(request);
|
2020-03-20 12:48:52 +00:00
|
|
|
order.push_back(newId);
|
2020-03-10 12:41:12 +00:00
|
|
|
}
|
|
|
|
realFilters.apply(MTP_updateDialogFilter(
|
|
|
|
MTP_flags(removed
|
|
|
|
? MTPDupdateDialogFilter::Flag(0)
|
|
|
|
: MTPDupdateDialogFilter::Flag::f_filter),
|
|
|
|
MTP_int(newId),
|
|
|
|
tl));
|
|
|
|
}
|
|
|
|
auto previousId = mtpRequestId(0);
|
2020-03-13 06:56:03 +00:00
|
|
|
auto &&requests = ranges::view::concat(removeRequests, addRequests);
|
2020-03-10 12:41:12 +00:00
|
|
|
for (auto &request : requests) {
|
|
|
|
previousId = session->api().request(
|
|
|
|
std::move(request)
|
|
|
|
).afterRequest(previousId).send();
|
|
|
|
}
|
2020-03-20 12:48:52 +00:00
|
|
|
if (!order.empty() && !addRequests.empty()) {
|
|
|
|
realFilters.saveOrder(order, previousId);
|
2020-03-10 12:41:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-03-24 08:58:47 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Folders::Folders(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Window::SessionController*> controller)
|
|
|
|
: Section(parent) {
|
|
|
|
setupContent(controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
Folders::~Folders() {
|
|
|
|
if (!App::quitting()) {
|
|
|
|
_save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Folders::setupContent(not_null<Window::SessionController*> controller) {
|
|
|
|
controller->session().data().chatsFilters().requestSuggested();
|
|
|
|
|
|
|
|
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
|
|
|
|
|
|
|
|
_save = SetupFoldersContent(controller, content);
|
|
|
|
|
|
|
|
Ui::ResizeFitChild(this, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Settings
|