From bbded05933add0f477b0660d6224e9728583541e Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Tue, 8 Mar 2022 22:32:33 +0100 Subject: [PATCH] Keep unreadEntryCount cached instead of getting it from DB everytime --- src/datamanager.cpp | 11 ----------- src/datamanager.h | 1 - src/entry.cpp | 4 +--- src/feed.cpp | 25 ++++++++++++++++++++++++- src/feed.h | 7 ++++++- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/datamanager.cpp b/src/datamanager.cpp index 49b372bb..4277d45a 100644 --- a/src/datamanager.cpp +++ b/src/datamanager.cpp @@ -168,17 +168,6 @@ int DataManager::entryCount(const Feed *feed) const return m_entrymap[feed->url()].count(); } -int DataManager::unreadEntryCount(const Feed *feed) const -{ - QSqlQuery query; - query.prepare(QStringLiteral("SELECT COUNT (id) FROM Entries where feed=:feed AND read=0;")); - query.bindValue(QStringLiteral(":feed"), feed->url()); - Database::instance().execute(query); - if (!query.next()) - return -1; - return query.value(0).toInt(); -} - int DataManager::newEntryCount(const Feed *feed) const { QSqlQuery query; diff --git a/src/datamanager.h b/src/datamanager.h index 36c187c1..630e69ba 100644 --- a/src/datamanager.h +++ b/src/datamanager.h @@ -36,7 +36,6 @@ public: QStringList getIdList(const Feed *feed) const; int entryCount(const int feed_index) const; int entryCount(const Feed *feed) const; - int unreadEntryCount(const Feed *feed) const; int newEntryCount(const Feed *feed) const; Q_INVOKABLE void addFeed(const QString &url); void addFeed(const QString &url, const bool fetch); diff --git a/src/entry.cpp b/src/entry.cpp index 7a062e11..9423c89a 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -149,9 +149,7 @@ void Entry::setReadInternal(bool read) query.bindValue(QStringLiteral(":read"), m_read); Database::instance().execute(query); - Q_EMIT m_feed->unreadEntryCountChanged(); - Q_EMIT DataManager::instance().unreadEntryCountChanged(m_feed->url()); - // TODO: can one of the two slots be removed?? + m_feed->setUnreadEntryCount(m_feed->unreadEntryCount() + (read ? -1 : 1)); // Follow up actions if (read) { diff --git a/src/feed.cpp b/src/feed.cpp index 124ea8f9..c4d32d72 100644 --- a/src/feed.cpp +++ b/src/feed.cpp @@ -43,6 +43,7 @@ Feed::Feed(const QString &feedurl) m_errorString = QLatin1String(""); updateAuthors(); + updateUnreadEntryCountFromDB(); connect(&Fetcher::instance(), &Fetcher::feedUpdateStatusChanged, this, [this](const QString &url, bool status) { if (url == m_url) { @@ -52,6 +53,7 @@ Feed::Feed(const QString &feedurl) connect(&DataManager::instance(), &DataManager::feedEntriesUpdated, this, [this](const QString &url) { if (url == m_url) { Q_EMIT entryCountChanged(); + updateUnreadEntryCountFromDB(); Q_EMIT unreadEntryCountChanged(); setErrorId(0); setErrorString(QLatin1String("")); @@ -128,6 +130,17 @@ void Feed::updateAuthors() qCDebug(kastsFeed) << "feed" << m_name << "authors have changed?" << haveAuthorsChanged; } +void Feed::updateUnreadEntryCountFromDB() +{ + QSqlQuery query; + query.prepare(QStringLiteral("SELECT COUNT (id) FROM Entries where feed=:feed AND read=0;")); + query.bindValue(QStringLiteral(":feed"), m_url); + Database::instance().execute(query); + if (!query.next()) + m_unreadEntryCount = -1; + m_unreadEntryCount = query.value(0).toInt(); +} + QString Feed::url() const { return m_url; @@ -195,7 +208,7 @@ int Feed::entryCount() const int Feed::unreadEntryCount() const { - return DataManager::instance().unreadEntryCount(this); + return m_unreadEntryCount; } int Feed::newEntryCount() const @@ -289,6 +302,16 @@ void Feed::setNotify(bool notify) } } +void Feed::setUnreadEntryCount(const int count) +{ + if (count != m_unreadEntryCount) { + m_unreadEntryCount = count; + Q_EMIT unreadEntryCountChanged(); + Q_EMIT DataManager::instance().unreadEntryCountChanged(m_url); + // TODO: can one of the two slots be removed?? + } +} + void Feed::setRefreshing(bool refreshing) { if (refreshing != m_refreshing) { diff --git a/src/feed.h b/src/feed.h index 4dcb1541..1684d4e2 100644 --- a/src/feed.h +++ b/src/feed.h @@ -34,7 +34,7 @@ class Feed : public QObject Q_PROPERTY(QDateTime lastUpdated READ lastUpdated WRITE setLastUpdated NOTIFY lastUpdatedChanged) Q_PROPERTY(bool notify READ notify WRITE setNotify NOTIFY notifyChanged) Q_PROPERTY(int entryCount READ entryCount NOTIFY entryCountChanged) - Q_PROPERTY(int unreadEntryCount READ unreadEntryCount NOTIFY unreadEntryCountChanged) + Q_PROPERTY(int unreadEntryCount READ unreadEntryCount WRITE setUnreadEntryCount NOTIFY unreadEntryCountChanged) Q_PROPERTY(int newEntryCount READ newEntryCount NOTIFY newEntryCountChanged) Q_PROPERTY(int errorId READ errorId WRITE setErrorId NOTIFY errorIdChanged) Q_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged) @@ -77,6 +77,7 @@ public: void setDeleteAfterType(int type); void setLastUpdated(const QDateTime &lastUpdated); void setNotify(bool notify); + void setUnreadEntryCount(const int count); void setRefreshing(bool refreshing); void setErrorId(int errorId); void setErrorString(const QString &errorString); @@ -103,6 +104,8 @@ Q_SIGNALS: void refreshingChanged(bool refreshing); private: + void updateUnreadEntryCountFromDB(); + QString m_url; QString m_name; QString m_image; @@ -116,6 +119,8 @@ private: bool m_notify; int m_errorId; QString m_errorString; + int m_unreadEntryCount = -1; + EntriesModel *m_entries; bool m_refreshing = false;