mirror of
https://github.com/HenkKalkwater/harbour-sailfin.git
synced 2025-09-04 01:42:44 +00:00
WIP: Refractor C++-side. Loader should be working again
This commit is contained in:
parent
2360b261f7
commit
9643482ae1
840 changed files with 100813 additions and 23560 deletions
|
@ -40,6 +40,7 @@
|
|||
#include "namedguidpair.h"
|
||||
#include "userdata.h"*/
|
||||
|
||||
#include "../loader/http/getitem.h"
|
||||
#include "../loader/requesttypes.h"
|
||||
#include "../model/item.h"
|
||||
#include "loader.h"
|
||||
|
@ -51,7 +52,8 @@ namespace ViewModel {
|
|||
class Item : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Q_INVOKABLE explicit Item(QSharedPointer<Model::Item> data = QSharedPointer<Model::Item>(),
|
||||
explicit Item(QObject *parent = nullptr);
|
||||
explicit Item(QSharedPointer<Model::Item> data = QSharedPointer<Model::Item>(),
|
||||
QObject *parent = nullptr);
|
||||
|
||||
// Please keep the order of the properties the same as in the file linked above.
|
||||
|
@ -185,10 +187,24 @@ protected:
|
|||
QSharedPointer<Model::Item> m_data;
|
||||
};
|
||||
|
||||
class ItemLoader : Loader<Item, DTO::BaseItemDto, Jellyfin::Loader::GetItemsByUserIdParams> {
|
||||
class ItemLoader : public Loader<ViewModel::Item, DTO::BaseItemDto, Jellyfin::Loader::GetItemParams> {
|
||||
Q_OBJECT
|
||||
using BaseClass = Loader<ViewModel::Item, DTO::BaseItemDto, Jellyfin::Loader::GetItemParams>;
|
||||
public:
|
||||
explicit ItemLoader(QObject *parent = nullptr);
|
||||
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); }
|
||||
virtual bool canReload() const override;
|
||||
|
||||
signals:
|
||||
void itemIdChanged(const QString &newItemId) const;
|
||||
|
||||
private slots:
|
||||
void onApiClientChanged(ApiClient *newApiClient);
|
||||
private:
|
||||
void setUserId(const QString &newUserId);
|
||||
};
|
||||
|
||||
} // NS ViewModel
|
||||
|
|
|
@ -61,10 +61,13 @@ public:
|
|||
Q_PROPERTY(Status status READ status NOTIFY statusChanged STORED false)
|
||||
Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged STORED false)
|
||||
Q_PROPERTY(bool autoReload MEMBER m_autoReload NOTIFY autoReloadChanged)
|
||||
Q_PROPERTY(QObject *data READ data NOTIFY dataChanged STORED false)
|
||||
|
||||
Status status() const { return m_status; }
|
||||
QString errorString() const { return m_errorString; }
|
||||
|
||||
virtual QObject *data() const { return nullptr; }
|
||||
|
||||
void setApiClient(ApiClient *newApiClient);
|
||||
void setExtraFields(const QStringList &extraFields);
|
||||
signals:
|
||||
|
@ -72,7 +75,7 @@ signals:
|
|||
void apiClientChanged(ApiClient *newApiClient);
|
||||
void errorStringChanged(QString newErrorString);
|
||||
void autoReloadChanged(bool newAutoReload);
|
||||
void viewModelChanged();
|
||||
void dataChanged();
|
||||
|
||||
/**
|
||||
* @brief Convenience signal for status == RemoteData.Ready.
|
||||
|
@ -139,19 +142,19 @@ template <class T, class R, class P>
|
|||
class Loader : public LoaderBase {
|
||||
using RFutureWatcher = QFutureWatcher<std::optional<R>>;
|
||||
public:
|
||||
Loader(QObject *parent = nullptr)
|
||||
: LoaderBase(parent),
|
||||
m_futureWatcher(new RFutureWatcher) {
|
||||
m_dataViewModel = new T(this);
|
||||
connect(m_futureWatcher, &RFutureWatcher::finished, this, &Loader<T, R, P>::updateData());
|
||||
}
|
||||
Loader(ApiClient *apiClient, 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)
|
||||
: LoaderBase(apiClient, parent),
|
||||
m_loader(loaderImpl),
|
||||
m_futureWatcher(new QFutureWatcher<std::optional<R>>) {
|
||||
m_dataViewModel = new T(this);
|
||||
connect(m_futureWatcher, &RFutureWatcher::finished, this, &Loader<T, R, P>::updateData);
|
||||
}
|
||||
|
||||
T *dataViewModel() const { return m_dataViewModel; }
|
||||
QObject *data() const { return m_dataViewModel; }
|
||||
|
||||
void reload() override {
|
||||
setStatus(Loading);
|
||||
|
@ -186,14 +189,16 @@ private:
|
|||
* @brief Updates the data when finished.
|
||||
*/
|
||||
void updateData() {
|
||||
std::optional<R> newData = m_futureWatcher->result();
|
||||
if (newData.has_value()) {
|
||||
if (newData.sameAs(*m_dataViewModel->data())) {
|
||||
std::optional<R> newDataOpt = m_futureWatcher->result();
|
||||
if (newDataOpt.has_value()) {
|
||||
R newData = newDataOpt.value();
|
||||
if (m_dataViewModel->data()->sameAs(newData)) {
|
||||
// Replace the data the model holds
|
||||
m_dataViewModel->data()->replaceData(*newData);
|
||||
m_dataViewModel->data()->replaceData(newData);
|
||||
} else {
|
||||
// Replace the model
|
||||
m_dataViewModel->setData(QSharedPointer<T>::create(*newData, m_apiClient));
|
||||
using PointerType = typename decltype(m_dataViewModel->data())::Type;
|
||||
m_dataViewModel = new T(QSharedPointer<PointerType>::create(newData), this);
|
||||
}
|
||||
setStatus(Ready);
|
||||
emitDataChanged();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue