tested currencies retrieval, now assets display the currency
This commit is contained in:
parent
45f924a4cf
commit
cf2f387f58
19
API/api.cpp
19
API/api.cpp
@ -11,7 +11,8 @@
|
||||
#include "requests/register.h"
|
||||
#include "requests/login.h"
|
||||
#include "requests/poll.h"
|
||||
#include "requests/listassets.h"
|
||||
#include "requests/assets.h"
|
||||
#include "requests/currencies.h"
|
||||
#include "requests/addasset.h"
|
||||
#include "requests/deleteasset.h"
|
||||
|
||||
@ -93,10 +94,20 @@ API::RequestId API::sendLogin (const QString& login, const QString& password, co
|
||||
API::RequestId API::requestAssets (const SuccessListHandler& success, const ErrorHandler& error) {
|
||||
qDebug() << "Requesting assets...";
|
||||
|
||||
auto list = std::make_unique<Request::ListAssets>(magpie.getAddress());
|
||||
auto list = std::make_unique<Request::Assets>(magpie.getAddress());
|
||||
list->setAuthorizationToken(magpie.getAccessToken());
|
||||
connect(list.get(), &Request::ListAssets::success, success);
|
||||
connect(list.get(), &Request::ListAssets::error, error);
|
||||
connect(list.get(), &Request::Assets::success, success);
|
||||
connect(list.get(), &Request::Assets::error, error);
|
||||
return registerAndSend(std::move(list));
|
||||
}
|
||||
|
||||
API::RequestId API::requestCurrencies (const SuccessListHandler& success, const ErrorHandler& error) {
|
||||
qDebug() << "Requesting currencies...";
|
||||
|
||||
auto list = std::make_unique<Request::Currencies>(magpie.getAddress());
|
||||
list->setAuthorizationToken(magpie.getAccessToken());
|
||||
connect(list.get(), &Request::Currencies::success, success);
|
||||
connect(list.get(), &Request::Currencies::error, error);
|
||||
return registerAndSend(std::move(list));
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ public:
|
||||
explicit API(Models::Magpie& magpie, QObject* parent = nullptr);
|
||||
|
||||
RequestId requestAssets(const SuccessListHandler& success, const ErrorHandler& error);
|
||||
RequestId requestCurrencies(const SuccessListHandler& success, const ErrorHandler& error);
|
||||
RequestId poll(const SuccessMapHandler& success, const ErrorHandler& error, bool clear = false);
|
||||
|
||||
static const RequestId none = 0;
|
||||
|
@ -8,7 +8,8 @@ set(HEADERS
|
||||
register.h
|
||||
login.h
|
||||
poll.h
|
||||
listassets.h
|
||||
assets.h
|
||||
currencies.h
|
||||
addasset.h
|
||||
deleteasset.h
|
||||
)
|
||||
@ -20,7 +21,8 @@ set(SOURCES
|
||||
register.cpp
|
||||
login.cpp
|
||||
poll.cpp
|
||||
listassets.cpp
|
||||
assets.cpp
|
||||
currencies.cpp
|
||||
addasset.cpp
|
||||
deleteasset.cpp
|
||||
)
|
||||
|
@ -1,14 +1,14 @@
|
||||
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "listassets.h"
|
||||
#include "assets.h"
|
||||
|
||||
#include "utils/helpers.h"
|
||||
|
||||
Request::ListAssets::ListAssets (const QUrl& baseUrl):
|
||||
Request(createUrl(baseUrl, "/listAssets")) {}
|
||||
Request::Assets::Assets (const QUrl& baseUrl):
|
||||
Request(createUrl(baseUrl, "/assets")) {}
|
||||
|
||||
void Request::ListAssets::onSuccess (const QVariantMap& data) {
|
||||
void Request::Assets::onSuccess (const QVariantMap& data) {
|
||||
QVariantMap::ConstIterator itr = data.find("assets");
|
||||
if (itr == data.constEnd() || !itr->canConvert<QVariantList>())
|
||||
return Request::onError("Error receiving assets: assets are missing or not in an array", std::nullopt);
|
@ -7,11 +7,11 @@
|
||||
|
||||
namespace Request {
|
||||
|
||||
class ListAssets : public Request {
|
||||
class Assets : public Request {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ListAssets (const QUrl& baseUrl);
|
||||
Assets (const QUrl& baseUrl);
|
||||
|
||||
signals:
|
||||
void success(const QVariantList& assets);
|
18
API/requests/currencies.cpp
Normal file
18
API/requests/currencies.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "currencies.h"
|
||||
|
||||
#include "utils/helpers.h"
|
||||
|
||||
Request::Currencies::Currencies (const QUrl& baseUrl):
|
||||
Request(createUrl(baseUrl, "/currencies")) {}
|
||||
|
||||
void Request::Currencies::onSuccess (const QVariantMap& data) {
|
||||
QVariantMap::ConstIterator itr = data.find("currencies");
|
||||
if (itr == data.constEnd() || !itr->canConvert<QVariantList>())
|
||||
return Request::onError("Error receiving currencies: currencies are missing or not in an array", std::nullopt);
|
||||
|
||||
emit success(qast<QVariantList>(itr.value()));
|
||||
emit done();
|
||||
}
|
24
API/requests/currencies.h
Normal file
24
API/requests/currencies.h
Normal file
@ -0,0 +1,24 @@
|
||||
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "request.h"
|
||||
|
||||
namespace Request {
|
||||
|
||||
class Currencies : public Request::Request {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Currencies (const QUrl& baseUrl);
|
||||
|
||||
signals:
|
||||
void success(const QVariantList& assets);
|
||||
|
||||
protected:
|
||||
void onSuccess (const QVariantMap& data) override;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -12,6 +12,7 @@ Item {
|
||||
required property string icon
|
||||
required property color color
|
||||
required property string balance
|
||||
required property string currency
|
||||
required property int assetId
|
||||
|
||||
signal error (err:string)
|
||||
@ -39,7 +40,7 @@ Item {
|
||||
}
|
||||
|
||||
Text {
|
||||
width: parent.freespace / 2
|
||||
width: parent.freespace / 3
|
||||
height: parent.height
|
||||
text: title
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
@ -48,13 +49,21 @@ Item {
|
||||
}
|
||||
|
||||
Text {
|
||||
width: parent.freespace / 2
|
||||
width: parent.freespace / 3
|
||||
height: parent.height
|
||||
text: balance
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
color: palette.text
|
||||
}
|
||||
|
||||
Text {
|
||||
width: parent.freespace / 3
|
||||
height: parent.height
|
||||
text: currency
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
color: palette.text
|
||||
}
|
||||
|
||||
Button {
|
||||
id: deleteButton
|
||||
text: qsTr("Delete")
|
||||
|
Loading…
Reference in New Issue
Block a user