1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-04-20 03:37:47 +00:00

Add QCoro for coroutine goodness

This commit is contained in:
Joshua Goins 2023-09-16 18:37:42 -04:00
parent 7605ce7a53
commit 2b3ee3da89
5 changed files with 60 additions and 42 deletions

View file

@ -48,6 +48,9 @@ find_package(Qt6 ${QT_MIN_VERSION} REQUIRED COMPONENTS
WebView)
find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS Kirigami2 I18n Config CoreAddons)
find_package(PkgConfig REQUIRED)
find_package(QCoro6 REQUIRED COMPONENTS Core Network Qml)
qcoro_enable_coroutines()
qt_policy(SET QTP0001 NEW)
if (ENABLE_WATCHDOG)

View file

@ -86,7 +86,10 @@ target_link_libraries(astra PRIVATE
KF6::I18n
KF6::ConfigCore
KF6::ConfigGui
KF6::CoreAddons)
KF6::CoreAddons
QCoro::Core
QCoro::Network
QCoro::Qml)
if (ENABLE_WATCHDOG)
target_sources(astra PRIVATE

View file

@ -7,6 +7,7 @@
#include <QNetworkAccessManager>
#include <QProcess>
#include <QtQml>
#include <qcorotask.h>
#include "accountmanager.h"
#include "headline.h"
@ -208,6 +209,8 @@ private:
bool checkIfInPath(const QString &program);
QCoro::Task<> fetchNews();
SteamAPI *steamApi = nullptr;
bool m_loadingFinished = false;

View file

@ -8,6 +8,7 @@
#include <QProcess>
#include <QStandardPaths>
#include <algorithm>
#include <qcoronetworkreply.h>
#include <utility>
#ifdef ENABLE_GAMEMODE
@ -574,6 +575,11 @@ void LauncherCore::setSquareEnixLoginServer(const QString &value)
}
void LauncherCore::refreshNews()
{
fetchNews();
}
QCoro::Task<> LauncherCore::fetchNews()
{
QUrlQuery query;
query.addQueryItem("lang", "en-us");
@ -594,56 +600,56 @@ void LauncherCore::refreshNews()
.toUtf8());
auto reply = mgr->get(request);
QObject::connect(reply, &QNetworkReply::finished, [this, reply] {
auto document = QJsonDocument::fromJson(reply->readAll());
co_await reply;
auto headline = new Headline(this);
if (document.isEmpty()) {
headline->failedToLoad = true;
auto document = QJsonDocument::fromJson(reply->readAll());
auto headline = new Headline(this);
if (document.isEmpty()) {
headline->failedToLoad = true;
}
const auto parseNews = [](QJsonObject object) -> News {
News news;
news.date = QDateTime::fromString(object["date"].toString(), Qt::DateFormat::ISODate);
news.id = object["id"].toString();
news.tag = object["tag"].toString();
news.title = object["title"].toString();
if (object["url"].toString().isEmpty()) {
news.url = QUrl(QString("https://na.finalfantasyxiv.com/lodestone/news/detail/%1").arg(news.id));
} else {
news.url = QUrl(object["url"].toString());
}
const auto parseNews = [](QJsonObject object) -> News {
News news;
news.date = QDateTime::fromString(object["date"].toString(), Qt::DateFormat::ISODate);
news.id = object["id"].toString();
news.tag = object["tag"].toString();
news.title = object["title"].toString();
return news;
};
if (object["url"].toString().isEmpty()) {
news.url = QUrl(QString("https://na.finalfantasyxiv.com/lodestone/news/detail/%1").arg(news.id));
} else {
news.url = QUrl(object["url"].toString());
}
for (auto bannerObject : document.object()["banner"].toArray()) {
auto banner = Banner();
banner.link = QUrl(bannerObject.toObject()["link"].toString());
banner.bannerImage = QUrl(bannerObject.toObject()["lsb_banner"].toString());
return news;
};
headline->banners.push_back(banner);
}
for (auto bannerObject : document.object()["banner"].toArray()) {
auto banner = Banner();
banner.link = QUrl(bannerObject.toObject()["link"].toString());
banner.bannerImage = QUrl(bannerObject.toObject()["lsb_banner"].toString());
for (auto newsObject : document.object()["news"].toArray()) {
auto news = parseNews(newsObject.toObject());
headline->news.push_back(news);
}
headline->banners.push_back(banner);
}
for (auto pinnedObject : document.object()["pinned"].toArray()) {
auto pinned = parseNews(pinnedObject.toObject());
headline->pinned.push_back(pinned);
}
for (auto newsObject : document.object()["news"].toArray()) {
auto news = parseNews(newsObject.toObject());
headline->news.push_back(news);
}
for (auto pinnedObject : document.object()["topics"].toArray()) {
auto pinned = parseNews(pinnedObject.toObject());
headline->topics.push_back(pinned);
}
for (auto pinnedObject : document.object()["pinned"].toArray()) {
auto pinned = parseNews(pinnedObject.toObject());
headline->pinned.push_back(pinned);
}
for (auto pinnedObject : document.object()["topics"].toArray()) {
auto pinned = parseNews(pinnedObject.toObject());
headline->topics.push_back(pinned);
}
m_headline = headline;
Q_EMIT newsChanged();
});
m_headline = headline;
Q_EMIT newsChanged();
}
Headline *LauncherCore::headline()

View file

@ -8,6 +8,7 @@
#include <QCommandLineParser>
#include <QQuickStyle>
#include <QtWebView>
#include <qcoroqml.h>
#include "astra-version.h"
#include "compatibilitytoolinstaller.h"
@ -76,6 +77,8 @@ int main(int argc, char *argv[])
}
#endif
QCoro::Qml::registerTypes();
QQmlApplicationEngine engine;
auto core = engine.singletonInstance<LauncherCore *>(QStringLiteral("zone.xiv.astra"), QStringLiteral("LauncherCore"));