refactor: use function cb instead of class cb in getModInfo
I've discovered functional programming :^) This makes this route more fit for general use. Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
158b7fd166
commit
74c6c5cfbc
@ -73,7 +73,7 @@ class ModAPI {
|
|||||||
};
|
};
|
||||||
|
|
||||||
virtual void searchMods(CallerType* caller, SearchArgs&& args) const = 0;
|
virtual void searchMods(CallerType* caller, SearchArgs&& args) const = 0;
|
||||||
virtual void getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack) = 0;
|
virtual void getModInfo(ModPlatform::IndexedPack& pack, std::function<void(QJsonDocument&, ModPlatform::IndexedPack&)> callback) = 0;
|
||||||
|
|
||||||
virtual auto getProject(QString addonId, QByteArray* response) const -> NetJob* = 0;
|
virtual auto getProject(QString addonId, QByteArray* response) const -> NetJob* = 0;
|
||||||
virtual auto getProjects(QStringList addonIds, QByteArray* response) const -> NetJob* = 0;
|
virtual auto getProjects(QStringList addonIds, QByteArray* response) const -> NetJob* = 0;
|
||||||
|
@ -31,22 +31,22 @@ void NetworkModAPI::searchMods(CallerType* caller, SearchArgs&& args) const
|
|||||||
netJob->start();
|
netJob->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkModAPI::getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack)
|
void NetworkModAPI::getModInfo(ModPlatform::IndexedPack& pack, std::function<void(QJsonDocument&, ModPlatform::IndexedPack&)> callback)
|
||||||
{
|
{
|
||||||
auto response = new QByteArray();
|
auto response = new QByteArray();
|
||||||
auto job = getProject(pack.addonId.toString(), response);
|
auto job = getProject(pack.addonId.toString(), response);
|
||||||
|
|
||||||
QObject::connect(job, &NetJob::succeeded, caller, [caller, &pack, response] {
|
QObject::connect(job, &NetJob::succeeded, [callback, &pack, response] {
|
||||||
QJsonParseError parse_error{};
|
QJsonParseError parse_error{};
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
||||||
if (parse_error.error != QJsonParseError::NoError) {
|
if (parse_error.error != QJsonParseError::NoError) {
|
||||||
qWarning() << "Error while parsing JSON response from " << caller->debugName() << " at " << parse_error.offset
|
qWarning() << "Error while parsing JSON response for mod info at " << parse_error.offset
|
||||||
<< " reason: " << parse_error.errorString();
|
<< " reason: " << parse_error.errorString();
|
||||||
qWarning() << *response;
|
qWarning() << *response;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
caller->infoRequestFinished(doc, pack);
|
callback(doc, pack);
|
||||||
});
|
});
|
||||||
|
|
||||||
job->start();
|
job->start();
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
class NetworkModAPI : public ModAPI {
|
class NetworkModAPI : public ModAPI {
|
||||||
public:
|
public:
|
||||||
void searchMods(CallerType* caller, SearchArgs&& args) const override;
|
void searchMods(CallerType* caller, SearchArgs&& args) const override;
|
||||||
void getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack) override;
|
void getModInfo(ModPlatform::IndexedPack& pack, std::function<void(QJsonDocument&, ModPlatform::IndexedPack&)> callback) override;
|
||||||
void getVersions(CallerType* caller, VersionSearchArgs&& args) const override;
|
void getVersions(CallerType* caller, VersionSearchArgs&& args) const override;
|
||||||
|
|
||||||
auto getProject(QString addonId, QByteArray* response) const -> NetJob* override;
|
auto getProject(QString addonId, QByteArray* response) const -> NetJob* override;
|
||||||
|
@ -102,7 +102,8 @@ void ListModel::performPaginatedSearch()
|
|||||||
|
|
||||||
void ListModel::requestModInfo(ModPlatform::IndexedPack& current)
|
void ListModel::requestModInfo(ModPlatform::IndexedPack& current)
|
||||||
{
|
{
|
||||||
m_parent->apiProvider()->getModInfo(this, current);
|
m_parent->apiProvider()->getModInfo(
|
||||||
|
current, [this](QJsonDocument& doc, ModPlatform::IndexedPack& pack) { infoRequestFinished(doc, pack); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListModel::refresh()
|
void ListModel::refresh()
|
||||||
|
Loading…
Reference in New Issue
Block a user