Debugged changing of the assets

This commit is contained in:
Blue 2024-04-07 20:02:00 -03:00
parent aa815a5bd7
commit 4ed280a550
Signed by: blue
GPG Key ID: 9B203B252A63EE38
6 changed files with 51 additions and 3 deletions

View File

@ -15,6 +15,7 @@
#include "requests/currencies.h"
#include "requests/addasset.h"
#include "requests/deleteasset.h"
#include "requests/updateasset.h"
API::API (Models::Magpie& magpie, QObject* parent):
QObject(parent),
@ -136,7 +137,17 @@ API::RequestId API::updateAsset (
const QColor& color,
Models::Currency::ID currency,
const QJSValue& finished
) {}
) {
qDebug() << "Updating asset...";
if (magpie.getState() != Models::Magpie::Authenticated)
return callCallback(finished, "Can not update assets in current state"), 0;
auto update = std::make_unique<Request::UpdateAsset>(id, title, icon, color, currency, magpie.getAddress());
update->setAuthorizationToken(magpie.getAccessToken());
connect(update.get(), &Request::UpdateAsset::success, std::bind(&API::callCallback, this, finished, QString(), QJSValueList{QJSValue(true)}));
connect(update.get(), &Request::UpdateAsset::error, std::bind(&API::callCallback, this, finished, std::placeholders::_1, QJSValueList{QJSValue(false)}));
return registerAndSend(std::move(update));
}
API::RequestId API::deleteAsset (unsigned int id, const QJSValue& finished) {
qDebug() << "Deleting asset...";

View File

@ -47,6 +47,14 @@ public slots:
Models::Currency::ID currency,
const QJSValue& finished = QJSValue()
);
RequestId updateAsset(
Models::Asset::ID id,
const QString& title,
const QString& icon,
const QColor& color,
Models::Currency::ID currency,
const QJSValue& finished = QJSValue()
);
RequestId deleteAsset(unsigned int id, const QJSValue& finished = QJSValue());
private slots:

View File

@ -57,6 +57,20 @@ void Models::Assets::remove (Asset::ID id) {
endRemoveRows();
}
void Models::Assets::change (const Asset& asset) {
int index = getIndexByID(asset.id);
if (index == -1)
throw std::runtime_error("An attempt to change non existing Asset in asset model");
records[index] = asset;
emit dataChanged(createIndex(index, 0, &records[index]), createIndex(index, 0, &records[index]));
}
void Models::Assets::change (const std::deque<Asset>& assets) {
for (const Asset& asset : assets)
change(asset);
}
Models::Asset Models::Assets::get (Asset::ID id) const {
int index = getIndexByID(id);
if (index == -1)
@ -163,7 +177,7 @@ QVariantMap Models::Assets::getAssetByIndex (int index) const {
return result;
QModelIndex idx = createIndex(index, 0, &records[index]);
for (int role = Roles::Title; role != Roles::ID; ++role)
for (int role = Roles::Title; role <= Roles::ID; ++role)
result[roles[role]] = data(idx, role);
return result;

View File

@ -47,6 +47,8 @@ public:
void add (const Asset& asset);
void add (const std::deque<Asset>& assets);
void remove (Asset::ID id);
void change (const Asset& asset);
void change (const std::deque<Asset>& assets);
Asset get(Asset::ID id) const;

View File

@ -221,6 +221,15 @@ bool Models::Magpie::handleAssetChanges (const QVariantMap& changes) {
}
}
aItr = changes.constFind("changed");
if (aItr != changes.constEnd() && aItr.value().canConvert<QVariantList>()) {
std::deque<Models::Asset> changes;
if (!Models::Assets::deserialize(qast<QVariantList>(aItr.value()), changes))
qDebug() << "Error deserializng changed assets";
else
assets.change(changes);
}
return true;
}

View File

@ -63,6 +63,7 @@ Item {
Button {
id: editButton
text: qsTr("Edit")
icon.name: "entry-edit"
flat: true
height: parent.height
onClicked: edit(assetID)
@ -71,9 +72,12 @@ Item {
Button {
id: deleteButton
text: qsTr("Delete")
flat: true
icon.name: "delete"
height: parent.height
onClicked: remove(assetID)
palette { //unfortunately doesn't work anymore
button: "red"
}
}
}
}