From 73f6e602ac220c6d933eb96b502633cca08f2441 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Mon, 14 Mar 2022 09:24:52 +0100 Subject: [PATCH] fixup after revert --- resources/sql/db_update_sqlite_1_2.sql | 4 +- src/librssguard/database/databasequeries.cpp | 56 ++++++++++++-------- src/librssguard/gui/dialogs/formmain.cpp | 2 + src/librssguard/gui/feedsview.cpp | 6 +++ src/librssguard/gui/feedsview.h | 1 + 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/resources/sql/db_update_sqlite_1_2.sql b/resources/sql/db_update_sqlite_1_2.sql index f44563c70..32ededa85 100644 --- a/resources/sql/db_update_sqlite_1_2.sql +++ b/resources/sql/db_update_sqlite_1_2.sql @@ -21,8 +21,8 @@ CREATE TABLE Feeds ( FOREIGN KEY (account_id) REFERENCES Accounts (id) ON DELETE CASCADE ); -- ! -INSERT INTO Feeds (id, title, description, date_created, icon, category, source, update_type, update_interval, account_id, custom_id, custom_data) -SELECT id, title, description, date_created, icon, category, source, update_type, update_interval, account_id, custom_id, custom_data +INSERT INTO Feeds (id, ordr, title, description, date_created, icon, category, source, update_type, update_interval, account_id, custom_id, custom_data) +SELECT id, id, title, description, date_created, icon, category, source, update_type, update_interval, account_id, custom_id, custom_data FROM backup_Feeds; -- ! DROP TABLE backup_Feeds; diff --git a/src/librssguard/database/databasequeries.cpp b/src/librssguard/database/databasequeries.cpp index b4dd20383..df5d59e4a 100644 --- a/src/librssguard/database/databasequeries.cpp +++ b/src/librssguard/database/databasequeries.cpp @@ -2153,7 +2153,6 @@ void DatabaseQueries::moveItem(RootItem* item, bool move_top, bool move_bottom, break; case RootItem::Kind::ServiceRoot: - break; default: @@ -2162,33 +2161,22 @@ void DatabaseQueries::moveItem(RootItem* item, bool move_top, bool move_bottom, } void DatabaseQueries::moveFeed(Feed* feed, bool move_top, bool move_bottom, int move_index, const QSqlDatabase& db) { + auto neighbors = feed->parent()->childItems(); + int max_sort_order = boolinq::from(neighbors).select([](RootItem* it) { + return it->kind() == RootItem::Kind::Feed ? it->sortOrder() : 0; + }).max(); + if (feed->sortOrder() == move_index || /* Item is already sorted OK. */ (!move_top && !move_bottom && move_index < 0 ) || /* Order cannot be smaller than 0 if we do not move to begin/end. */ - (move_top && feed->sortOrder() == 0)) { /* Item is already on top. */ + (!move_top && !move_bottom && move_index > max_sort_order ) || /* Cannot move past biggest sort order. */ + (move_top && feed->sortOrder() == 0) || /* Item is already on top. */ + (move_bottom && feed->sortOrder() == max_sort_order) || /* Item is already on bottom. */ + max_sort_order <= 0) { /* We only have 1 item, nothing to sort. */ return; } QSqlQuery q(db); - q.prepare(QSL("SELECT MAX(ordr) FROM Feeds WHERE account_id = :account_id AND category = :category;")); - q.bindValue(QSL(":account_id"), feed->getParentServiceRoot()->accountId()); - q.bindValue(QSL(":category"), feed->parent()->id()); - - int max_sort_order; - - if (q.exec() && q.next()) { - max_sort_order = q.value(0).toInt(); - } - else { - throw ApplicationException(q.lastError().text()); - } - - if (max_sort_order == 0 || /* We only have 1 item, nothing to sort. */ - max_sort_order == feed->sortOrder() || /* Item is already sorted OK. */ - move_index > max_sort_order) { /* Cannot move past biggest sort order. */ - return; - } - if (move_top) { move_index = 0; } @@ -2216,6 +2204,32 @@ void DatabaseQueries::moveFeed(Feed* feed, bool move_top, bool move_bottom, int if (!q.exec()) { throw ApplicationException(q.lastError().text()); } + + q.prepare(QSL("UPDATE Feeds SET ordr = :ordr WHERE id = :id;")); + q.bindValue(QSL(":id"), feed->id()); + q.bindValue(QSL(":ordr"), move_index); + + if (!q.exec()) { + throw ApplicationException(q.lastError().text()); + } + + // Fix live sort orders. + if (feed->sortOrder() > move_index) { + boolinq::from(neighbors).where([=](RootItem* it) { + return it->kind() == RootItem::Kind::Feed && it->sortOrder() < move_high && it->sortOrder() >= move_low; + }).for_each([](RootItem* it) { + it->setSortOrder(it->sortOrder() + 1); + }); + } + else { + boolinq::from(neighbors).where([=](RootItem* it) { + return it->kind() == RootItem::Kind::Feed && it->sortOrder() > move_low && it->sortOrder() <= move_high; + }).for_each([](RootItem* it) { + it->setSortOrder(it->sortOrder() - 1); + }); + } + + feed->setSortOrder(move_index); } MessageFilter* DatabaseQueries::addMessageFilter(const QSqlDatabase& db, const QString& title, diff --git a/src/librssguard/gui/dialogs/formmain.cpp b/src/librssguard/gui/dialogs/formmain.cpp index 0965c4ed1..5705f11d4 100644 --- a/src/librssguard/gui/dialogs/formmain.cpp +++ b/src/librssguard/gui/dialogs/formmain.cpp @@ -899,6 +899,8 @@ void FormMain::createConnections() { }); connect(m_ui->m_actionFeedMoveUp, &QAction::triggered, tabWidget()->feedMessageViewer()->feedsView(), &FeedsView::moveSelectedItemUp); + connect(m_ui->m_actionFeedMoveDown, &QAction::triggered, + tabWidget()->feedMessageViewer()->feedsView(), &FeedsView::moveSelectedItemDown); } void FormMain::backupDatabaseSettings() { diff --git a/src/librssguard/gui/feedsview.cpp b/src/librssguard/gui/feedsview.cpp index 76373b37d..577ce0dcc 100644 --- a/src/librssguard/gui/feedsview.cpp +++ b/src/librssguard/gui/feedsview.cpp @@ -290,6 +290,12 @@ void FeedsView::deleteSelectedItem() { void FeedsView::moveSelectedItemUp() { m_sourceModel->changeSortOrder(selectedItem(), false, false, selectedItem()->sortOrder() - 1); + m_proxyModel->invalidate(); +} + +void FeedsView::moveSelectedItemDown() { + m_sourceModel->changeSortOrder(selectedItem(), false, false, selectedItem()->sortOrder() + 1); + m_proxyModel->invalidate(); } void FeedsView::markSelectedItemReadStatus(RootItem::ReadStatus read) { diff --git a/src/librssguard/gui/feedsview.h b/src/librssguard/gui/feedsview.h index e715ad94e..b80701245 100644 --- a/src/librssguard/gui/feedsview.h +++ b/src/librssguard/gui/feedsview.h @@ -68,6 +68,7 @@ class RSSGUARD_DLLSPEC FeedsView : public BaseTreeView { // Sort order manipulations. void moveSelectedItemUp(); + void moveSelectedItemDown(); // Selects next/previous item (feed/category) in the list. void selectNextItem();