1
0
Fork 0
mirror of https://github.com/HenkKalkwater/harbour-sailfin.git synced 2025-09-04 01:42:44 +00:00

WIP: Slowly bringing back viewmodels

This commit is contained in:
Chris Josten 2021-03-29 23:48:16 +02:00
parent 9abee12658
commit 228f81984b
17 changed files with 292 additions and 96 deletions

View file

@ -168,7 +168,7 @@ public:
m_startIndex = 0;
m_totalRecordCount = -1;
emitModelShouldClear();
loadMore(0, -1, ViewModel::ModelStatus::Loading);
loadMore(0, m_limit, ViewModel::ModelStatus::Loading);
}
void loadMore() {
@ -272,7 +272,9 @@ protected:
// meaning loadMore is not supported.
return;
}
setRequestLimit<P>(this->m_parameters, limit);
if (limit > 0) {
setRequestLimit<P>(this->m_parameters, limit);
}
this->setStatus(suggestedModelStatus);
// We never want to set this while the loader is running, hence the Mutex and setting it here

View file

@ -30,6 +30,7 @@
#include "apiclient.h"
#include "apimodel.h"
#include "serverdiscoverymodel.h"
#include "websocket.h"
#include "viewmodel/item.h"
#include "viewmodel/itemmodel.h"
#include "viewmodel/loader.h"

View file

