From fdabdc36265f2471c71480f544d6b020408141a1 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 25 Mar 2020 17:28:59 +0400 Subject: [PATCH] Refresh filter chats count in filter settings. --- Telegram/SourceFiles/data/data_folder.cpp | 17 +++++-- Telegram/SourceFiles/data/data_folder.h | 5 ++- .../SourceFiles/settings/settings_folders.cpp | 44 ++++++++++++++++++- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/Telegram/SourceFiles/data/data_folder.cpp b/Telegram/SourceFiles/data/data_folder.cpp index 0d07bd833..b7222e657 100644 --- a/Telegram/SourceFiles/data/data_folder.cpp +++ b/Telegram/SourceFiles/data/data_folder.cpp @@ -67,7 +67,7 @@ Folder::Folder(not_null owner, FolderId id) for (const auto history : _lastHistories) { if (history->peer == update.peer) { ++_chatListViewVersion; - updateChatListEntry(); + updateChatListEntryPostponed(); return; } } @@ -81,15 +81,26 @@ Folder::Folder(not_null owner, FolderId id) }) | rpl::start_with_next([=](const Dialogs::UnreadState &old) { ++_chatListViewVersion; notifyUnreadStateChange(old); - updateChatListEntry(); + updateChatListEntryPostponed(); }, _lifetime); _chatsList.fullSize().changes( ) | rpl::start_with_next([=] { - updateChatListEntry(); + updateChatListEntryPostponed(); }, _lifetime); } +void Folder::updateChatListEntryPostponed() { + if (_updateChatListEntryPostponed) { + return; + } + _updateChatListEntryPostponed = true; + Ui::PostponeCall(this, [=] { + updateChatListEntry(); + _updateChatListEntryPostponed = false; + }); +} + FolderId Folder::id() const { return _id; } diff --git a/Telegram/SourceFiles/data/data_folder.h b/Telegram/SourceFiles/data/data_folder.h index cba736f97..eece5182f 100644 --- a/Telegram/SourceFiles/data/data_folder.h +++ b/Telegram/SourceFiles/data/data_folder.h @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "dialogs/dialogs_entry.h" #include "dialogs/dialogs_main_list.h" #include "data/data_messages.h" +#include "base/weak_ptr.h" class ChannelData; @@ -24,7 +25,7 @@ class Folder; //MessagePosition FeedPositionFromMTP(const MTPFeedPosition &position); // #feed -class Folder final : public Dialogs::Entry { +class Folder final : public Dialogs::Entry, public base::has_weak_ptr { public: static constexpr auto kId = 1; @@ -84,6 +85,7 @@ private: void computeChatListMessage(); void reorderLastHistories(); + void updateChatListEntryPostponed(); void paintUserpic( Painter &p, @@ -103,6 +105,7 @@ private: std::vector> _lastHistories; HistoryItem *_chatListMessage = nullptr; uint32 _chatListViewVersion = 0; + bool _updateChatListEntryPostponed = false; //rpl::variable _unreadPosition; rpl::lifetime _lifetime; diff --git a/Telegram/SourceFiles/settings/settings_folders.cpp b/Telegram/SourceFiles/settings/settings_folders.cpp index a45467266..9331f89a7 100644 --- a/Telegram/SourceFiles/settings/settings_folders.cpp +++ b/Telegram/SourceFiles/settings/settings_folders.cpp @@ -56,6 +56,7 @@ public: void setRemoved(bool removed); void updateData(const Data::ChatFilter &filter); + void updateCount(const Data::ChatFilter &filter); [[nodiscard]] rpl::producer<> removeRequests() const; [[nodiscard]] rpl::producer<> restoreRequests() const; @@ -100,6 +101,7 @@ struct FilterRow { Data::ChatFilter filter; bool removed = false; bool added = false; + bool postponedCountUpdate = false; }; [[nodiscard]] int CountFilterChats( @@ -193,8 +195,12 @@ void FilterRowButton::updateData(const Data::ChatFilter &filter) { Expects(_session != nullptr); _title.setText(st::contactsNameStyle, filter.title()); - _status = ComputeCountString(_session, filter, true); _icon = Ui::ComputeFilterIcon(filter); + updateCount(filter); +} + +void FilterRowButton::updateCount(const Data::ChatFilter &filter) { + _status = ComputeCountString(_session, filter, true); update(); } @@ -323,7 +329,8 @@ void FilterRowButton::paintEvent(QPaintEvent *e) { controller->window().showToast(tr::lng_filters_limit(tr::now)); return true; }; - const auto wrap = container->add(object_ptr(container)); + const auto wrap = container->add(object_ptr( + container)); const auto addFilter = [=](const Data::ChatFilter &filter) { const auto button = wrap->add( object_ptr(wrap, session, filter)); @@ -358,6 +365,39 @@ void FilterRowButton::paintEvent(QPaintEvent *e) { rows->push_back({ button, filter }); *rowsCount = rows->size(); + 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()); + } + wrap->resizeToWidth(container->width()); }; const auto &list = session->data().chatsFilters().list();