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
main.cpp
feedListModel.cpp
entryListModel.cpp
feedsmodel.cpp
entriesmodel.cpp
fetcher.cpp
database.cpp
entry.cpp

View File

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

View File

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

View File

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

View File

@ -12,6 +12,8 @@
#include "author.h"
class EntriesModel;
class Feed : public QObject
{
Q_OBJECT
@ -32,6 +34,7 @@ class Feed : public QObject
Q_PROPERTY(int unreadEntryCount READ unreadEntryCount NOTIFY unreadEntryCountChanged)
Q_PROPERTY(int errorId READ errorId WRITE setErrorId NOTIFY errorIdChanged)
Q_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged)
Q_PROPERTY(EntriesModel *entries MEMBER m_entries CONSTANT)
public:
Feed(int index);
@ -104,6 +107,7 @@ private:
bool m_notify;
int m_errorId;
QString m_errorString;
EntriesModel *m_entries;
bool m_refreshing = false;
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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