Keep unreadEntryCount cached instead of getting it from DB everytime

This commit is contained in:
Bart De Vries 2022-03-08 22:32:33 +01:00
parent 529a1ca878
commit bbded05933
5 changed files with 31 additions and 17 deletions

View File

@ -168,17 +168,6 @@ int DataManager::entryCount(const Feed *feed) const
return m_entrymap[feed->url()].count(); 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 int DataManager::newEntryCount(const Feed *feed) const
{ {
QSqlQuery query; QSqlQuery query;

View File

@ -36,7 +36,6 @@ public:
QStringList getIdList(const Feed *feed) const; QStringList getIdList(const Feed *feed) const;
int entryCount(const int feed_index) const; int entryCount(const int feed_index) const;
int entryCount(const Feed *feed) const; int entryCount(const Feed *feed) const;
int unreadEntryCount(const Feed *feed) const;
int newEntryCount(const Feed *feed) const; int newEntryCount(const Feed *feed) const;
Q_INVOKABLE void addFeed(const QString &url); Q_INVOKABLE void addFeed(const QString &url);
void addFeed(const QString &url, const bool fetch); void addFeed(const QString &url, const bool fetch);

View File

@ -149,9 +149,7 @@ void Entry::setReadInternal(bool read)
query.bindValue(QStringLiteral(":read"), m_read); query.bindValue(QStringLiteral(":read"), m_read);
Database::instance().execute(query); Database::instance().execute(query);
Q_EMIT m_feed->unreadEntryCountChanged(); m_feed->setUnreadEntryCount(m_feed->unreadEntryCount() + (read ? -1 : 1));
Q_EMIT DataManager::instance().unreadEntryCountChanged(m_feed->url());
// TODO: can one of the two slots be removed??
// Follow up actions // Follow up actions
if (read) { if (read) {

View File

@ -43,6 +43,7 @@ Feed::Feed(const QString &feedurl)
m_errorString = QLatin1String(""); m_errorString = QLatin1String("");
updateAuthors(); updateAuthors();
updateUnreadEntryCountFromDB();
connect(&Fetcher::instance(), &Fetcher::feedUpdateStatusChanged, this, [this](const QString &url, bool status) { connect(&Fetcher::instance(), &Fetcher::feedUpdateStatusChanged, this, [this](const QString &url, bool status) {
if (url == m_url) { if (url == m_url) {
@ -52,6 +53,7 @@ Feed::Feed(const QString &feedurl)
connect(&DataManager::instance(), &DataManager::feedEntriesUpdated, this, [this](const QString &url) { connect(&DataManager::instance(), &DataManager::feedEntriesUpdated, this, [this](const QString &url) {
if (url == m_url) { if (url == m_url) {
Q_EMIT entryCountChanged(); Q_EMIT entryCountChanged();
updateUnreadEntryCountFromDB();
Q_EMIT unreadEntryCountChanged(); Q_EMIT unreadEntryCountChanged();
setErrorId(0); setErrorId(0);
setErrorString(QLatin1String("")); setErrorString(QLatin1String(""));
@ -128,6 +130,17 @@ void Feed::updateAuthors()
qCDebug(kastsFeed) << "feed" << m_name << "authors have changed?" << haveAuthorsChanged; 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 QString Feed::url() const
{ {
return m_url; return m_url;
@ -195,7 +208,7 @@ int Feed::entryCount() const
int Feed::unreadEntryCount() const int Feed::unreadEntryCount() const
{ {
return DataManager::instance().unreadEntryCount(this); return m_unreadEntryCount;
} }
int Feed::newEntryCount() const 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) void Feed::setRefreshing(bool refreshing)
{ {
if (refreshing != m_refreshing) { if (refreshing != m_refreshing) {

View File

@ -34,7 +34,7 @@ class Feed : public QObject
Q_PROPERTY(QDateTime lastUpdated READ lastUpdated WRITE setLastUpdated NOTIFY lastUpdatedChanged) Q_PROPERTY(QDateTime lastUpdated READ lastUpdated WRITE setLastUpdated NOTIFY lastUpdatedChanged)
Q_PROPERTY(bool notify READ notify WRITE setNotify NOTIFY notifyChanged) Q_PROPERTY(bool notify READ notify WRITE setNotify NOTIFY notifyChanged)
Q_PROPERTY(int entryCount READ entryCount NOTIFY entryCountChanged) 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 newEntryCount READ newEntryCount NOTIFY newEntryCountChanged)
Q_PROPERTY(int errorId READ errorId WRITE setErrorId NOTIFY errorIdChanged) Q_PROPERTY(int errorId READ errorId WRITE setErrorId NOTIFY errorIdChanged)
Q_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged) Q_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged)
@ -77,6 +77,7 @@ public:
void setDeleteAfterType(int type); void setDeleteAfterType(int type);
void setLastUpdated(const QDateTime &lastUpdated); void setLastUpdated(const QDateTime &lastUpdated);
void setNotify(bool notify); void setNotify(bool notify);
void setUnreadEntryCount(const int count);
void setRefreshing(bool refreshing); void setRefreshing(bool refreshing);
void setErrorId(int errorId); void setErrorId(int errorId);
void setErrorString(const QString &errorString); void setErrorString(const QString &errorString);
@ -103,6 +104,8 @@ Q_SIGNALS:
void refreshingChanged(bool refreshing); void refreshingChanged(bool refreshing);
private: private:
void updateUnreadEntryCountFromDB();
QString m_url; QString m_url;
QString m_name; QString m_name;
QString m_image; QString m_image;
@ -116,6 +119,8 @@ private:
bool m_notify; bool m_notify;
int m_errorId; int m_errorId;
QString m_errorString; QString m_errorString;
int m_unreadEntryCount = -1;
EntriesModel *m_entries; EntriesModel *m_entries;
bool m_refreshing = false; bool m_refreshing = false;