refactor: move NetJob away from ModModel to ModAPI

This is done so that 1. ModAPI behaves more like an actual API instead
of just a helper, and 2. Allows for more easily creating other mod
providers that may or may not use network tasks (foreshadowing lol)
This commit is contained in:
flow
2022-03-07 16:22:57 -03:00
parent 39bd04f06f
commit f714adf6d2
14 changed files with 230 additions and 135 deletions

View File

@ -10,19 +10,10 @@ ListModel::ListModel(ModrinthPage* parent) : ModPlatform::ListModel(parent) {}
ListModel::~ListModel() {}
void Modrinth::ListModel::searchRequestFinished()
void Modrinth::ListModel::searchRequestFinished(QJsonDocument& doc)
{
jobPtr.reset();
QJsonParseError parse_error;
QJsonDocument doc = QJsonDocument::fromJson(response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Modrinth at " << parse_error.offset
<< " reason: " << parse_error.errorString();
qWarning() << response;
return;
}
QList<ModPlatform::IndexedPack> newList;
auto packs = doc.object().value("hits").toArray();
for (auto packRaw : packs) {

View File

@ -29,8 +29,8 @@ class ListModel : public ModPlatform::ListModel {
ListModel(ModrinthPage* parent);
virtual ~ListModel();
private slots:
void searchRequestFinished() override;
public slots:
void searchRequestFinished(QJsonDocument& doc) override;
private:
const char** getSorts() const override;

View File

@ -35,22 +35,15 @@ ModrinthPage::ModrinthPage(ModDownloadDialog* dialog, BaseInstance* instance)
bool ModrinthPage::shouldDisplay() const { return true; }
void ModrinthPage::onRequestVersionsSucceeded(ModPage* instance, QByteArray* response, QString addonId)
void ModrinthPage::onRequestVersionsSucceeded(QJsonDocument& response, QString addonId)
{
if (addonId != current.addonId) { return; }
QJsonParseError parse_error;
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Modrinth at " << parse_error.offset
<< " reason: " << parse_error.errorString();
qWarning() << *response;
return;
}
QJsonArray arr = doc.array();
QJsonArray arr = response.array();
try {
Modrinth::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
} catch (const JSONValidationError& e) {
qDebug() << *response;
qDebug() << response;
qWarning() << "Error while reading Modrinth mod version: " << e.cause();
}
auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile();

View File

@ -22,5 +22,5 @@ class ModrinthPage : public ModPage {
bool shouldDisplay() const override;
private:
void onRequestVersionsSucceeded(ModPage*, QByteArray*, QString) override;
void onRequestVersionsSucceeded(QJsonDocument&, QString) override;
};