Refactor Feed loading

This commit is contained in:
Tobias Fella 2020-06-09 23:57:24 +02:00
parent c172a91627
commit 846437dbcb
3 changed files with 38 additions and 82 deletions

View File

@ -24,35 +24,40 @@
#include "feed.h" #include "feed.h"
#include "fetcher.h" #include "fetcher.h"
Feed::Feed(QString url, Feed::Feed(int index)
QString name, : QObject(nullptr)
QString image,
QString link,
QString description,
QVector<Author *> authors,
int deleteAfterCount,
int deleteAfterType,
QDateTime subscribed,
QDateTime lastUpdated,
int autoUpdateCount,
int autoUpdateType,
bool notify,
QObject *parent)
: QObject(parent)
, m_url(url)
, m_name(name)
, m_image(image)
, m_link(link)
, m_description(description)
, m_authors(authors)
, m_deleteAfterCount(deleteAfterCount)
, m_deleteAfterType(deleteAfterType)
, m_subscribed(subscribed)
, m_lastUpdated(lastUpdated)
, m_autoUpdateCount(autoUpdateCount)
, m_autoUpdateType(autoUpdateType)
, m_notify(notify)
{ {
QSqlQuery query;
query.prepare(QStringLiteral("SELECT * FROM Feeds LIMIT 1 OFFSET :index;"));
query.bindValue(QStringLiteral(":index"), index);
Database::instance().execute(query);
if (!query.next())
qWarning() << "Failed to load feed" << index;
QSqlQuery authorQuery;
authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id='' AND feed=:feed"));
authorQuery.bindValue(QStringLiteral(":feed"), query.value(QStringLiteral("url")).toString());
Database::instance().execute(authorQuery);
while (authorQuery.next()) {
m_authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr);
}
m_subscribed.setSecsSinceEpoch(query.value(QStringLiteral("subscribed")).toInt());
m_lastUpdated.setSecsSinceEpoch(query.value(QStringLiteral("lastUpdated")).toInt());
m_url = query.value(QStringLiteral("url")).toString();
m_name = query.value(QStringLiteral("name")).toString();
m_image = query.value(QStringLiteral("image")).toString();
m_link = query.value(QStringLiteral("link")).toString();
m_description = query.value(QStringLiteral("description")).toString();
m_deleteAfterCount = query.value(QStringLiteral("deleteAfterCount")).toInt();
m_deleteAfterType = query.value(QStringLiteral("deleteAfterType")).toInt();
m_autoUpdateCount = query.value(QStringLiteral("autoUpdateCount")).toInt();
m_autoUpdateType = query.value(QStringLiteral("autoUpdateType")).toInt();
m_notify = query.value(QStringLiteral("notify")).toBool();
connect(&Fetcher::instance(), &Fetcher::startedFetchingFeed, this, [this](QString url) { connect(&Fetcher::instance(), &Fetcher::startedFetchingFeed, this, [this](QString url) {
if (url == m_url) { if (url == m_url) {
setRefreshing(true); setRefreshing(true);
@ -61,8 +66,8 @@ Feed::Feed(QString url,
connect(&Fetcher::instance(), &Fetcher::feedUpdated, this, [this](QString url) { connect(&Fetcher::instance(), &Fetcher::feedUpdated, this, [this](QString url) {
if (url == m_url) { if (url == m_url) {
setRefreshing(false); setRefreshing(false);
emit entryCountChanged(); Q_EMIT entryCountChanged();
emit unreadEntryCountChanged(); Q_EMIT unreadEntryCountChanged();
} }
}); });
} }

View File

@ -48,20 +48,7 @@ class Feed : public QObject
Q_PROPERTY(int unreadEntryCount READ unreadEntryCount NOTIFY unreadEntryCountChanged) Q_PROPERTY(int unreadEntryCount READ unreadEntryCount NOTIFY unreadEntryCountChanged)
public: public:
Feed(QString url, Feed(int index);
QString name,
QString image,
QString link,
QString description,
QVector<Author *> authors,
int deleteAfterCount,
int deleteAfterType,
QDateTime subscribed,
QDateTime lastUpdated,
int autoUpdateCount,
int autoUpdateType,
bool notify,
QObject *parent = nullptr);
~Feed(); ~Feed();

View File

@ -79,43 +79,7 @@ QVariant FeedListModel::data(const QModelIndex &index, int role) const
void FeedListModel::loadFeed(int index) const void FeedListModel::loadFeed(int index) const
{ {
QSqlQuery query; m_feeds[index] = new Feed(index);
query.prepare(QStringLiteral("SELECT * FROM Feeds LIMIT 1 OFFSET :index;"));
query.bindValue(QStringLiteral(":index"), index);
Database::instance().execute(query);
if (!query.next())
qWarning() << "Failed to load feed" << index;
QSqlQuery authorQuery;
authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id='' AND feed=:feed"));
authorQuery.bindValue(QStringLiteral(":feed"), query.value(QStringLiteral("url")).toString());
Database::instance().execute(authorQuery);
QVector<Author *> authors;
while (authorQuery.next()) {
authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr);
}
QDateTime subscribed;
subscribed.setSecsSinceEpoch(query.value(QStringLiteral("subscribed")).toInt());
QDateTime lastUpdated;
lastUpdated.setSecsSinceEpoch(query.value(QStringLiteral("lastUpdated")).toInt());
Feed *feed = new Feed(query.value(QStringLiteral("url")).toString(),
query.value(QStringLiteral("name")).toString(),
query.value(QStringLiteral("image")).toString(),
query.value(QStringLiteral("link")).toString(),
query.value(QStringLiteral("description")).toString(),
authors,
query.value(QStringLiteral("deleteAfterCount")).toInt(),
query.value(QStringLiteral("deleteAfterType")).toInt(),
subscribed,
lastUpdated,
query.value(QStringLiteral("autoUpdateCount")).toInt(),
query.value(QStringLiteral("autoUpdateType")).toInt(),
query.value(QStringLiteral("notify")).toBool(),
nullptr);
m_feeds[index] = feed;
} }
void FeedListModel::removeFeed(int index) void FeedListModel::removeFeed(int index)