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) WebView)
find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS Kirigami2 I18n Config CoreAddons) find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS Kirigami2 I18n Config CoreAddons)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
find_package(QCoro6 REQUIRED COMPONENTS Core Network Qml)
qcoro_enable_coroutines()
qt_policy(SET QTP0001 NEW) qt_policy(SET QTP0001 NEW)
if (ENABLE_WATCHDOG) if (ENABLE_WATCHDOG)

View file

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

View file

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

View file

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

View file

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