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
6 changed files with 65 additions and 170 deletions

View File

@ -1,6 +1,6 @@
#include "ModModel.h"
#include "ModPage.h"
#include "Json.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "ui/dialogs/ModDownloadDialog.h"
@ -11,18 +11,6 @@ namespace ModPlatform {
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
{
int pos = index.row();
@ -73,16 +61,6 @@ void ListModel::logoFailed(QString 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)
{
if (parent.isValid()) return;
@ -140,6 +118,38 @@ void ListModel::searchWithTerm(const QString& term, const int sort)
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)
{
if (jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) {