diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 65caa017..37b77586 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,7 @@ set(SRCS_base models/entriesmodel.cpp models/queuemodel.cpp models/episodemodel.cpp + models/episodeproxymodel.cpp models/downloadmodel.cpp models/errorlogmodel.cpp models/podcastsearchmodel.cpp diff --git a/src/main.cpp b/src/main.cpp index c00966da..9b5c67aa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,7 +41,7 @@ #include "kasts-version.h" #include "models/downloadmodel.h" #include "models/entriesmodel.h" -#include "models/episodemodel.h" +#include "models/episodeproxymodel.h" #include "models/errorlogmodel.h" #include "models/feedsmodel.h" #include "models/podcastsearchmodel.h" @@ -120,7 +120,7 @@ int main(int argc, char *argv[]) qmlRegisterType("org.kde.kasts", 1, 0, "FeedsModel"); qmlRegisterType("org.kde.kasts", 1, 0, "QueueModel"); - qmlRegisterType("org.kde.kasts", 1, 0, "EpisodeModel"); + qmlRegisterType("org.kde.kasts", 1, 0, "EpisodeProxyModel"); qmlRegisterType("org.kde.kasts", 1, 0, "Mpris2"); qmlRegisterType("org.kde.kasts", 1, 0, "PodcastSearchModel"); diff --git a/src/models/episodeproxymodel.cpp b/src/models/episodeproxymodel.cpp new file mode 100644 index 00000000..23c80f9a --- /dev/null +++ b/src/models/episodeproxymodel.cpp @@ -0,0 +1,51 @@ +/** + * SPDX-FileCopyrightText: 2021 Bart De Vries + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#include "models/episodeproxymodel.h" + +EpisodeProxyModel::EpisodeProxyModel() + : QSortFilterProxyModel(nullptr) +{ + m_currentFilter = NoFilter; + m_episodeModel = new EpisodeModel(); + setSourceModel(m_episodeModel); +} + +EpisodeProxyModel::~EpisodeProxyModel() +{ + delete m_episodeModel; +} + +bool EpisodeProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +{ + QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); + + switch (m_currentFilter) { + case NoFilter: + return true; + case ReadFilter: + return sourceModel()->data(index, EpisodeModel::Roles::ReadRole).value(); + case NotReadFilter: + return !sourceModel()->data(index, EpisodeModel::Roles::ReadRole).value(); + case NewFilter: + return sourceModel()->data(index, EpisodeModel::Roles::NewRole).value(); + case NotNewFilter: + return !sourceModel()->data(index, EpisodeModel::Roles::NewRole).value(); + default: + return true; + } +} + +EpisodeProxyModel::FilterType EpisodeProxyModel::filterType() const +{ + return m_currentFilter; +} + +void EpisodeProxyModel::setFilterType(FilterType type) +{ + m_currentFilter = type; + Q_EMIT filterTypeChanged(m_currentFilter); +} diff --git a/src/models/episodeproxymodel.h b/src/models/episodeproxymodel.h new file mode 100644 index 00000000..4ea14b9c --- /dev/null +++ b/src/models/episodeproxymodel.h @@ -0,0 +1,43 @@ +/** + * SPDX-FileCopyrightText: 2021 Bart De Vries + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#pragma once + +#include + +#include "models/episodemodel.h" + +class EpisodeProxyModel : public QSortFilterProxyModel +{ + Q_OBJECT + +public: + enum FilterType { + NoFilter, + ReadFilter, + NotReadFilter, + NewFilter, + NotNewFilter, + }; + Q_ENUM(FilterType) + + Q_PROPERTY(FilterType filterType READ filterType WRITE setFilterType NOTIFY filterTypeChanged) + + explicit EpisodeProxyModel(); + ~EpisodeProxyModel(); + + bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; + + FilterType filterType() const; + void setFilterType(FilterType type); + +Q_SIGNALS: + void filterTypeChanged(FilterType filter); + +private: + EpisodeModel *m_episodeModel; + FilterType m_currentFilter; +}; diff --git a/src/qml/EpisodeListPage.qml b/src/qml/EpisodeListPage.qml index f12a0903..afff313d 100644 --- a/src/qml/EpisodeListPage.qml +++ b/src/qml/EpisodeListPage.qml @@ -17,8 +17,6 @@ Kirigami.ScrollablePage { title: i18n("Episode List") - property var episodeType: EpisodeModel.All - supportsRefreshing: true onRefreshingChanged: { if(refreshing) { @@ -52,15 +50,15 @@ Kirigami.ScrollablePage { } } - EpisodeModel { - id: episodeModel + EpisodeProxyModel { + id: episodeProxyModel } ListView { id: episodeList anchors.fill: parent visible: count !== 0 - model: episodeModel + model: episodeProxyModel delegate: Kirigami.DelegateRecycler { width: episodeList.width