@ -33,6 +33,16 @@ namespace Model {
class Item : public DTO::BaseItemDto {
public:
/**
* @brief Constructor that creates an empty item.
*/
Item();
/**
* @brief Copies the data from the DTO into this model and attaches an ApiClient
* @param data The DTO to copy information from
* @param apiClient The ApiClient to attach to, to listen for updates and so on.
*/
Item(const DTO::BaseItemDto &data, ApiClient *apiClient = nullptr);
virtual ~Item();

View file

@ -152,9 +152,24 @@ QJsonValue toJsonValue(const QSharedPointer<T> &source, convertType<QSharedPoint
/**
* Templates for string conversion.
*/
template <typename T>
QString toString(const T &source, convertType<T>) {
return toJsonValue(source).toString();
}
template <typename T>
QString toString(const std::optional<T> &source, convertType<std::optional<T>>) {
if (source.has_value()) {
return toString<T>(source.value(), convertType<T>{});
} else {
return QString();
}
}
template <typename T>
QString toString(const T &source) {
return toJsonValue(source).toString();
return toString(source, convertType<T>{});
}

View file

@ -51,9 +51,7 @@ namespace ViewModel {
class Item : public QObject {
Q_OBJECT
public:
explicit Item(QObject *parent = nullptr);
explicit Item(QSharedPointer<Model::Item> data = QSharedPointer<Model::Item>(),
QObject *parent = nullptr);
explicit Item(QObject *parent = nullptr, QSharedPointer<Model::Item> data = QSharedPointer<Model::Item>::create());
// Please keep the order of the properties the same as in the file linked above.
Q_PROPERTY(QUuid jellyfinId READ jellyfinId NOTIFY jellyfinIdChanged)
@ -194,7 +192,7 @@ public:
Q_PROPERTY(QString itemId READ itemId WRITE setItemId NOTIFY itemIdChanged)
QString itemId() const { return m_parameters.itemId(); }
void setItemId(QString newItemId) { m_parameters.setItemId(newItemId); }
void setItemId(QString newItemId) { m_parameters.setItemId(newItemId); emit itemIdChanged(newItemId); }
virtual bool canReload() const override;
signals:

View file

@ -44,7 +44,44 @@ namespace ViewModel {
// This file contains all models that expose a Model::Item
using UserViewsLoaderBase = LoaderModelLoader<Model::Item, DTO::BaseItemDto, DTO::BaseItemDtoQueryResult, Jellyfin::Loader::GetUserViewsParams>;
/**
* @brief Class intended for models which have a mandatory userId property, which can be extracted from the
* ApiClient.
*/
template <class T, class D, class R, class P>
class AbstractUserParameterLoader : public LoaderModelLoader<T, D, R, P> {
public:
explicit AbstractUserParameterLoader(Support::Loader<R, P> *loader, QObject *parent = nullptr)
: LoaderModelLoader<T, D, R, P>(loader, parent) {
this->connect(this, &BaseModelLoader::apiClientChanged, this, &AbstractUserParameterLoader<T, D, R, P>::apiClientChanged);
}
protected:
virtual bool canReload() const override {
return BaseModelLoader::canReload() && !this->m_parameters.userId().isNull();
}
private:
void apiClientChanged(ApiClient *newApiClient) {
if (this->m_apiClient != nullptr) {
this->disconnect(this->m_apiClient, &ApiClient::userIdChanged, this, &AbstractUserParameterLoader<T, D, R, P>::userIdChanged);
}
if (newApiClient != nullptr) {
this->connect(newApiClient, &ApiClient::userIdChanged, this, &AbstractUserParameterLoader<T, D, R, P>::userIdChanged);
if (!newApiClient->userId().isNull()) {
this->m_parameters.setUserId(newApiClient->userId());
}
}
}
void userIdChanged(const QString &newUserId) {
this->m_parameters.setUserId(newUserId);
this->autoReloadIfNeeded();
}
};
/**
* Loads the views of an user, such as "Videos", "Music" and so on.
*/
using UserViewsLoaderBase = AbstractUserParameterLoader<Model::Item, DTO::BaseItemDto, DTO::BaseItemDtoQueryResult, Jellyfin::Loader::GetUserViewsParams>;
class UserViewsLoader : public UserViewsLoaderBase {
Q_OBJECT
public:
@ -53,12 +90,27 @@ public:
FWDPROP(bool, includeExternalContent, IncludeExternalContent)
FWDPROP(bool, includeHidden, IncludeHidden)
FWDPROP(QStringList, presetViews, PresetViews)
private slots:
void apiClientChanged(ApiClient *newApiClient);
void userIdChanged(const QString &newUserId);
};
using UserItemsLoaderBase = LoaderModelLoader<Model::Item, DTO::BaseItemDto, DTO::BaseItemDtoQueryResult, Jellyfin::Loader::GetItemsByUserIdParams>;
using LatestMediaBase = AbstractUserParameterLoader<Model::Item, DTO::BaseItemDto, QList<DTO::BaseItemDto>, Jellyfin::Loader::GetLatestMediaParams>;
class LatestMediaLoader : public LatestMediaBase {
Q_OBJECT
public:
explicit LatestMediaLoader(QObject *parent = nullptr);
// Optional
FWDPROP(QList<Jellyfin::DTO::ImageTypeClass::Value>, enableImageTypes, EnableImageTypes)
FWDPROP(bool, enableImages, EnableImages)
FWDPROP(bool, enableUserData, EnableUserData)
FWDPROP(QList<Jellyfin::DTO::ItemFieldsClass::Value>, fields, Fields)
FWDPROP(bool, groupItems, GroupItems)
FWDPROP(qint32, imageTypeLimit, ImageTypeLimit)
FWDPROP(QStringList, includeItemTypes, IncludeItemTypes)
FWDPROP(bool, isPlayed, IsPlayed)
FWDPROP(QString, parentId, ParentId)
};
using UserItemsLoaderBase = AbstractUserParameterLoader<Model::Item, DTO::BaseItemDto, DTO::BaseItemDtoQueryResult, Jellyfin::Loader::GetItemsByUserIdParams>;
class UserItemsLoader : public UserItemsLoaderBase {
Q_OBJECT
public:
@ -70,14 +122,26 @@ public:
FWDPROP(QStringList, albums, Albums)
FWDPROP(QStringList, artistIds, ArtistIds)
FWDPROP(QStringList, artists, Artists)
FWDPROP(bool, collapseBoxSetItems, CollapseBoxSetItems)
FWDPROP(QStringList, contributingArtistIds, ContributingArtistIds)
FWDPROP(QList<Jellyfin::DTO::ImageTypeClass::Value>, enableImageTypes, EnableImageTypes);
FWDPROP(bool, enableImages, EnableImages)
FWDPROP(bool, enableTotalRecordCount, EnableTotalRecordCount)
FWDPROP(bool, enableUserData, EnableUserData)
FWDPROP(QStringList, excludeArtistIds, ExcludeArtistIds)
FWDPROP(QStringList, excludeItemIds, ExcludeItemIds)
FWDPROP(QStringList, excludeItemTypes, ExcludeItemTypes)
FWDPROP(QList<Jellyfin::DTO::LocationTypeClass::Value>, excludeLocationTypes, ExcludeLocationTypes)
FWDPROP(QList<Jellyfin::DTO::ItemFieldsClass::Value>, fields, Fields)
FWDPROP(QList<Jellyfin::DTO::ItemFilterClass::Value>, filters, Filters)
FWDPROP(QString, parentId, ParentId)
FWDPROP(bool, recursive, Recursive)
//FWDPROP(bool, collapseBoxSetItems)
protected:
virtual bool canReload() const override;
private slots:
void apiClientChanged(ApiClient *newApiClient);
void userIdChanged(const QString &newUserId);
};
/**
* @brief Base class for each model that works with items.
*/
@ -95,7 +159,10 @@ public:
playlistItemId,
dateCreated,
dateLastMediaAdded,
extraType
extraType,
// Hand-picked, important ones
imageTags
};
explicit ItemModel (QObject *parent = nullptr);
@ -111,7 +178,9 @@ public:
JFRN(playlistItemId),
JFRN(dateCreated),
JFRN(dateLastMediaAdded),
JFRN(extraType)
JFRN(extraType),
// Handpicked, important ones
JFRN(imageTags),
};
}
QVariant data(const QModelIndex &index, int role) const override;

View file

@ -144,13 +144,14 @@ template <class T, class R, class P>
class Loader : public LoaderBase {
using RFutureWatcher = QFutureWatcher<std::optional<R>>;
public:
Loader(Support::Loader<R, P> loaderImpl, QObject *parent = nullptr)
Loader(Support::Loader<R, P> *loaderImpl, QObject *parent = nullptr)
: Loader(nullptr, loaderImpl, parent) {}
Loader(ApiClient *apiClient, Support::Loader<R, P> loaderImpl, QObject *parent = nullptr)
Loader(ApiClient *apiClient, Support::Loader<R, P> *loaderImpl, QObject *parent = nullptr)
: LoaderBase(apiClient, parent),
m_loader(loaderImpl),
m_futureWatcher(new QFutureWatcher<std::optional<R>>) {
m_futureWatcher(new QFutureWatcher<std::optional<R>>(this)) {
m_dataViewModel = new T(this);
connect(m_futureWatcher, &RFutureWatcher::finished, this, &Loader<T, R, P>::updateData);
}
@ -161,8 +162,9 @@ public:
void reload() override {
if (m_futureWatcher->isRunning()) return;
setStatus(Loading);
m_loader.setParameters(m_parameters);
m_loader.prepareLoad();
this->m_loader->setApiClient(m_apiClient);
m_loader->setParameters(m_parameters);
m_loader->prepareLoad();
QFuture<std::optional<R>> future = QtConcurrent::run(this, &Loader<T, R, P>::invokeLoader);
m_futureWatcher->setFuture(future);
}
@ -172,7 +174,7 @@ protected:
/**
* @brief Subclasses should initialize this to a loader that actually loads stuff.
*/
Support::Loader<R, P> m_loader;
QScopedPointer<Support::Loader<R, P>> m_loader = nullptr;
private:
QFutureWatcher<std::optional<R>> *m_futureWatcher;
@ -184,9 +186,8 @@ private:
*/
std::optional<R> invokeLoader() {
QMutexLocker(&this->m_mutex);
this->m_loader.setApiClient(m_apiClient);
try {
return this->m_loader.load();
return this->m_loader->load();
} catch (Support::LoadException &e) {
qWarning() << "Exception while loading an item: " << e.what();
this->setErrorString(QString(e.what()));
@ -206,7 +207,7 @@ private:
} else {
// Replace the model
using PointerType = typename decltype(m_dataViewModel->data())::Type;
m_dataViewModel = new T(QSharedPointer<PointerType>::create(newData), this);
m_dataViewModel = new T(this, QSharedPointer<PointerType>::create(newData, m_apiClient));
}
setStatus(Ready);
emitDataChanged();

View file

@ -80,7 +80,7 @@ public:
Q_PROPERTY(PlayMethod playMethod READ playMethod NOTIFY playMethodChanged)
// Current Item and queue informatoion
Q_PROPERTY(Model::Item *item READ item NOTIFY itemChanged)
Q_PROPERTY(ViewModel::Item *item READ item NOTIFY itemChanged)
Q_PROPERTY(QAbstractItemModel *queue READ queue NOTIFY queueChanged)
Q_PROPERTY(int queueIndex READ queueIndex NOTIFY queueIndexChanged)
@ -94,7 +94,7 @@ public:
Q_PROPERTY(QMediaPlayer::State playbackState READ playbackState NOTIFY playbackStateChanged)
Q_PROPERTY(qint64 position READ position NOTIFY positionChanged)
Model::Item *item() const { return m_item.data(); }
ViewModel::Item *item() const { return m_displayItem.get(); }
void setApiClient(ApiClient *apiClient);
QString streamUrl() const { return m_streamUrl; }
@ -169,6 +169,8 @@ private:
QTimer m_updateTimer;
ApiClient *m_apiClient = nullptr;
QSharedPointer<Model::Item> m_item;
QScopedPointer<ViewModel::Item> m_displayItem = QScopedPointer<ViewModel::Item>(new ViewModel::Item());
QString m_streamUrl;
QString m_playSessionId;
int m_audioIndex = 0;