some little refactor, deleting assets

This commit is contained in:
Blue 2024-01-21 16:22:56 -03:00
parent 7a116bfdf2
commit 5c4bd18cdc
Signed by: blue
GPG key ID: 9B203B252A63EE38
21 changed files with 139 additions and 36 deletions

View file

@ -3,7 +3,7 @@
#include "assets.h"
#include "API/helpers.h"
#include "utils/helpers.h"
Models::Assets::Assets (QObject* parent):
QAbstractListModel(parent),
@ -45,14 +45,11 @@ void Models::Assets::addAssets (const std::deque<Asset>& assets) {
void Models::Assets::deleteAsset (unsigned int id) {
QModelIndex index = getIndex(id);
if (!index.isValid())
throw std::runtime_error("An attempt to insert to delete non existing Asset from asset model");
throw std::runtime_error("An attempt to delete non existing Asset from asset model");
int row = index.row();
beginRemoveRows(QModelIndex(), row, row);
records.erase(records.begin() + row);
if (state == State::syncronized) //give a second thought
state = State::initial;
endRemoveRows();
}
@ -67,9 +64,9 @@ int Models::Assets::rowCount (const QModelIndex& parent) const {
}
QHash<int, QByteArray> Models::Assets::roleNames () const {
static const QHash<int, QByteArray> roleNames{
{Title, "title"}, {Icon, "icon"}, {Balance, "balance"}, {Archived, "archived"}, {Color, "color"}
};
static const QHash<int, QByteArray> roleNames({
{Title, "title"}, {Icon, "icon"}, {Balance, "balance"}, {Archived, "archived"}, {Color, "color"}, {Id, "assetId"}
});
return roleNames;
}
@ -103,6 +100,8 @@ QVariant Models::Assets::data (const QModelIndex& index, int role) const {
return records[row].archived;
case Color:
return records[row].color;
case Id:
return records[row].id;
}
}
@ -122,8 +121,7 @@ bool Models::Assets::deserialize (const QVariantList& from, std::deque<Asset>& o
asset.id = ser.value("id").toUInt();
uint32_t color = ser.value("color").toUInt();
uint8_t* rgba = reinterpret_cast<uint8_t*>(&color);
asset.color = QColor::fromRgb(rgba[0], rgba[1], rgba[2], rgba[3]);
asset.color = QColor::fromRgba(color);
}
return true;

View file

@ -33,7 +33,8 @@ public:
Icon,
Balance,
Archived,
Color
Color,
Id
};
void clear();

View file

@ -6,8 +6,8 @@
#include <QDebug>
#include "API/api.h"
#include "API/helpers.h"
#include "API/codes.h"
#include "utils/helpers.h"
Models::Magpie::Magpie (QObject* parent):
QObject(parent),
@ -190,6 +190,17 @@ bool Models::Magpie::handleChanges (const QVariantMap& changes) {
else
Magpie::assets.addAssets(added);
}
aItr = assets.constFind("removed");
if (aItr != assets.constEnd() && aItr.value().canConvert<QVariantList>()) {
const QVariantList rem = qast<QVariantList>(aItr.value());
for (const QVariant& vId : rem) {
if (vId.isValid() && vId.canConvert<unsigned int>())
Magpie::assets.deleteAsset(vId.toUInt());
else
qDebug() << "Error deserializing removed assets";
}
}
}
return true;

View file

@ -52,6 +52,7 @@ signals:
void stateChanged(State state);
void storeTokens(const QString& access, const QString& renew);
void displayError(const QString& err);
public:
Assets assets;