Refactor models

This commit is contained in:
Tobias Fella 2020-10-09 13:47:35 +02:00
parent 0f7f79898e
commit 7647edcdc2
11 changed files with 40 additions and 45 deletions

View File

@ -1,7 +1,7 @@
set(alligator_SRCS set(alligator_SRCS
main.cpp main.cpp
feedListModel.cpp feedsmodel.cpp
entryListModel.cpp entriesmodel.cpp
fetcher.cpp fetcher.cpp
database.cpp database.cpp
entry.cpp entry.cpp

View File

@ -9,11 +9,12 @@
#include <QVector> #include <QVector>
#include "database.h" #include "database.h"
#include "entryListModel.h" #include "entriesmodel.h"
#include "fetcher.h" #include "fetcher.h"
EntryListModel::EntryListModel(QObject *parent) EntriesModel::EntriesModel(Feed *feed)
: QAbstractListModel(parent) : QAbstractListModel(feed)
, m_feed(feed)
{ {
connect(&Fetcher::instance(), &Fetcher::feedUpdated, this, [this](QString url) { connect(&Fetcher::instance(), &Fetcher::feedUpdated, this, [this](QString url) {
if (m_feed->url() == url) { if (m_feed->url() == url) {
@ -27,7 +28,7 @@ EntryListModel::EntryListModel(QObject *parent)
}); });
} }
QVariant EntryListModel::data(const QModelIndex &index, int role) const QVariant EntriesModel::data(const QModelIndex &index, int role) const
{ {
if (role != 0) if (role != 0)
return QVariant(); return QVariant();
@ -36,14 +37,14 @@ QVariant EntryListModel::data(const QModelIndex &index, int role) const
return QVariant::fromValue(m_entries[index.row()]); return QVariant::fromValue(m_entries[index.row()]);
} }
QHash<int, QByteArray> EntryListModel::roleNames() const QHash<int, QByteArray> EntriesModel::roleNames() const
{ {
QHash<int, QByteArray> roleNames; QHash<int, QByteArray> roleNames;
roleNames[0] = "entry"; roleNames[0] = "entry";
return roleNames; return roleNames;
} }
int EntryListModel::rowCount(const QModelIndex &parent) const int EntriesModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent) Q_UNUSED(parent)
QSqlQuery query; QSqlQuery query;
@ -55,18 +56,12 @@ int EntryListModel::rowCount(const QModelIndex &parent) const
return query.value(0).toInt(); return query.value(0).toInt();
} }
void EntryListModel::loadEntry(int index) const void EntriesModel::loadEntry(int index) const
{ {
m_entries[index] = new Entry(m_feed, index); m_entries[index] = new Entry(m_feed, index);
} }
Feed *EntryListModel::feed() const Feed *EntriesModel::feed() const
{ {
return m_feed; return m_feed;
} }
void EntryListModel::setFeed(Feed *feed)
{
m_feed = feed;
Q_EMIT feedChanged(feed);
}

View File

@ -12,26 +12,22 @@
#include <QString> #include <QString>
#include "entry.h" #include "entry.h"
#include "feed.h"
class EntryListModel : public QAbstractListModel class EntriesModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(Feed *feed READ feed WRITE setFeed NOTIFY feedChanged) Q_PROPERTY(Feed *feed READ feed CONSTANT)
public: public:
explicit EntryListModel(QObject *parent = nullptr); explicit EntriesModel(Feed *feed);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
Feed *feed() const; Feed *feed() const;
void setFeed(Feed *feed);
Q_SIGNALS:
void feedChanged(Feed *feed);
private: private:
void loadEntry(int index) const; void loadEntry(int index) const;

View File

@ -7,6 +7,7 @@
#include <QVariant> #include <QVariant>
#include "database.h" #include "database.h"
#include "entriesmodel.h"
#include "feed.h" #include "feed.h"
#include "fetcher.h" #include "fetcher.h"
@ -70,6 +71,8 @@ Feed::Feed(int index)
if(url == m_image) if(url == m_image)
Q_EMIT imageChanged(url); Q_EMIT imageChanged(url);
}); });
m_entries = new EntriesModel(this);
} }
Feed::~Feed() Feed::~Feed()

View File

@ -12,6 +12,8 @@
#include "author.h" #include "author.h"
class EntriesModel;
class Feed : public QObject class Feed : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -32,6 +34,7 @@ class Feed : public QObject
Q_PROPERTY(int unreadEntryCount READ unreadEntryCount NOTIFY unreadEntryCountChanged) Q_PROPERTY(int unreadEntryCount READ unreadEntryCount NOTIFY unreadEntryCountChanged)
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)
Q_PROPERTY(EntriesModel *entries MEMBER m_entries CONSTANT)
public: public:
Feed(int index); Feed(int index);
@ -104,6 +107,7 @@ private:
bool m_notify; bool m_notify;
int m_errorId; int m_errorId;
QString m_errorString; QString m_errorString;
EntriesModel *m_entries;
bool m_refreshing = false; bool m_refreshing = false;
}; };

View File

