diff --git a/KomplexHubPlugin/newestpacksmodel.cpp b/KomplexHubPlugin/newestpacksmodel.cpp index 9500044..02db1d7 100644 --- a/KomplexHubPlugin/newestpacksmodel.cpp +++ b/KomplexHubPlugin/newestpacksmodel.cpp @@ -1,33 +1,72 @@ #include "newestpacksmodel.h" -#include "common/coreservices.h" -#include "common/logging.h" #include "common/wallpapercache.h" NewestPacksModel::NewestPacksModel(QObject *parent) : QAbstractListModel{parent} { + m_paginator = new NewestPacksPaginator(this); + PaginationNotifier *notifier = static_cast(m_paginator); + QObject::connect( + notifier, + &PaginationNotifier::resultsPerPageChanged, + this, + &NewestPacksModel::resultsPerPageChanged + ); + + QObject::connect( + notifier, + &PaginationNotifier::totalResultsChanged, + this, + &NewestPacksModel::totalResultsChanged + ); + + QObject::connect( + notifier, + &PaginationNotifier::totalPagesChanged, + this, + &NewestPacksModel::totalPagesChanged + ); + + QObject::connect( + notifier, + &PaginationNotifier::pageChanged, + this, + &NewestPacksModel::pageChanged + ); + + QObject::connect( + notifier, + &PaginationNotifier::dataChanged, + this, + &NewestPacksModel::restDataModel + ); } auto NewestPacksModel::rowCount(const QModelIndex &) const -> int { - return m_wallpaperCache.count(); + if(m_paginator != nullptr) + { + return m_paginator->count(); + } + + return 0; } auto NewestPacksModel::data(const QModelIndex &index, int role) const -> QVariant { - if(index.row() < 0) + if(index.row() < 0 || m_paginator == nullptr) { return {}; } - WallpaperCache dataPoint = m_wallpaperCache.at(index.row()); + WallpaperCache dataPoint = m_data.at(index.row()); QVariant data; switch(static_cast(role)) { - case UriRole: - data = dataPoint.uri; + case UuidRole: + data = dataPoint.uuid; break; case AuthorRole: data = dataPoint.author; @@ -68,8 +107,10 @@ auto NewestPacksModel::index(int row, int column, const QModelIndex &parent) con { Q_UNUSED(parent) - WallpaperCache data = m_wallpaperCache.at(row); - return createIndex(row, column, &data); + if(row < 0 || row >= m_data.count()) + return {}; + + return createIndex(row, column, &m_data[row]); } auto NewestPacksModel::columnCount(const QModelIndex &) const -> int @@ -108,6 +149,25 @@ auto NewestPacksModel::resetState() -> void setState(Idle); } +auto NewestPacksModel::restDataModel() -> void +{ + //invalidate previous model data + beginRemoveRows(QModelIndex(), 0, m_data.count()); + m_data.clear(); + endRemoveRows(); + + //signal new data + beginInsertRows(QModelIndex(), 0, m_paginator->count() - 1); + auto dataset = m_paginator->data(); + + for(const WallpaperCache& data : std::as_const(dataset)) + { + m_data.append(data); + } + + endInsertRows(); +} + auto NewestPacksModel::roleNames() const -> QHash { return m_dataRoles; @@ -129,30 +189,66 @@ auto NewestPacksModel::setErrorString(const QString &errorString) -> void emit errorStringChanged(); } -auto NewestPacksModel::resultsPerPage() const -> qint64 +auto NewestPacksModel::resultsPerPage() const -> qsizetype { - return m_resultsPerPage; + if(m_paginator != nullptr) + { + return m_paginator->resultsPerPage(); + } + + return 0; } -auto NewestPacksModel::setResultsPerPage(qint64 resultsPerPage) -> void +auto NewestPacksModel::setResultsPerPage(qsizetype resultsPerPage) -> void { - if (m_resultsPerPage == resultsPerPage) - return; - - m_resultsPerPage = resultsPerPage; - emit resultsPerPageChanged(); + if(m_paginator != nullptr) + { + m_paginator->setResultsPerPage(resultsPerPage); + } } -qint64 NewestPacksModel::page() const +auto NewestPacksModel::totalResults() const -> qsizetype { - return m_page; + if(m_paginator != nullptr) + { + return m_paginator->totalResults(); + } + + return 0; } -void NewestPacksModel::setPage(qint64 page) +auto NewestPacksModel::nextPage() const -> void { - if (m_page == page) - return; - - m_page = page; - emit pageChanged(); + if(m_paginator != nullptr) + { + m_paginator->next(); + } +} + +auto NewestPacksModel::previousPage() const -> void +{ + if(m_paginator != nullptr) + { + m_paginator->previous(); + } +} + +qsizetype NewestPacksModel::page() const +{ + if(m_paginator != nullptr) + { + return m_paginator->page(); + } + + return 0; +} + +auto NewestPacksModel::totalPages() const -> qsizetype +{ + if(m_paginator != nullptr) + { + return m_paginator->totalPages(); + } + + return 0; } diff --git a/KomplexHubPlugin/newestpacksmodel.h b/KomplexHubPlugin/newestpacksmodel.h index 04942be..dfd6d06 100644 --- a/KomplexHubPlugin/newestpacksmodel.h +++ b/KomplexHubPlugin/newestpacksmodel.h @@ -38,12 +38,18 @@ #include "paginators/newestpackspaginator.h" +/** + * @brief The NewestPacksModel class + */ class KOMPLEX_EXPORT NewestPacksModel : public QAbstractListModel { Q_OBJECT QML_ELEMENT - public: + + /** + * @brief Used to control the UI state + */ enum State { Idle, @@ -52,8 +58,11 @@ public: }; Q_ENUM(State) + /** + * @brief Data roles used by the view to request data + */ enum DataRole { - UriRole = Qt::UserRole + 1, + UuidRole = Qt::UserRole + 1, AuthorRole, AuthorIdRole, DescriptionRole, @@ -70,59 +79,139 @@ public: explicit NewestPacksModel(QObject *parent = nullptr); auto rowCount(const QModelIndex &parent = QModelIndex()) const -> int override; - auto data(const QModelIndex &index, int role = Qt::DisplayRole) const -> QVariant override; + /** + * @brief data + * Used by the view to pull data from the model. + * Required by QAbstractListModel + * @param index + * @param role + * @return + */ + auto data( + const QModelIndex &index, + int role = Qt::DisplayRole + ) const -> QVariant override; + + /** + * @brief index + * Used by the view to create an index for the data point. + * Required by QAbstractListModel + * @param row + * @param column + * @param parent + * @return + */ auto index( int row, int column, const QModelIndex &parent = QModelIndex() ) const -> QModelIndex override; + /** + * @brief columnCount + * Required by QAbstractListModel, but just returns 0 + * since we dont use cols + * @param parent + * @return + */ auto columnCount( const QModelIndex &parent = QModelIndex() ) const -> int override; + /** + * @brief parent + * @param index + * @return + */ auto parent(const QModelIndex &index) const -> QModelIndex override; + /** + * @brief setData + * Required by QAbstractListModel but not used + * @param index + * @param value + * @param role + * @return true always + */ auto setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) -> bool override; + /** + * @brief state + * Current Model State + * @return + */ auto state() const -> State; - auto setState(State state) -> void; - auto resetState() -> void; + + /** + * @brief roleNames + * Used to tell the view what data roles are available + * and their QML friendly names + * @return + */ auto roleNames() const -> QHash override; auto errorString() const -> QString; + + auto resultsPerPage() const -> qsizetype; + auto setResultsPerPage(qsizetype resultsPerPage) -> void; + + auto totalResults() const -> qsizetype; + + auto page() const -> qsizetype; + auto totalPages() const -> qsizetype; + + Q_INVOKABLE auto nextPage() const -> void; + Q_INVOKABLE auto previousPage() const -> void; + +protected: + /** + * @brief setErrorString + * @param errorString + */ auto setErrorString(const QString &errorString) -> void; - auto resultsPerPage() const -> qint64; - auto setResultsPerPage(qint64 resultsPerPage) -> void; + /** + * @brief setState + * @param state + */ + auto setState(State state) -> void; - qint64 page() const; - void setPage(qint64 page); + /** + * @brief resetState + */ + auto resetState() -> void; + + auto restDataModel() -> void; signals: - auto installedWallpapersChanged() -> void; auto stateChanged() -> void; auto errorStringChanged() -> void; auto resultsPerPageChanged() -> void; - void pageChanged(); + auto pageChanged() -> void; + auto totalResultsChanged() -> void; + auto totalPagesChanged() -> void; private: static inline const QHash m_dataRoles = { { - static_cast(UriRole), - QByteArray("uri") + static_cast(UuidRole), + QByteArray("uuid") }, { static_cast(AuthorRole), QByteArray("author") }, + { + static_cast(AuthorIdRole), + QByteArray("authorId") + }, { static_cast(DescriptionRole), QByteArray("description") @@ -156,23 +245,20 @@ private: QByteArray("type") } }; - - static inline const QString m_endpointUri = QString(""); State m_state = Idle; QString m_errorString = QString(); - QString m_installDirectoryUri; + NewestPacksPaginator *m_paginator = nullptr; - qint64 m_page = 0; - qint64 m_resultsPerPage = 0; - - NewestPacksPaginator m_wallpaperCache; + mutable QList m_data; Q_PROPERTY(State state READ state WRITE setState RESET resetState NOTIFY stateChanged FINAL) Q_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged FINAL) - Q_PROPERTY(qint64 resultsPerPage READ resultsPerPage WRITE setResultsPerPage NOTIFY resultsPerPageChanged FINAL) - Q_PROPERTY(qint64 page READ page WRITE setPage NOTIFY pageChanged FINAL) + Q_PROPERTY(qsizetype resultsPerPage READ resultsPerPage WRITE setResultsPerPage NOTIFY resultsPerPageChanged FINAL) + Q_PROPERTY(qsizetype totalResults READ totalResults NOTIFY totalResultsChanged FINAL) + Q_PROPERTY(qsizetype totalPages READ totalPages NOTIFY totalPagesChanged FINAL) + Q_PROPERTY(qsizetype page READ page NOTIFY pageChanged FINAL) }; Q_DECLARE_METATYPE(NewestPacksModel)