//SPDX-FileCopyrightText: 2023 Yury Gubich //SPDX-License-Identifier: GPL-3.0-or-later #pragma once #include #include #include #include #include "currencies.h" #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) Q_PROPERTY(Currencies* currencies READ getCurrencies CONSTANT) explicit Magpie(QObject *parent = nullptr); QUrl getAddress() const; State getState() const; QString getAccessToken() const; QString getRenewToken() const; void installAPI(const std::shared_ptr& 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(); Currencies* getCurrencies(); signals: void addressChanged(const QUrl& path); void stateChanged(State state); void storeTokens(const QString& access, const QString& renew); void displayError(const QString& err); public: Assets assets; Currencies currencies; private slots: void requestAssets(); void onFirstPollTimerSuccess(); private: void startPolling(); void sendPoll(bool clear = false); void onPollSuccess(const QVariantMap& data); void onPollError(const QString& err, const std::optional& data); bool handleChanges(const QVariantMap& changes); bool handleAssetChanges(const QVariantMap& changes); bool handleCurrenciesChanges(const QVariantMap& changes); void resetAllModels(); void requestCurrencies(); private: QUrl address; State state; QString accessToken; QString renewToken; std::shared_ptr api; QTimer firstPoll; unsigned int pollRequestId; bool requestingCurrencies; }; }