@ -11,10 +11,10 @@
#include <QVariant> #include <QVariant>
#include "database.h" #include "database.h"
#include "feedListModel.h" #include "feedsmodel.h"
#include "fetcher.h" #include "fetcher.h"
FeedListModel::FeedListModel(QObject *parent) FeedsModel::FeedsModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
{ {
connect(&Database::instance(), &Database::feedAdded, this, [this]() { connect(&Database::instance(), &Database::feedAdded, this, [this]() {
@ -36,14 +36,14 @@ FeedListModel::FeedListModel(QObject *parent)
}); });
} }
QHash<int, QByteArray> FeedListModel::roleNames() const QHash<int, QByteArray> FeedsModel::roleNames() const
{ {
QHash<int, QByteArray> roleNames; QHash<int, QByteArray> roleNames;
roleNames[0] = "feed"; roleNames[0] = "feed";
return roleNames; return roleNames;
} }
int FeedListModel::rowCount(const QModelIndex &parent) const int FeedsModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent) Q_UNUSED(parent)
QSqlQuery query; QSqlQuery query;
@ -54,7 +54,7 @@ int FeedListModel::rowCount(const QModelIndex &parent) const
return query.value(0).toInt(); return query.value(0).toInt();
} }
QVariant FeedListModel::data(const QModelIndex &index, int role) const QVariant FeedsModel::data(const QModelIndex &index, int role) const
{ {
if (role != 0) if (role != 0)
return QVariant(); return QVariant();
@ -63,12 +63,12 @@ QVariant FeedListModel::data(const QModelIndex &index, int role) const
return QVariant::fromValue(m_feeds[index.row()]); return QVariant::fromValue(m_feeds[index.row()]);
} }
void FeedListModel::loadFeed(int index) const void FeedsModel::loadFeed(int index) const
{ {
m_feeds += new Feed(index); m_feeds += new Feed(index);
} }
void FeedListModel::removeFeed(int index) void FeedsModel::removeFeed(int index)
{ {
m_feeds[index]->remove(); m_feeds[index]->remove();
delete m_feeds[index]; delete m_feeds[index];
@ -77,7 +77,7 @@ void FeedListModel::removeFeed(int index)
endRemoveRows(); endRemoveRows();
} }
void FeedListModel::refreshAll() void FeedsModel::refreshAll()
{ {
for (auto &feed : m_feeds) { for (auto &feed : m_feeds) {
feed->refresh(); feed->refresh();

View File

@ -13,12 +13,12 @@
#include "feed.h" #include "feed.h"
class FeedListModel : public QAbstractListModel class FeedsModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit FeedListModel(QObject *parent = nullptr); explicit FeedsModel(QObject *parent = nullptr);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;

View File

@ -23,8 +23,8 @@
#include "alligatorsettings.h" #include "alligatorsettings.h"
#include "database.h" #include "database.h"
#include "entryListModel.h" #include "entriesmodel.h"
#include "feedListModel.h" #include "feedsmodel.h"
#include "fetcher.h" #include "fetcher.h"
#ifdef Q_OS_ANDROID #ifdef Q_OS_ANDROID
@ -44,8 +44,8 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationName(QStringLiteral("Alligator")); QCoreApplication::setApplicationName(QStringLiteral("Alligator"));
QCoreApplication::setApplicationVersion(QStringLiteral("0.1")); QCoreApplication::setApplicationVersion(QStringLiteral("0.1"));
qmlRegisterType<FeedListModel>("org.kde.alligator", 1, 0, "FeedListModel"); qmlRegisterType<FeedsModel>("org.kde.alligator", 1, 0, "FeedsModel");
qmlRegisterType<EntryListModel>("org.kde.alligator", 1, 0, "EntryListModel"); qmlRegisterUncreatableType<EntriesModel>("org.kde.alligator", 1, 0, "EntriesModel", QStringLiteral("Get from Feed"));
qmlRegisterSingletonType<Fetcher>("org.kde.alligator", 1, 0, "Fetcher", [](QQmlEngine *engine, QJSEngine *) -> QObject * { qmlRegisterSingletonType<Fetcher>("org.kde.alligator", 1, 0, "Fetcher", [](QQmlEngine *engine, QJSEngine *) -> QObject * {
engine->setObjectOwnership(&Fetcher::instance(), QQmlEngine::CppOwnership); engine->setObjectOwnership(&Fetcher::instance(), QQmlEngine::CppOwnership);
return &Fetcher::instance(); return &Fetcher::instance();

View File

@ -61,10 +61,7 @@ Kirigami.ScrollablePage {
ListView { ListView {
id: entryList id: entryList
visible: count !== 0 visible: count !== 0
model: EntryListModel { model: page.feed.entries
id: entryListModel
feed: page.feed
}
header: EntryListHeader { } header: EntryListHeader { }

View File

@ -36,7 +36,7 @@ Kirigami.SwipeListItem {
onTriggered: { onTriggered: {
if(pageStack.depth > 1 && model.feed.url === lastFeed) if(pageStack.depth > 1 && model.feed.url === lastFeed)
pageStack.pop() pageStack.pop()
feedListModel.removeFeed(index) feedsModel.removeFeed(index)
} }
} }

View File

@ -69,8 +69,8 @@ Kirigami.ScrollablePage {
id: feedList id: feedList
visible: count !== 0 visible: count !== 0
anchors.fill: parent anchors.fill: parent
model: FeedListModel { model: FeedsModel {
id: feedListModel id: feedsModel
} }
delegate: FeedListDelegate { } delegate: FeedListDelegate { }