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:
flow 2022-07-18 19:15:02 -03:00
parent 158b7fd166
commit 74c6c5cfbc
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
4 changed files with 8 additions and 7 deletions

View File

@ -73,7 +73,7 @@ class ModAPI {
};
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 getProjects(QStringList addonIds, QByteArray* response) const -> NetJob* = 0;

View File

@ -31,22 +31,22 @@ void NetworkModAPI::searchMods(CallerType* caller, SearchArgs&& args) const
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 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{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
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();
qWarning() << *response;
return;
}
caller->infoRequestFinished(doc, pack);
callback(doc, pack);
});
job->start();

View File

@ -5,7 +5,7 @@
class NetworkModAPI : public ModAPI {
public:
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;
auto getProject(QString addonId, QByteArray* response) const -> NetJob* override;

View File

@ -102,7 +102,8 @@ void ListModel::performPaginatedSearch()
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()