magpie/models/magpie.h

91 lines
2.2 KiB
C
Raw Normal View History

2024-01-20 21:17:21 +00:00
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory.h>
#include <QObject>
#include <QQmlEngine>
#include <QTimer>
2024-01-24 21:00:15 +00:00
#include "currencies.h"
2024-01-20 21:17:21 +00:00
#include "assets.h"
class API;
namespace Models {
class Magpie : public QObject {
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
public:
enum State {
Offline,
NoServer,
NotAuthenticated,
Authenticating,
Authenticated
};
Q_ENUM(State)
Q_PROPERTY(QUrl address READ getAddress NOTIFY addressChanged)
Q_PROPERTY(State state READ getState NOTIFY stateChanged)
Q_PROPERTY(Assets* assets READ getAssets CONSTANT)
2024-01-24 21:00:15 +00:00
Q_PROPERTY(Currencies* currencies READ getCurrencies CONSTANT)
2024-01-20 21:17:21 +00:00
explicit Magpie(QObject *parent = nullptr);
QUrl getAddress() const;
State getState() const;
QString getAccessToken() const;
QString getRenewToken() const;
void installAPI(const std::shared_ptr<API>& api);
void setAddress(const QUrl& address);
void forceAddress(const QUrl& address);
void setTokens(const QString access, const QString& renew, bool notify = false);
void setState(State newState);
Assets* getAssets();
2024-01-24 21:00:15 +00:00
Currencies* getCurrencies();
2024-01-20 21:17:21 +00:00
signals:
void addressChanged(const QUrl& path);
void stateChanged(State state);
void storeTokens(const QString& access, const QString& renew);
2024-01-21 19:22:56 +00:00
void displayError(const QString& err);
2024-01-20 21:17:21 +00:00
public:
2024-01-24 21:00:15 +00:00
Currencies currencies;
Assets assets;
2024-01-20 21:17:21 +00:00
private slots:
void requestAssets();
void onPositivePoll();
2024-01-20 21:17:21 +00:00
private:
void startPolling();
void sendPoll(bool clear = false);
void onPollSuccess(const QVariantMap& data);
void onPollError(const QString& err, const std::optional<QVariantMap>& data);
bool handleChanges(const QVariantMap& changes);
2024-01-24 21:00:15 +00:00
bool handleAssetChanges(const QVariantMap& changes);
bool handleCurrenciesChanges(const QVariantMap& changes);
2024-01-20 21:17:21 +00:00
void resetAllModels();
2024-01-24 21:00:15 +00:00
void requestCurrencies();
2024-01-20 21:17:21 +00:00
private:
QUrl address;
State state;
QString accessToken;
QString renewToken;
std::shared_ptr<API> api;
QTimer firstPoll;
unsigned int pollRequestId;
2024-01-24 21:00:15 +00:00
bool requestingCurrencies;
2024-01-20 21:17:21 +00:00
};
}