diff --git a/src/CalcWidget.cpp b/src/CalcWidget.cpp index 91c0cab..69050d7 100644 --- a/src/CalcWidget.cpp +++ b/src/CalcWidget.cpp @@ -10,6 +10,8 @@ #include "utils/AppData.h" #include "utils/ColorScheme.h" #include "utils/config.h" +#include "utils/WebsocketClient.h" +#include "utils/WebsocketNotifier.h" CalcWidget::CalcWidget(QWidget *parent) : QWidget(parent) @@ -44,6 +46,13 @@ CalcWidget::CalcWidget(QWidget *parent) QTimer::singleShot(1, [this]{ this->skinChanged(); }); + + m_statusTimer.start(5000); + connect(&m_statusTimer, &QTimer::timeout, this, &CalcWidget::updateStatus); + QPixmap warningIcon = QPixmap(":/assets/images/warning.png"); + ui->icon_warning->setPixmap(warningIcon.scaledToWidth(32, Qt::SmoothTransformation)); + + this->updateStatus(); } void CalcWidget::convert(bool reverse) { @@ -86,6 +95,7 @@ void CalcWidget::onPricesReceived() { ui->btn_configure->setEnabled(true); this->initComboBox(); m_comboBoxInit = true; + this->updateStatus(); } void CalcWidget::initComboBox() { @@ -144,4 +154,18 @@ void CalcWidget::setupComboBox(QComboBox *comboBox, const QStringList &crypto, c comboBox->addItems(fiat); } +void CalcWidget::updateStatus() { + if (!m_comboBoxInit) { + ui->label_warning->setText("Waiting on exchange data."); + ui->frame_warning->show(); + } + else if (websocketNotifier()->stale(10)) { + ui->label_warning->setText("No new exchange rates received for over 10 minutes."); + ui->frame_warning->show(); + } + else { + ui->frame_warning->hide(); + } +} + CalcWidget::~CalcWidget() = default; \ No newline at end of file diff --git a/src/CalcWidget.h b/src/CalcWidget.h index 2a1a291..3c5da01 100644 --- a/src/CalcWidget.h +++ b/src/CalcWidget.h @@ -6,6 +6,7 @@ #include #include +#include namespace Ui { class CalcWidget; @@ -30,9 +31,11 @@ private slots: private: void convert(bool reverse); void setupComboBox(QComboBox *comboBox, const QStringList &crypto, const QStringList &fiat); + void updateStatus(); QScopedPointer ui; bool m_comboBoxInit = false; + QTimer m_statusTimer; }; #endif // FEATHER_CALCWIDGET_H diff --git a/src/CalcWidget.ui b/src/CalcWidget.ui index 3fa1db2..1c4bf02 100644 --- a/src/CalcWidget.ui +++ b/src/CalcWidget.ui @@ -7,7 +7,7 @@ 0 0 800 - 132 + 242 @@ -15,41 +15,51 @@ - - - - - Crypto/fiat and fiat/fiat calculator. - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Configure - - - - - - - - - Qt::Horizontal + + + QFrame::StyledPanel + + QFrame::Raised + + + + + + + 0 + 0 + + + + icon + + + + + + + Qt::Horizontal + + + QSizePolicy::Maximum + + + + 10 + 20 + + + + + + + + Warning text + + + + @@ -122,12 +132,36 @@ - - - Exchange rates are updated every 2 minutes. + + + Qt::Horizontal + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Configure + + + + + diff --git a/src/utils/WebsocketNotifier.cpp b/src/utils/WebsocketNotifier.cpp index ee1edd7..ad436df 100644 --- a/src/utils/WebsocketNotifier.cpp +++ b/src/utils/WebsocketNotifier.cpp @@ -20,6 +20,7 @@ QPointer WebsocketNotifier::m_instance(nullptr); void WebsocketNotifier::onWSMessage(const QJsonObject &msg) { QString cmd = msg.value("cmd").toString(); + m_lastMessageReceived = QDateTime::currentDateTimeUtc(); m_cache[cmd] = msg; if (cmd == "blockheights") { @@ -92,6 +93,10 @@ void WebsocketNotifier::emitCache() { } } +bool WebsocketNotifier::stale(int minutes) { + return m_lastMessageReceived < QDateTime::currentDateTimeUtc().addSecs(-(minutes*60)); +} + void WebsocketNotifier::onWSNodes(const QJsonArray &nodes) { // TODO: Refactor, should be filtered client side diff --git a/src/utils/WebsocketNotifier.h b/src/utils/WebsocketNotifier.h index 5fb7eed..388a9d4 100644 --- a/src/utils/WebsocketNotifier.h +++ b/src/utils/WebsocketNotifier.h @@ -27,6 +27,8 @@ public: static WebsocketNotifier* instance(); void emitCache(); + bool stale(int minutes); + signals: void BlockHeightsReceived(int mainnet, int stagenet); void NodesReceived(QList &L); @@ -54,6 +56,7 @@ private: static QPointer m_instance; QHash m_cache; + QDateTime m_lastMessageReceived; }; inline WebsocketNotifier* websocketNotifier()