Improve filters reordering.

This commit is contained in:
John Preston 2020-03-20 16:48:52 +04:00
parent 18805a5ef8
commit b8684af537
8 changed files with 40 additions and 32 deletions

View File

@ -32,20 +32,4 @@ void SaveNewFilterPinned(
} }
void SaveNewOrder(
not_null<Main::Session*> session,
const std::vector<FilterId> &order) {
auto &filters = session->data().chatsFilters();
auto ids = QVector<MTPint>();
ids.reserve(order.size());
for (const auto id : order) {
ids.push_back(MTP_int(id));
}
const auto wrapped = MTP_vector<MTPint>(ids);
filters.apply(MTP_updateDialogFilterOrder(wrapped));
session->api().request(MTPmessages_UpdateDialogFiltersOrder(
wrapped
)).send();
}
} // namespace Api } // namespace Api

View File

@ -17,8 +17,4 @@ void SaveNewFilterPinned(
not_null<Main::Session*> session, not_null<Main::Session*> session,
FilterId filterId); FilterId filterId);
void SaveNewOrder(
not_null<Main::Session*> session,
const std::vector<FilterId> &order);
} // namespace Api } // namespace Api

View File

@ -501,7 +501,8 @@ void ManageFiltersPrepare::SetupBox(
auto addRequests = Requests(), removeRequests = Requests(); auto addRequests = Requests(), removeRequests = Requests();
auto &realFilters = session->data().chatsFilters(); auto &realFilters = session->data().chatsFilters();
const auto &list = realFilters.list(); const auto &list = realFilters.list();
auto order = QVector<MTPint>(); auto order = std::vector<FilterId>();
order.reserve(rows->size());
for (const auto &row : *rows) { for (const auto &row : *rows) {
const auto id = row.filter.id(); const auto id = row.filter.id();
const auto removed = row.removed; const auto removed = row.removed;
@ -509,7 +510,7 @@ void ManageFiltersPrepare::SetupBox(
if (removed && i == end(list)) { if (removed && i == end(list)) {
continue; continue;
} else if (!removed && i != end(list) && *i == row.filter) { } else if (!removed && i != end(list) && *i == row.filter) {
order.push_back(MTP_int(id)); order.push_back(id);
continue; continue;
} }
const auto newId = ids.take(row.button).value_or(id); const auto newId = ids.take(row.button).value_or(id);
@ -526,7 +527,7 @@ void ManageFiltersPrepare::SetupBox(
removeRequests.push_back(request); removeRequests.push_back(request);
} else { } else {
addRequests.push_back(request); addRequests.push_back(request);
order.push_back(MTP_int(newId)); order.push_back(newId);
} }
realFilters.apply(MTP_updateDialogFilter( realFilters.apply(MTP_updateDialogFilter(
MTP_flags(removed MTP_flags(removed
@ -542,12 +543,8 @@ void ManageFiltersPrepare::SetupBox(
std::move(request) std::move(request)
).afterRequest(previousId).send(); ).afterRequest(previousId).send();
} }
if (!order.isEmpty() && !addRequests.empty()) { if (!order.empty() && !addRequests.empty()) {
realFilters.apply( realFilters.saveOrder(order, previousId);
MTP_updateDialogFilterOrder(MTP_vector(order)));
session->api().request(MTPmessages_UpdateDialogFiltersOrder(
MTP_vector(order)
)).afterRequest(previousId).send();
} }
box->closeBox(); box->closeBox();
}; };

View File

@ -451,6 +451,28 @@ const ChatFilter &ChatFilters::applyUpdatedPinned(
return *i; return *i;
} }
void ChatFilters::saveOrder(
const std::vector<FilterId> &order,
mtpRequestId after) {
if (after) {
_saveOrderAfterId = after;
}
const auto api = &_owner->session().api();
api->request(_saveOrderRequestId).cancel();
auto ids = QVector<MTPint>();
ids.reserve(order.size());
for (const auto id : order) {
ids.push_back(MTP_int(id));
}
const auto wrapped = MTP_vector<MTPint>(ids);
apply(MTP_updateDialogFilterOrder(wrapped));
_saveOrderRequestId = api->request(MTPmessages_UpdateDialogFiltersOrder(
wrapped
)).afterRequest(_saveOrderAfterId).send();
}
bool ChatFilters::archiveNeeded() const { bool ChatFilters::archiveNeeded() const {
for (const auto &filter : _list) { for (const auto &filter : _list) {
if (!(filter.flags() & ChatFilter::Flag::NoArchived)) { if (!(filter.flags() & ChatFilter::Flag::NoArchived)) {

View File

@ -106,6 +106,9 @@ public:
const ChatFilter &applyUpdatedPinned( const ChatFilter &applyUpdatedPinned(
FilterId id, FilterId id,
const std::vector<Dialogs::Key> &dialogs); const std::vector<Dialogs::Key> &dialogs);
void saveOrder(
const std::vector<FilterId> &order,
mtpRequestId after = 0);
[[nodiscard]] bool archiveNeeded() const; [[nodiscard]] bool archiveNeeded() const;
@ -123,6 +126,8 @@ private:
rpl::event_stream<> _listChanged; rpl::event_stream<> _listChanged;
rpl::event_stream<not_null<History*>> _refreshHistoryRequests; rpl::event_stream<not_null<History*>> _refreshHistoryRequests;
mtpRequestId _loadRequestId = 0; mtpRequestId _loadRequestId = 0;
mtpRequestId _saveOrderRequestId = 0;
mtpRequestId _saveOrderAfterId = 0;
bool _loaded = false; bool _loaded = false;
}; };

View File

@ -99,7 +99,7 @@ void FiltersMenu::setup() {
void FiltersMenu::refresh() { void FiltersMenu::refresh() {
const auto filters = &_session->session().data().chatsFilters(); const auto filters = &_session->session().data().chatsFilters();
if (filters->list().empty()) { if (filters->list().empty() || _ignoreRefresh) {
return; return;
} }
@ -220,7 +220,10 @@ void FiltersMenu::applyReorder(
&Data::ChatFilter::id &Data::ChatFilter::id
) | ranges::to_vector; ) | ranges::to_vector;
base::reorder(order, oldPosition, newPosition); base::reorder(order, oldPosition, newPosition);
Api::SaveNewOrder(&_session->session(), order);
_ignoreRefresh = true;
filters->saveOrder(order);
_ignoreRefresh = false;
} }
} // namespace Window } // namespace Window

View File

@ -57,6 +57,7 @@ private:
base::flat_map<FilterId, base::unique_qptr<Ui::SideBarButton>> _filters; base::flat_map<FilterId, base::unique_qptr<Ui::SideBarButton>> _filters;
FilterId _activeFilterId = 0; FilterId _activeFilterId = 0;
int _reordering = 0; int _reordering = 0;
bool _ignoreRefresh = false;
}; };

@ -1 +1 @@
Subproject commit b051948e6947b97394df9f0f8b24e8bf407e596c Subproject commit eaab3f9a8a5a1bc429449a9a40c0798f2203947b