refactor: move more common code to base class

Also removes unused imports and organize the ModModel header
This commit is contained in:
flow 2022-03-07 17:46:18 -03:00
parent 16bfafa29e
commit b131d3b2ec
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
6 changed files with 65 additions and 170 deletions

View File

@ -1,6 +1,6 @@
#include "ModModel.h" #include "ModModel.h"
#include "ModPage.h"
#include "Json.h"
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h" #include "minecraft/PackProfile.h"
#include "ui/dialogs/ModDownloadDialog.h" #include "ui/dialogs/ModDownloadDialog.h"
@ -11,18 +11,6 @@ namespace ModPlatform {
ListModel::ListModel(ModPage* parent) : QAbstractListModel(parent), m_parent(parent) {} ListModel::ListModel(ModPage* parent) : QAbstractListModel(parent), m_parent(parent) {}
ListModel::~ListModel() {}
int ListModel::rowCount(const QModelIndex& parent) const
{
return modpacks.size();
}
int ListModel::columnCount(const QModelIndex& parent) const
{
return 1;
}
QVariant ListModel::data(const QModelIndex& index, int role) const QVariant ListModel::data(const QModelIndex& index, int role) const
{ {
int pos = index.row(); int pos = index.row();
@ -73,16 +61,6 @@ void ListModel::logoFailed(QString logo)
m_loadingLogos.removeAll(logo); m_loadingLogos.removeAll(logo);
} }
Qt::ItemFlags ListModel::flags(const QModelIndex& index) const
{
return QAbstractListModel::flags(index);
}
bool ListModel::canFetchMore(const QModelIndex& parent) const
{
return searchState == CanPossiblyFetchMore;
}
void ListModel::fetchMore(const QModelIndex& parent) void ListModel::fetchMore(const QModelIndex& parent)
{ {
if (parent.isValid()) return; if (parent.isValid()) return;
@ -140,6 +118,38 @@ void ListModel::searchWithTerm(const QString& term, const int sort)
performPaginatedSearch(); performPaginatedSearch();
} }
void ListModel::searchRequestFinished(QJsonDocument& doc)
{
jobPtr.reset();
QList<ModPlatform::IndexedPack> newList;
auto packs = documentToArray(doc);
for (auto packRaw : packs) {
auto packObj = packRaw.toObject();
ModPlatform::IndexedPack pack;
try {
loadIndexedPack(pack, packObj);
newList.append(pack);
} catch (const JSONValidationError& e) {
qWarning() << "Error while loading mod from " << m_parent->debugName() << ": " << e.cause();
continue;
}
}
if (packs.size() < 25) {
searchState = Finished;
} else {
nextSearchOffset += 25;
searchState = CanPossiblyFetchMore;
}
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
modpacks.append(newList);
endInsertRows();
}
void ListModel::searchRequestFailed(QString reason) void ListModel::searchRequestFailed(QString reason)
{ {
if (jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) { if (jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) {

View File

@ -18,29 +18,30 @@ class ListModel : public QAbstractListModel {
public: public:
ListModel(ModPage* parent); ListModel(ModPage* parent);
virtual ~ListModel(); virtual ~ListModel() = default;
int rowCount(const QModelIndex& parent) const override; inline int rowCount(const QModelIndex& parent) const override { return modpacks.size(); };
int columnCount(const QModelIndex& parent) const override; inline int columnCount(const QModelIndex& parent) const override { return 1; };
inline Qt::ItemFlags flags(const QModelIndex& index) const override { return QAbstractListModel::flags(index); };
QString debugName() const; QString debugName() const;
/* Retrieve information from the model at a given index with the given role */ /* Retrieve information from the model at a given index with the given role */
QVariant data(const QModelIndex& index, int role) const override; QVariant data(const QModelIndex& index, int role) const override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
void setActiveJob(NetJob::Ptr ptr) { jobPtr = ptr; } inline void setActiveJob(NetJob::Ptr ptr) { jobPtr = ptr; }
bool canFetchMore(const QModelIndex& parent) const override; /* Ask the API for more information */
void fetchMore(const QModelIndex& parent) override; void fetchMore(const QModelIndex& parent) override;
void searchWithTerm(const QString& term, const int sort);
void requestModVersions(const ModPlatform::IndexedPack& current);
void getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback); void getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback);
void searchWithTerm(const QString& term, const int sort);
virtual void requestModVersions(const ModPlatform::IndexedPack& current); inline bool canFetchMore(const QModelIndex& parent) const override { return searchState == CanPossiblyFetchMore; };
public slots: public slots:
virtual void searchRequestFinished(QJsonDocument& doc) = 0; void searchRequestFinished(QJsonDocument& doc);
void searchRequestFailed(QString reason); void searchRequestFailed(QString reason);
void versionRequestSucceeded(QJsonDocument doc, QString addonId); void versionRequestSucceeded(QJsonDocument doc, QString addonId);
@ -53,6 +54,8 @@ class ListModel : public QAbstractListModel {
void performPaginatedSearch(); void performPaginatedSearch();
protected: protected:
virtual void loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) = 0;
virtual QJsonArray documentToArray(QJsonDocument& obj) const = 0;
virtual const char** getSorts() const = 0; virtual const char** getSorts() const = 0;
void requestLogo(QString file, QString url); void requestLogo(QString file, QString url);
@ -73,6 +76,5 @@ class ListModel : public QAbstractListModel {
enum SearchState { None, CanPossiblyFetchMore, ResetRequested, Finished } searchState = None; enum SearchState { None, CanPossiblyFetchMore, ResetRequested, Finished } searchState = None;
NetJob::Ptr jobPtr; NetJob::Ptr jobPtr;
QByteArray response;
}; };
} // namespace ModPlatform } // namespace ModPlatform

View File

@ -1,53 +1,7 @@
#include "FlameModModel.h" #include "FlameModModel.h"
#include "FlameModPage.h"
#include "minecraft/PackProfile.h"
#include <Json.h>
namespace FlameMod { namespace FlameMod {
ListModel::ListModel(FlameModPage* parent) : ModPlatform::ListModel(parent) {} const char* ListModel::sorts[6]{ "Featured", "Popularity", "LastUpdated", "Name", "Author", "TotalDownloads" };
ListModel::~ListModel() {}
void FlameMod::ListModel::searchRequestFinished(QJsonDocument& doc)
{
jobPtr.reset();
QList<ModPlatform::IndexedPack> newList;
auto packs = doc.array();
for(auto packRaw : packs) {
auto packObj = packRaw.toObject();
ModPlatform::IndexedPack pack;
try
{
FlameMod::loadIndexedPack(pack, packObj);
newList.append(pack);
}
catch(const JSONValidationError &e)
{
qWarning() << "Error while loading mod from Flame: " << e.cause();
continue;
}
}
if(packs.size() < 25) {
searchState = Finished;
} else {
nextSearchOffset += 25;
searchState = CanPossiblyFetchMore;
}
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
modpacks.append(newList);
endInsertRows();
}
const char* sorts[6]{ "Featured", "Popularity", "LastUpdated", "Name", "Author", "TotalDownloads" };
const char** FlameMod::ListModel::getSorts() const
{
return sorts;
}
} // namespace FlameMod } // namespace FlameMod

View File

@ -1,39 +1,25 @@
#pragma once #pragma once
#include <RWStorage.h>
#include <QIcon>
#include <QList>
#include <QMetaType>
#include <QSortFilterProxyModel>
#include <QString>
#include <QStringList>
#include <QStyledItemDelegate>
#include <QThreadPool>
#include <net/NetJob.h>
#include <functional>
#include "BaseInstance.h"
#include "FlameModPage.h" #include "FlameModPage.h"
#include "modplatform/flame/FlameModIndex.h" #include "modplatform/flame/FlameModIndex.h"
namespace FlameMod { namespace FlameMod {
typedef std::function<void(QString)> LogoCallback;
class ListModel : public ModPlatform::ListModel { class ListModel : public ModPlatform::ListModel {
Q_OBJECT Q_OBJECT
public: public:
ListModel(FlameModPage* parent); ListModel(FlameModPage* parent) : ModPlatform::ListModel(parent) {}
virtual ~ListModel(); ;
virtual ~ListModel() = default;
private slots:
void searchRequestFinished(QJsonDocument& doc) override;
private: private:
const char** getSorts() const override; void loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) override { FlameMod::loadIndexedPack(m, obj); };
QJsonArray documentToArray(QJsonDocument& obj) const override { return obj.array(); };
static const char* sorts[6];
const char** getSorts() const override { return sorts; };
}; };
} // namespace Modrinth } // namespace FlameMod

View File

@ -1,49 +1,7 @@
#include "ModrinthModel.h" #include "ModrinthModel.h"
#include "ModrinthPage.h"
#include "minecraft/MinecraftInstance.h"
#include <Json.h>
namespace Modrinth { namespace Modrinth {
ListModel::ListModel(ModrinthPage* parent) : ModPlatform::ListModel(parent) {} const char* ListModel::sorts[5] { "relevance", "downloads", "follows", "updated", "newest" };
ListModel::~ListModel() {}
void Modrinth::ListModel::searchRequestFinished(QJsonDocument& doc)
{
jobPtr.reset();
QList<ModPlatform::IndexedPack> newList;
auto packs = doc.object().value("hits").toArray();
for (auto packRaw : packs) {
auto packObj = packRaw.toObject();
ModPlatform::IndexedPack pack;
try {
Modrinth::loadIndexedPack(pack, packObj);
newList.append(pack);
} catch (const JSONValidationError& e) {
qWarning() << "Error while loading mod from Modrinth: " << e.cause();
continue;
}
}
if (packs.size() < 25) {
searchState = Finished;
} else {
nextSearchOffset += 25;
searchState = CanPossiblyFetchMore;
}
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
modpacks.append(newList);
endInsertRows();
}
const char* sorts[5]{ "relevance", "downloads", "follows", "updated", "newest" };
const char** Modrinth::ListModel::getSorts() const
{
return sorts;
}
} // namespace Modrinth } // namespace Modrinth

View File

@ -1,39 +1,24 @@
#pragma once #pragma once
#include <RWStorage.h>
#include <QIcon>
#include <QList>
#include <QMetaType>
#include <QSortFilterProxyModel>
#include <QString>
#include <QStringList>
#include <QStyledItemDelegate>
#include <QThreadPool>
#include <net/NetJob.h>
#include <functional>
#include "BaseInstance.h"
#include "ModrinthPage.h" #include "ModrinthPage.h"
#include "modplatform/modrinth/ModrinthPackIndex.h" #include "modplatform/modrinth/ModrinthPackIndex.h"
namespace Modrinth { namespace Modrinth {
typedef std::function<void(QString)> LogoCallback;
class ListModel : public ModPlatform::ListModel { class ListModel : public ModPlatform::ListModel {
Q_OBJECT Q_OBJECT
public: public:
ListModel(ModrinthPage* parent); ListModel(ModrinthPage* parent) : ModPlatform::ListModel(parent){};
virtual ~ListModel(); virtual ~ListModel() = default;
public slots:
void searchRequestFinished(QJsonDocument& doc) override;
private: private:
const char** getSorts() const override; void loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) override { Modrinth::loadIndexedPack(m, obj); };
QJsonArray documentToArray(QJsonDocument& obj) const override { return obj.object().value("hits").toArray(); };
static const char* sorts[5];
const char** getSorts() const override { return sorts; };
}; };
} // namespace Modrinth } // namespace Modrinth