tested currencies retrieval, now assets display the currency
This commit is contained in:
parent
45f924a4cf
commit
cf2f387f58
14 changed files with 137 additions and 27 deletions
|
@ -6,13 +6,14 @@
|
|||
#include "utils/helpers.h"
|
||||
|
||||
const QHash<int, QByteArray> Models::Assets::roles({
|
||||
{Title, "title"}, {Icon, "icon"}, {Balance, "balance"}, {Archived, "archived"}, {Color, "color"}, {Id, "assetId"}
|
||||
{Title, "title"}, {Icon, "icon"}, {Balance, "balance"}, {Archived, "archived"}, {Color, "color"}, {Currency, "currency"}, {Id, "assetId"}
|
||||
});
|
||||
|
||||
Models::Assets::Assets (QObject* parent):
|
||||
Models::Assets::Assets (Currencies& currencies, QObject* parent):
|
||||
QAbstractListModel(parent),
|
||||
records(),
|
||||
state(State::initial)
|
||||
state(State::initial),
|
||||
currencies(currencies)
|
||||
{}
|
||||
|
||||
void Models::Assets::clear () {
|
||||
|
@ -101,6 +102,8 @@ QVariant Models::Assets::data (const QModelIndex& index, int role) const {
|
|||
return records[row].archived;
|
||||
case Color:
|
||||
return records[row].color;
|
||||
case Currency:
|
||||
return currencies.getCode(records[row].currency);
|
||||
case Id:
|
||||
return records[row].id;
|
||||
}
|
||||
|
@ -120,6 +123,7 @@ bool Models::Assets::deserialize (const QVariantList& from, std::deque<Asset>& o
|
|||
asset.icon = ser.value("icon").toString();
|
||||
asset.archived = ser.value("archived").toBool();
|
||||
asset.id = ser.value("id").toUInt();
|
||||
asset.currency = ser.value("currency").toUInt();
|
||||
|
||||
uint32_t color = ser.value("color").toUInt();
|
||||
asset.color = QColor::fromRgba(color);
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <QAbstractListModel>
|
||||
#include <qqmlregistration.h>
|
||||
|
||||
#include "currencies.h"
|
||||
|
||||
namespace Models {
|
||||
struct Asset {
|
||||
unsigned int id;
|
||||
|
@ -26,7 +28,7 @@ class Assets : public QAbstractListModel {
|
|||
QML_ELEMENT
|
||||
|
||||
public:
|
||||
explicit Assets (QObject* parent = nullptr);
|
||||
explicit Assets (Currencies& currencies, QObject* parent = nullptr);
|
||||
|
||||
enum Roles {
|
||||
Title = Qt::UserRole + 1,
|
||||
|
@ -34,6 +36,7 @@ public:
|
|||
Balance,
|
||||
Archived,
|
||||
Color,
|
||||
Currency,
|
||||
Id
|
||||
};
|
||||
|
||||
|
@ -72,5 +75,6 @@ private:
|
|||
|
||||
State state;
|
||||
std::deque<Asset> records;
|
||||
Currencies& currencies;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -69,6 +69,15 @@ void Models::Currencies::remove (Currency::ID id) {
|
|||
endRemoveRows();
|
||||
}
|
||||
|
||||
QString Models::Currencies::getCode (Currency::ID id) {
|
||||
Map::iterator mItr = map.find(id);
|
||||
if (mItr != map.end())
|
||||
return mItr->second->code;
|
||||
|
||||
//todo request;
|
||||
return "Requesting...";
|
||||
}
|
||||
|
||||
int Models::Currencies::rowCount (const QModelIndex& parent) const {
|
||||
//For list models only the root node (an invalid parent) should return the
|
||||
//list's size. For all other (valid) parents, rowCount() should return 0 so
|
||||
|
|
|
@ -47,6 +47,8 @@ public:
|
|||
void add (const std::deque<Currency>& currencies);
|
||||
void remove (Currency::ID id);
|
||||
|
||||
QString getCode(Currency::ID id);
|
||||
|
||||
//Basic functionality:
|
||||
int rowCount (const QModelIndex& parent = QModelIndex()) const override;
|
||||
QHash<int, QByteArray> roleNames () const override;
|
||||
|
|
|
@ -11,7 +11,8 @@
|
|||
|
||||
Models::Magpie::Magpie (QObject* parent):
|
||||
QObject(parent),
|
||||
assets(),
|
||||
currencies(),
|
||||
assets(currencies),
|
||||
address(),
|
||||
state(State::Offline),
|
||||
accessToken(),
|
||||
|
@ -23,7 +24,7 @@ Models::Magpie::Magpie (QObject* parent):
|
|||
{
|
||||
firstPoll.setSingleShot(true);
|
||||
firstPoll.setInterval(2000);
|
||||
connect(&firstPoll, &QTimer::timeout, this, &Magpie::onFirstPollTimerSuccess);
|
||||
connect(&firstPoll, &QTimer::timeout, this, &Magpie::onPositivePoll);
|
||||
connect(&assets, &Assets::requestAssets, this, &Magpie::requestAssets);
|
||||
}
|
||||
|
||||
|
@ -109,13 +110,14 @@ void Models::Magpie::requestAssets () {
|
|||
assets.add(result);
|
||||
},
|
||||
[this] (const QString& error, const std::optional<QVariantMap>& data) {
|
||||
assets.add(std::deque<Asset>());
|
||||
qDebug() << "Error receiving assets:" << error;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void Models::Magpie::onFirstPollTimerSuccess () {
|
||||
setState(Authenticated);
|
||||
void Models::Magpie::onPositivePoll () {
|
||||
if (state == Authenticating)
|
||||
requestCurrencies();
|
||||
}
|
||||
|
||||
void Models::Magpie::startPolling () {
|
||||
|
@ -149,8 +151,10 @@ void Models::Magpie::onPollSuccess (const QVariantMap& data) {
|
|||
clear = handleChanges(qast<QVariantMap>(data.value("data")));
|
||||
//todo handle the result
|
||||
case Codes::Poll::timeout:
|
||||
setState(Authenticated);
|
||||
return sendPoll(clear);
|
||||
onPositivePoll();
|
||||
if (state == Authenticating || state == Authenticated)
|
||||
return sendPoll(clear);
|
||||
|
||||
case Codes::Poll::tokenProblem:
|
||||
case Codes::Poll::replace:
|
||||
case Codes::Poll::unknownError: //todo this one doesn't actually mean that we can't work for now, the network may be temporarily down or something
|
||||
|
@ -240,5 +244,27 @@ void Models::Magpie::requestCurrencies () {
|
|||
if (requestingCurrencies)
|
||||
return;
|
||||
|
||||
requestingCurrencies = true;
|
||||
currencies.clear();
|
||||
api->requestCurrencies(
|
||||
[this] (const QVariantList& list) {
|
||||
std::deque<Currency> result;
|
||||
bool res = Currencies::deserialize(list, result);
|
||||
if (!res) {
|
||||
qDebug() << "Error deserializer received currencies";
|
||||
result.clear();
|
||||
} else {
|
||||
qDebug() << "Currencies successfully received";
|
||||
}
|
||||
|
||||
currencies.add(result);
|
||||
requestingCurrencies = false;
|
||||
setState(Authenticated);
|
||||
},
|
||||
[this] (const QString& error, const std::optional<QVariantMap>& data) {
|
||||
qDebug() << "Error receiving currencies:" << error;
|
||||
requestingCurrencies = false;
|
||||
setState(NotAuthenticated);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -58,12 +58,12 @@ signals:
|
|||
void displayError(const QString& err);
|
||||
|
||||
public:
|
||||
Assets assets;
|
||||
Currencies currencies;
|
||||
Assets assets;
|
||||
|
||||
private slots:
|
||||
void requestAssets();
|
||||
void onFirstPollTimerSuccess();
|
||||
void onPositivePoll();
|
||||
|
||||
private:
|
||||
void startPolling();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue