Recurring (#131)

* Save created recurring info

* Add to confirm dialog

* Update RPI at confirm

* Make singleton

* Add history to recurring payments

* USD/ZEC switch

* Fix check state

* recurring item serialization

* Add recurring payments to file

* Refactor

* Address view model

* Wire up dialog

* Update windows installer logos

* Store all payments in the store

* Save table geometry

* Add recurring payments view

* Add deletion

* Add recurring payment execution

* Add donation address to address book

* Add multiple payment handling

* Disable recurring for multiple payments

* Handle pay last

* Handle pay all

* Reomve frequency

* Enable recurring payments only for testnet

* For testing, allow payments in 5 min intervals

* Fix request money amounts
This commit is contained in:
adityapk00
2019-05-14 13:12:31 -07:00
committed by GitHub
parent cf3edd4aba
commit 41bc296f20
21 changed files with 1448 additions and 210 deletions

View File

@@ -62,7 +62,7 @@ void MainWindow::setupSendTab() {
// Disable custom fees if settings say no
ui->minerFeeAmt->setReadOnly(!Settings::getInstance()->getAllowCustomFees());
QObject::connect(ui->minerFeeAmt, &QLineEdit::textChanged, [=](auto txt) {
ui->lblMinerFeeUSD->setText(Settings::getUSDFormat(txt.toDouble()));
ui->lblMinerFeeUSD->setText(Settings::getUSDFromZecAmount(txt.toDouble()));
});
ui->minerFeeAmt->setText(Settings::getDecimalString(Settings::getMinerFee()));
@@ -70,7 +70,7 @@ void MainWindow::setupSendTab() {
QObject::connect(ui->tabWidget, &QTabWidget::currentChanged, [=] (int pos) {
if (pos == 1) {
QString txt = ui->minerFeeAmt->text();
ui->lblMinerFeeUSD->setText(Settings::getUSDFormat(txt.toDouble()));
ui->lblMinerFeeUSD->setText(Settings::getUSDFromZecAmount(txt.toDouble()));
}
});
@@ -86,30 +86,63 @@ void MainWindow::setupSendTab() {
// Recurring button
QObject::connect(ui->chkRecurring, &QCheckBox::stateChanged, [=] (int checked) {
if (checked) {
ui->btnRecurSchedule->setEnabled(true);
ui->btnRecurSchedule->setEnabled(true);
// If this is the first time the button is checked, open the edit schedule dialog
if (sendTxRecurringInfo == nullptr) {
ui->btnRecurSchedule->click();
}
} else {
ui->btnRecurSchedule->setEnabled(false);
ui->lblRecurDesc->setText("");
}
});
// Recurring schedule button
QObject::connect(ui->btnRecurSchedule, &QPushButton::clicked, this, &MainWindow::editSchedule);
// Hide the recurring section for now
ui->chkRecurring->setVisible(false);
ui->lblRecurDesc->setVisible(false);
ui->btnRecurSchedule->setVisible(false);
// Set the default state for the whole page
removeExtraAddresses();
clearSendForm();
}
void MainWindow::disableRecurring() {
if (!Settings::getInstance()->isTestnet()) {
ui->chkRecurring->setEnabled(false);
ui->btnRecurSchedule->setEnabled(false);
}
}
void MainWindow::editSchedule() {
// Open the edit schedule dialog
Recurring::showEditDialog(this, this, createTxFromSendPage());
// Only on testnet for now
if (!Settings::getInstance()->isTestnet()) {
QMessageBox::critical(this, "Not Supported yet",
"Recurring payments are only supported on Testnet for now.", QMessageBox::Ok);
return;
}
// Check to see that recurring payments are not selected when there are 2 or more addresses
if (ui->sendToWidgets->children().size()-1 > 2) {
QMessageBox::critical(this, tr("Cannot support multiple addresses"),
tr("Recurring payments doesn't currently support multiple addresses"), QMessageBox::Ok);
return;
}
// Open the edit schedule dialog
auto recurringInfo = Recurring::getInstance()->getNewRecurringFromTx(this, this,
createTxFromSendPage(), this->sendTxRecurringInfo);
if (recurringInfo == nullptr) {
// User pressed cancel.
// If there is no existing recurring info, uncheck the recurring box
if (sendTxRecurringInfo == nullptr) {
ui->chkRecurring->setCheckState(Qt::Unchecked);
}
}
else {
delete this->sendTxRecurringInfo;
this->sendTxRecurringInfo = recurringInfo;
ui->lblRecurDesc->setText(recurringInfo->getScheduleDescription());
}
}
void MainWindow::updateLabelsAutoComplete() {
@@ -192,7 +225,7 @@ void MainWindow::inputComboTextChanged(int index) {
auto balFmt = Settings::getZECDisplayFormat(bal);
ui->sendAddressBalance->setText(balFmt);
ui->sendAddressBalanceUSD->setText(Settings::getUSDFormat(bal));
ui->sendAddressBalanceUSD->setText(Settings::getUSDFromZecAmount(bal));
}
@@ -283,6 +316,14 @@ void MainWindow::addAddressSection() {
ui->sendToLayout->insertWidget(itemNumber-1, verticalGroupBox);
// Disable recurring payments if a address section is added, since recurring payments
// aren't supported for more than 1 address
delete sendTxRecurringInfo;
sendTxRecurringInfo = nullptr;
ui->lblRecurDesc->setText("");
ui->chkRecurring->setChecked(false);
ui->chkRecurring->setEnabled(false);
// Set focus into the address
Address1->setFocus();
@@ -297,7 +338,13 @@ void MainWindow::addressChanged(int itemNumber, const QString& text) {
void MainWindow::amountChanged(int item, const QString& text) {
auto usd = ui->sendToWidgets->findChild<QLabel*>(QString("AmtUSD") % QString::number(item));
usd->setText(Settings::getUSDFormat(text.toDouble()));
usd->setText(Settings::getUSDFromZecAmount(text.toDouble()));
// If there is a recurring payment, update the info there as well
if (sendTxRecurringInfo != nullptr) {
Recurring::getInstance()->updateInfoWithTx(sendTxRecurringInfo, createTxFromSendPage());
ui->lblRecurDesc->setText(sendTxRecurringInfo->getScheduleDescription());
}
}
void MainWindow::setMemoEnabled(int number, bool enabled) {
@@ -365,7 +412,7 @@ void MainWindow::memoButtonClicked(int number, bool includeReplyTo) {
}
}
void MainWindow::removeExtraAddresses() {
void MainWindow::clearSendForm() {
// The last one is a spacer, so ignore that
int totalItems = ui->sendToWidgets->children().size() - 2;
@@ -395,9 +442,15 @@ void MainWindow::removeExtraAddresses() {
}
// Reset the recurring button
if (Settings::getInstance()->isTestnet()) {
ui->chkRecurring->setEnabled(true);
}
ui->chkRecurring->setCheckState(Qt::Unchecked);
ui->btnRecurSchedule->setEnabled(false);
ui->lblRecurDesc->setText("");
delete sendTxRecurringInfo;
sendTxRecurringInfo = nullptr;
}
void MainWindow::maxAmountChecked(int checked) {
@@ -498,7 +551,7 @@ Tx MainWindow::createTxFromSendPage() {
return tx;
}
bool MainWindow::confirmTx(Tx tx) {
bool MainWindow::confirmTx(Tx tx, RecurringPaymentInfo* rpi) {
auto fnSplitAddressForWrap = [=] (const QString& a) -> QString {
if (! Settings::isZAddress(a)) return a;
@@ -507,6 +560,10 @@ bool MainWindow::confirmTx(Tx tx) {
return splitted;
};
// Update the recurring info with the latest Tx
if (rpi != nullptr) {
Recurring::getInstance()->updateInfoWithTx(rpi, tx);
}
// Show a confirmation dialog
QDialog d(this);
@@ -561,7 +618,7 @@ bool MainWindow::confirmTx(Tx tx) {
// Amount (USD)
auto AmtUSD = new QLabel(confirm.sendToAddrs);
AmtUSD->setObjectName(QString("AmtUSD") % QString::number(i + 1));
AmtUSD->setText(Settings::getUSDFormat(toAddr.amount));
AmtUSD->setText(Settings::getUSDFromZecAmount(toAddr.amount));
AmtUSD->setAlignment(Qt::AlignRight | Qt::AlignTrailing | Qt::AlignVCenter);
confirm.gridLayout->addWidget(AmtUSD, row, 2, 1, 1);
@@ -612,7 +669,7 @@ bool MainWindow::confirmTx(Tx tx) {
minerFeeUSD->setObjectName(QStringLiteral("minerFeeUSD"));
minerFeeUSD->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
confirm.gridLayout->addWidget(minerFeeUSD, row, 2, 1, 1);
minerFeeUSD->setText(Settings::getUSDFormat(tx.fee));
minerFeeUSD->setText(Settings::getUSDFromZecAmount(tx.fee));
if (Settings::getInstance()->getAllowCustomFees() && tx.fee != Settings::getMinerFee()) {
confirm.warningLabel->setVisible(true);
@@ -622,6 +679,15 @@ bool MainWindow::confirmTx(Tx tx) {
}
}
// Recurring payment info, show only if there is exactly one destination address
if (rpi == nullptr || tx.toAddrs.size() != 1) {
confirm.grpRecurring->setVisible(false);
}
else {
confirm.grpRecurring->setVisible(true);
confirm.lblRecurringDesc->setText(rpi->getScheduleDescription());
}
// Syncing warning
confirm.syncingWarning->setVisible(Settings::getInstance()->isSyncing());
@@ -637,13 +703,7 @@ bool MainWindow::confirmTx(Tx tx) {
confirm.sendFrom->setToolTip(tooltip);
// Show the dialog and submit it if the user confirms
if (d.exec() == QDialog::Accepted) {
// Then delete the additional fields from the sendTo tab
removeExtraAddresses();
return true;
} else {
return false;
}
return d.exec() == QDialog::Accepted;
}
// Send button clicked
@@ -661,23 +721,54 @@ void MainWindow::sendButton() {
// abort the Tx
return;
}
// Show a dialog to confirm the Tx
if (confirmTx(tx)) {
if (confirmTx(tx, sendTxRecurringInfo)) {
// If this is a recurring payment, save the hash so we can
// update the payment if it submits.
QString recurringPaymentHash;
// Recurring payments are enabled only if there is exactly 1 destination address.
if (sendTxRecurringInfo && tx.toAddrs.size() == 1) {
// Add it to the list
Recurring::getInstance()->addRecurringInfo(*sendTxRecurringInfo);
recurringPaymentHash = sendTxRecurringInfo->getHash();
}
// Then delete the additional fields from the sendTo tab
clearSendForm();
// And send the Tx
rpc->executeTransaction(tx,
// Submitted
[=] (QString opid) {
ui->statusBar->showMessage(tr("Computing Tx: ") % opid);
},
// Accepted
[=] (QString, QString txid) {
ui->statusBar->showMessage(Settings::txidStatusMessage + " " + txid);
// If this was a recurring payment, update the payment with the info
if (!recurringPaymentHash.isEmpty()) {
// Since this is the send button payment, this is the first payment
Recurring::getInstance()->updatePaymentItem(recurringPaymentHash, 0,
txid, "", PaymentStatus::COMPLETED);
}
},
// Errored out
[=] (QString opid, QString errStr) {
ui->statusBar->showMessage(QObject::tr(" Tx ") % opid % QObject::tr(" failed"), 15 * 1000);
if (!opid.isEmpty())
errStr = QObject::tr("The transaction with id ") % opid % QObject::tr(" failed. The error was") + ":\n\n" + errStr;
// If this was a recurring payment, update the payment with the failure
if (!recurringPaymentHash.isEmpty()) {
// Since this is the send button payment, this is the first payment
Recurring::getInstance()->updatePaymentItem(recurringPaymentHash, 0,
"", errStr, PaymentStatus::ERROR);
}
QMessageBox::critical(this, QObject::tr("Transaction Error"), errStr, QMessageBox::Ok);
}
);
@@ -705,6 +796,6 @@ QString MainWindow::doSendTxValidations(Tx tx) {
}
void MainWindow::cancelButton() {
removeExtraAddresses();
clearSendForm();
}