debugging

This commit is contained in:
Blue 2024-01-18 19:14:33 -03:00
parent 6e16292f06
commit 27124380e4
Signed by: blue
GPG Key ID: 9B203B252A63EE38
8 changed files with 76 additions and 52 deletions

View File

@ -290,7 +290,8 @@ void API::addAsset(const QString& title, const QString& icon, const QJSValue &fi
request.setRawHeader("Authorization", "Bearer " + accessToken.toUtf8());
QNetworkReply* reply = network.post(request, params.toString(QUrl::FullyEncoded).toUtf8());
connect(reply, &QNetworkReply::finished,
std::bind(&API::onAssetAdded, this, reply, finished)
this, std::bind(&API::onAssetAdded, this, reply, finished),
Qt::QueuedConnection
);
}
@ -313,18 +314,16 @@ void API::sendPoll(bool clear) {
if (clear)
url.setQuery(QUrlQuery({{"clearCache", "all"}}));
QByteArray authorizationHeader = "Bearer " + accessToken.toUtf8();
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, json);
request.setRawHeader("Authorization", authorizationHeader);
request.setRawHeader("Authorization", "Bearer " + accessToken.toUtf8());
request.setTransferTimeout(30000);
pollReply = std::unique_ptr<QNetworkReply>(network.get(request));
connect(
pollReply.get(), &QNetworkReply::finished,
this, &API::onPollFinished,
Qt::QueuedConnection
);
this, &API::onPollFinished, Qt::QueuedConnection
);
}
void API::onPollFinished() {
@ -371,40 +370,33 @@ void API::onPollFinished() {
bool API::handleChanges(const QVariantMap& changes) {
QVariantMap::ConstIterator itr = changes.constFind("system");
if (itr != changes.constEnd()) {
const QVariant& vsys = itr.value();
if (vsys.canConvert<QVariantMap>()) {
const QVariantMap& sys = qast<QVariantMap>(vsys);
QVariantMap::ConstIterator invItr = sys.constFind("invalidate");
if (invItr != sys.constEnd()) {
const QVariant& vinv = invItr.value();
if (vinv.canConvert<bool>() && vinv.toBool())
resetAllModels();
}
if (itr != changes.constEnd() && itr.value().canConvert<QVariantMap>()) {
const QVariantMap& sys = qast<QVariantMap>(itr.value());
QVariantMap::ConstIterator invItr = sys.constFind("invalidate");
if (invItr != sys.constEnd()) {
const QVariant& vinv = invItr.value();
if (vinv.canConvert<bool>() && vinv.toBool())
resetAllModels();
}
}
itr = changes.constFind("assets");
if (itr != changes.constEnd()) {
const QVariant& vassets = itr.value();
if (vassets.canConvert<QVariantMap>()) {
const QVariantMap& assets = qast<QVariantMap>(vassets);
QVariantMap::ConstIterator aItr = assets.constFind("invalidate");
if (aItr != assets.constEnd()) {
const QVariant& vinv = aItr.value();
if (vinv.canConvert<bool>() && vinv.toBool())
API::assets.clear();
}
if (itr != changes.constEnd() && itr.value().canConvert<QVariantMap>()) {
const QVariantMap& assets = qast<QVariantMap>(itr.value());
QVariantMap::ConstIterator aItr = assets.constFind("invalidate");
if (aItr != assets.constEnd()) {
const QVariant& vinv = aItr.value();
if (vinv.canConvert<bool>() && vinv.toBool())
API::assets.clear();
}
aItr = assets.constFind("added");
if (aItr != assets.constEnd()) {
const QVariant& vadd = aItr.value();
std::deque<Models::Asset> added;
if (!Models::Assets::deserialize(qast<QVariantList>(itr.value()), added))
qDebug() << "Error deserializng added assets";
else
API::assets.addAssets(added);
}
aItr = assets.constFind("added");
if (aItr != assets.constEnd() && aItr.value().canConvert<QVariantList>()) {
std::deque<Models::Asset> added;
if (!Models::Assets::deserialize(qast<QVariantList>(aItr.value()), added))
qDebug() << "Error deserializng added assets";
else
API::assets.addAssets(added);
}
}
@ -428,14 +420,13 @@ void API::requestAssets() {
qDebug() << "Requesting assets...";
QUrl url = createUrl("/listAssets");
QByteArray authorizationHeader = "Bearer " + accessToken.toUtf8();
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, json);
request.setRawHeader("Authorization", authorizationHeader);
request.setRawHeader("Authorization", "Bearer " + accessToken.toUtf8());
QNetworkReply* reply = network.get(request);
connect(
pollReply.get(), &QNetworkReply::finished,
reply, &QNetworkReply::finished,
this, std::bind(&API::responseAssets, this, reply)
);
}

View File

@ -82,6 +82,7 @@ void Models::Assets::fetchMore (const QModelIndex& parent) {
return;
state = State::requesting;
emit requestAssets();
}
QVariant Models::Assets::data (const QModelIndex& index, int role) const {
@ -116,7 +117,7 @@ bool Models::Assets::deserialize (const QVariantList& from, std::deque<Asset>& o
asset.title = ser.value("title").toString();
asset.icon = ser.value("icon").toString();
asset.archived = ser.value("archived").toBool();
asset.id = ser.value("archived").toUInt();
asset.id = ser.value("id").toUInt();
}
return true;

View File

@ -22,6 +22,7 @@ struct Asset {
class Assets : public QAbstractListModel {
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
public:
explicit Assets (QObject* parent = nullptr);

View File

@ -3,16 +3,20 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import magpie.API
import magpie.Models as Models
import magpie.Components as Components
Item {
signal add
Column {
ColumnLayout {
id: column
anchors.fill: parent
Label {
id: label
text: "This is Assets screen"
font {
pixelSize: 24
@ -21,12 +25,12 @@ Item {
}
ListView {
anchors.centerIn: parent
model: API.assets
delegate: Rectangle {
Text {
text: title
}
id: listView
Layout.fillHeight: true
Layout.fillWidth: true
model: Models.Assets
delegate: Components.AssetLine {
height: 20
}
}
}

View File

@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick
import QtQuick.Controls
Rectangle {
id: line
required property string title
required property string icon
Row {
anchors.fill: parent
IconLabel {
anchors.verticalCenter: parent.verticalCenter
icon.name: line.icon
width: parent.height
height: parent.height
}
Text {
anchors.verticalCenter: parent.verticalCenter
text: title
}
}
}

View File

@ -10,6 +10,7 @@ qt_add_qml_module(magpieComponents
NO_PLUGIN
QML_FILES
Modal.qml
AssetLine.qml
)
target_link_libraries(magpie PRIVATE magpieComponents)

View File

@ -66,7 +66,7 @@ Item {
Button {
text: qsTr("Create")
onClicked: inner.send(titleField.text, titleField.text, colorField.text)
onClicked: inner.send(titleField.text, iconField.text, colorField.text)
}
}
@ -79,7 +79,7 @@ Item {
return;
titleField.text = title;
titleField.text = icon;
iconField.text = icon;
colorField.text = color;
modal.inProgress = true;

View File

@ -38,9 +38,9 @@ Root::Root(const QUrl& root, int& argc, char* argv[]) :
connect(&api, &API::addressChanged, this, &Root::onAPIAddressChanged);
connect(&api, &API::storeTokens, this, &Root::onStoreTokens);
qmlRegisterSingletonType<API>("magpie.API", 1, 0, "API", [this] (QQmlEngine *engine, QJSEngine *scriptEngine) {
return &api;
});
qmlRegisterSingletonInstance("magpie.API", 1, 0, "API", &api);
qmlRegisterSingletonInstance("magpie.Models", 1, 0, "Assets", &api.assets);
engine.addImportPath(":/");
engine.load(root);