Merge pull request 'Include Satoshi ticker, include Monero ticker, and introduce 24h pct change for WOW' (#57) from dsc/wowlet:refactor-tickers into master

Reviewed-on: https://git.wownero.com/wowlet/wowlet/pulls/57
This commit is contained in:
wowario 2021-05-10 17:00:02 +00:00
commit ac1ed1873a
6 changed files with 138 additions and 67 deletions

View file

@ -7,6 +7,7 @@
#include <QCoreApplication>
#include <QSystemTrayIcon>
#include <QMessageBox>
#include <QGroupBox>
#include <QFileDialog>
#include "mainwindow.h"
@ -129,15 +130,29 @@ MainWindow::MainWindow(AppContext *ctx, QWidget *parent) :
#endif
// ticker widgets
m_tickerWidgets.append(new TickerWidget(this, "WOW"));
m_tickerWidgets.append(new TickerWidget(this, "BTC"));
for(auto tickerWidget: m_tickerWidgets) {
ui->tickerLayout->addWidget(tickerWidget);
}
m_tickerWOW = new TickerWidget(this, "WOW");
ui->tickerLayout->addWidget(m_tickerWOW);
m_balanceWidget = new TickerWidget(this, "WOW", "Balance", true, true);
m_tickerSAT = new TickerWidget(this, "Satoshi");
m_tickerSAT->hidePct(true);
ui->tickerLayout->addWidget(m_tickerSAT);
m_tickerBTC = new TickerWidget(this, "BTC");
ui->tickerLayout->addWidget(m_tickerBTC);
m_tickerXMR = new TickerWidget(this, "XMR");
ui->tickerLayout->addWidget(m_tickerXMR);
m_balanceWidget = new TickerWidget(this, "Balance");
m_balanceWidget->hidePct(true);
ui->fiatTickerLayout->addWidget(m_balanceWidget);
connect(m_tickerWOW, &TickerWidget::reload, this, &MainWindow::onUpdateWowWidget);
connect(m_tickerBTC, &TickerWidget::reload, this, &MainWindow::onUpdateBTCWidget);
connect(m_tickerSAT, &TickerWidget::reload, this, &MainWindow::onUpdateSATWidget);
connect(m_tickerSAT, &TickerWidget::reload, this, &MainWindow::onUpdateXMRWidget);
connect(m_balanceWidget, &TickerWidget::reload, this, &MainWindow::onUpdateFiatBalanceWidget);
// Send widget
connect(ui->sendWidget, &SendWidget::createTransaction, m_ctx, QOverload<const QString, quint64, const QString, bool>::of(&AppContext::onCreateTransaction));
connect(ui->sendWidget, &SendWidget::createTransactionMultiDest, m_ctx, &AppContext::onCreateTransactionMultiDest);
@ -252,10 +267,7 @@ MainWindow::MainWindow(AppContext *ctx, QWidget *parent) :
}
// settings connects
// Update ticker widget(s) on home tab when settings preferred fiat currency is changed
for(auto tickerWidget: m_tickerWidgets)
connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, tickerWidget, &TickerWidget::init);
connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, m_balanceWidget, &TickerWidget::init);
connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, this, &MainWindow::onUpdateFiatBalanceWidget);
connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, m_ctx, &AppContext::onPreferredFiatCurrencyChanged);
connect(m_windowSettings, &Settings::preferredFiatCurrencyChanged, ui->sendWidget, QOverload<>::of(&SendWidget::onPreferredFiatCurrencyChanged));
@ -632,7 +644,6 @@ void MainWindow::onBalanceUpdated(quint64 balance, quint64 spendable) {
m_statusLabelBalance->setToolTip("Click for details");
m_statusLabelBalance->setText(label_str);
m_balanceWidget->setHidden(hide);
}
void MainWindow::setStatusText(const QString &text, bool override, int timeout) {
@ -1322,6 +1333,85 @@ void MainWindow::showBalanceDialog() {
dialog->deleteLater();
}
void MainWindow::onUpdateSATWidget() {
if(!AppContext::prices->markets.count() || !AppContext::prices->rates.count())
return;
auto wowObj = AppContext::prices->markets["WOW"];
QString satStr = QString::number(wowObj.price_sat);
m_tickerSAT->setFiatText(satStr);
}
void MainWindow::onUpdateWowWidget() {
if(!AppContext::prices->markets.count() || !AppContext::prices->rates.count())
return;
QString fiatCurrency = config()->get(Config::preferredFiatCurrency).toString();
if(!AppContext::prices->rates.contains(fiatCurrency)){
config()->set(Config::preferredFiatCurrency, "USD");
fiatCurrency = "USD";
}
auto wowObj = AppContext::prices->markets["WOW"];
auto currencyText = Utils::amountToCurrencyString(wowObj.price_usd, fiatCurrency);
m_tickerWOW->setFiatText(currencyText);
auto pct24h = AppContext::prices->markets["WOW"].price_usd_change_pct_24h;
auto pct24hText = QString::number(pct24h, 'f', 2);
this->m_tickerWOW->setPctText(pct24hText, pct24h >= 0.0);
}
void MainWindow::onUpdateBTCWidget() {
if(!AppContext::prices->markets.count() || !AppContext::prices->rates.count())
return;
QString fiatCurrency = config()->get(Config::preferredFiatCurrency).toString();
if(!AppContext::prices->rates.contains(fiatCurrency)){
config()->set(Config::preferredFiatCurrency, "USD");
fiatCurrency = "USD";
}
double conversion = AppContext::prices->convert("BTC", fiatCurrency, 1.0);
auto conversionText = Utils::amountToCurrencyString(conversion, fiatCurrency);
m_tickerBTC->setFiatText(conversionText);
auto pct24h = AppContext::prices->markets["BTC"].price_usd_change_pct_24h;
auto pct24hText = QString::number(pct24h, 'f', 2);
this->m_tickerBTC->setPctText(pct24hText, pct24h >= 0.0);
}
void MainWindow::onUpdateXMRWidget() {
if(!AppContext::prices->markets.count() || !AppContext::prices->rates.count())
return;
QString fiatCurrency = config()->get(Config::preferredFiatCurrency).toString();
if(!AppContext::prices->rates.contains(fiatCurrency)){
config()->set(Config::preferredFiatCurrency, "USD");
fiatCurrency = "USD";
}
double conversion = AppContext::prices->convert("XMR", fiatCurrency, 1.0);
auto conversionText = Utils::amountToCurrencyString(conversion, fiatCurrency);
m_tickerXMR->setFiatText(conversionText);
auto pct24h = AppContext::prices->markets["XMR"].price_usd_change_pct_24h;
auto pct24hText = QString::number(pct24h, 'f', 2);
this->m_tickerXMR->setPctText(pct24hText, pct24h >= 0.0);
}
void MainWindow::onUpdateFiatBalanceWidget() {
bool hide = config()->get(Config::hideBalance).toBool();
m_balanceWidget->setHidden(hide);
if(!AppContext::prices->markets.count() || !AppContext::prices->rates.count())
return;
QString fiatCurrency = config()->get(Config::preferredFiatCurrency).toString();
if(!AppContext::prices->rates.contains(fiatCurrency)){
config()->set(Config::preferredFiatCurrency, "USD");
fiatCurrency = "USD";
}
double conversion = AppContext::prices->convert("WOW", fiatCurrency, AppContext::balance);
auto conversionText = Utils::amountToCurrencyString(conversion, fiatCurrency);
m_balanceWidget->setFiatText(conversionText);
}
QString MainWindow::statusDots() {
m_statusDots++;
m_statusDots = m_statusDots % 4;

View file

@ -147,6 +147,13 @@ public slots:
void onCreateTransactionSuccess(PendingTransaction *tx, const QVector<QString> &address);
void onTransactionCommitted(bool status, PendingTransaction *tx, const QStringList& txid);
// tickers
void onUpdateWowWidget();
void onUpdateFiatBalanceWidget();
void onUpdateBTCWidget();
void onUpdateSATWidget();
void onUpdateXMRWidget();
signals:
void closed();
@ -191,7 +198,10 @@ private:
QAction *m_trayActionSend;
QAction *m_trayActionHistory;
QList<TickerWidget*> m_tickerWidgets;
TickerWidget *m_tickerWOW;
TickerWidget *m_tickerBTC;
TickerWidget *m_tickerSAT;
TickerWidget *m_tickerXMR;
TickerWidget *m_balanceWidget;
// lower status bar

View file

@ -41,7 +41,9 @@ void Prices::cryptoPricesReceived(const QJsonArray &data) {
ms.symbol = obj.value("symbol").toString().toUpper();
ms.image = obj.value("image").toString();
ms.name = obj.value("name").toString();
ms.price_usd = obj.value("current_price").toDouble();
ms.price_usd = obj.value("current_price").toDouble(0);
ms.price_btc = obj.value("current_price_btc").toDouble(0.0);
ms.price_sat = obj.value("current_price_satoshi").toInt(0);
ms.price_usd_change_pct_24h = obj.value("price_change_percentage_24h").toDouble();
if(ms.price_usd <= 0) continue;

View file

@ -15,6 +15,8 @@ struct marketStruct {
QString name;
QString image;
double price_usd;
double price_btc;
uint price_sat;
double price_usd_change_pct_24h;
};

View file

@ -6,67 +6,36 @@
#include "mainwindow.h"
TickerWidget::TickerWidget(QWidget *parent, QString symbol, QString title, bool convertBalance, bool hidePercent) :
TickerWidget::TickerWidget(QWidget *parent, QString title) :
QWidget(parent),
ui(new Ui::TickerWidget),
m_symbol(std::move(symbol)),
m_convertBalance(convertBalance),
m_hidePercent(hidePercent)
ui(new Ui::TickerWidget)
{
ui->setupUi(this);
m_ctx = MainWindow::getContext();
// default values before API data
if (title == "") title = m_symbol;
this->ui->tickerBox->setTitle(title);
QString defaultPct = "0.0";
QString defaultFiat = "...";
this->setFontSizes();
this->setPctText(defaultPct, true);
this->setFiatText(defaultFiat, 0.0);
this->setFiatText(defaultFiat);
this->setTitleText(title);
ui->tickerPct->setHidden(hidePercent);
connect(AppContext::prices, &Prices::fiatPricesUpdated, this, &TickerWidget::init);
connect(AppContext::prices, &Prices::cryptoPricesUpdated, this, &TickerWidget::init);
if (convertBalance)
connect(m_ctx, &AppContext::balanceUpdated, this, &TickerWidget::init);
connect(AppContext::prices, &Prices::fiatPricesUpdated, this, &TickerWidget::reload);
connect(AppContext::prices, &Prices::cryptoPricesUpdated, this, &TickerWidget::reload);
connect(m_ctx, &AppContext::balanceUpdated, this, &TickerWidget::reload);
}
void TickerWidget::init() {
if(!AppContext::prices->markets.count() || !AppContext::prices->rates.count())
return;
QString fiatCurrency = config()->get(Config::preferredFiatCurrency).toString();
if(!AppContext::prices->rates.contains(fiatCurrency)){
config()->set(Config::preferredFiatCurrency, "USD");
return;
}
double amount = m_convertBalance ? AppContext::balance : 1.0;
double conversion = AppContext::prices->convert(m_symbol, fiatCurrency, amount);
if (conversion < 0) return;
auto markets = AppContext::prices->markets;
if(!markets.contains(m_symbol)) return;
bool hidePercent = (conversion == 0 || m_hidePercent);
if (hidePercent) {
ui->tickerPct->hide();
} else {
auto pct24h = markets[m_symbol].price_usd_change_pct_24h;
auto pct24hText = QString::number(pct24h, 'f', 2);
this->setPctText(pct24hText, pct24h >= 0.0);
}
this->setFiatText(fiatCurrency, conversion);
void TickerWidget::setFiatText(QString &text) {
ui->tickerFiat->setText(text);
}
void TickerWidget::setFiatText(QString &fiatCurrency, double amount) {
QString conversionText = Utils::amountToCurrencyString(amount, fiatCurrency);
ui->tickerFiat->setText(conversionText);
void TickerWidget::setTitleText(QString text) {
ui->tickerBox->setTitle(text);
}
void TickerWidget::hidePct(bool hide) {
ui->tickerPct->setHidden(hide);
}
void TickerWidget::setPctText(QString &text, bool positive) {

View file

@ -17,21 +17,19 @@ class TickerWidget : public QWidget
Q_OBJECT
public:
explicit TickerWidget(QWidget *parent, QString symbol, QString title = "", bool convertBalance = false, bool hidePercent = false);
void setFiatText(QString &fiatCurrency, double amount);
explicit TickerWidget(QWidget *parent, QString title = "");
void setFiatText(QString &text);
void setTitleText(QString text);
void setPctText(QString &text, bool positive);
void setFontSizes();
void hidePct(bool hide);
~TickerWidget() override;
public slots:
void init();
signals:
void reload();
private:
Ui::TickerWidget *ui;
QString m_symbol;
bool m_convertBalance;
bool m_hidePercent;
AppContext *m_ctx;
};