Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix max button on send page #396

Merged
merged 6 commits into from
May 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 110 additions & 13 deletions ui/send_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ type sendPage struct {

txAuthorErrChan chan error
broadcastErrChan chan error

walletSelected int
}

func (win *Window) SendPage(common pageCommon) layout.Widget {
Expand Down Expand Up @@ -141,6 +143,8 @@ func (win *Window) SendPage(common pageCommon) layout.Widget {
txAuthorErrChan: make(chan error),
}

pg.walletSelected = common.wallAcctSelector.selectedSendWallet

pg.accountSwitch = common.theme.SwitchButtonText([]decredmaterial.SwitchItem{{Text: "Address"}, {Text: "My account"}})

pg.balanceAfterSendValue = "- DCR"
Expand Down Expand Up @@ -684,6 +688,14 @@ func (pg *sendPage) validate(c pageCommon) bool {
isAmountValid = pg.validateRightAmount()
}

if pg.usdExchangeSet && !isAmountValid {
if pg.rightAmountEditor.Editor.Focused() {
pg.leftAmountEditor.Editor.SetText("")
} else {
pg.rightAmountEditor.Editor.SetText("")
}
}

if !pg.validateDestinationAddress(c) || !isAmountValid || pg.calculateErrorText != "" {
pg.nextButton.Background = pg.theme.Color.Hint
return false
Expand Down Expand Up @@ -752,7 +764,7 @@ func (pg *sendPage) inputsNotEmpty(editors ...*widget.Editor) bool {
return true
}

func (pg *sendPage) calculateValues(c pageCommon) {
func (pg *sendPage) calculateValues(c pageCommon, isUpdateAmountInput bool) {
defaultLeftValues := fmt.Sprintf("- %s", "DCR")
defaultRightValues := "($ -)"

Expand Down Expand Up @@ -782,13 +794,13 @@ func (pg *sendPage) calculateValues(c pageCommon) {
pg.amountDCRtoUSD = pg.inputAmount * pg.usdExchangeRate
}

pg.updateAmountInputsValues(c)
pg.updateAmountInputsValues(c, isUpdateAmountInput)
pg.getTxFee()
pg.updateDefaultValues()
pg.balanceAfterSend(false, c)
}

func (pg *sendPage) updateAmountInputsValues(c pageCommon) {
func (pg *sendPage) updateAmountInputsValues(c pageCommon, isUpdateAmountInput bool) {
switch {
case pg.leftExchangeValue == "USD" && pg.LastTradeRate != "" && pg.leftAmountEditor.Editor.Focused():
pg.rightAmountEditor.Editor.SetText(fmt.Sprintf("%f", pg.amountUSDtoDCR))
Expand All @@ -803,10 +815,12 @@ func (pg *sendPage) updateAmountInputsValues(c pageCommon) {
pg.rightAmountEditor.Editor.SetText(fmt.Sprintf("%f", pg.amountDCRtoUSD))
pg.setDestinationAddr(pg.inputAmount, c)
default:
if pg.rightAmountEditor.Editor.Focused() {
pg.leftAmountEditor.Editor.SetText(pg.rightAmountEditor.Editor.Text())
} else {
pg.rightAmountEditor.Editor.SetText(pg.leftAmountEditor.Editor.Text())
if isUpdateAmountInput {
if pg.rightAmountEditor.Editor.Focused() {
pg.leftAmountEditor.Editor.SetText(pg.rightAmountEditor.Editor.Text())
} else {
pg.rightAmountEditor.Editor.SetText(pg.leftAmountEditor.Editor.Text())
}
}
pg.setDestinationAddr(pg.inputAmount, c)
}
Expand Down Expand Up @@ -931,7 +945,7 @@ func (pg *sendPage) watchForBroadcastResult(c pageCommon) {
pg.resetFields()
c.modalLoad.setLoading(false)
pg.broadcastResult.TxHash = ""
pg.calculateValues(c)
pg.calculateValues(c, true)
pg.destinationAddressEditor.Editor.SetText("")
}
}
Expand All @@ -940,7 +954,7 @@ func (pg *sendPage) handleEditorChange(evt widget.EditorEvent, c pageCommon) {
switch evt.(type) {
case widget.ChangeEvent:
pg.fetchExchangeValue()
pg.calculateValues(c)
pg.calculateValues(c, true)
}
}

Expand All @@ -960,6 +974,17 @@ func (pg *sendPage) resetFields() {
pg.leftAmountEditor.Editor.SetText("")
pg.rightAmountEditor.Editor.SetText("")
pg.passwordEditor.Editor.SetText("")
pg.leftTotalCostValue = ""
pg.rightTotalCostValue = ""
}

func (pg *sendPage) resetErrorText() {
pg.amountErrorText = ""
pg.calculateErrorText = ""
pg.destinationAddressEditor.SetError("")
pg.leftAmountEditor.SetError("")
pg.rightAmountEditor.SetError("")
pg.passwordEditor.SetError("")
}

func (pg *sendPage) fetchExchangeValue() {
Expand All @@ -971,6 +996,62 @@ func (pg *sendPage) fetchExchangeValue() {
}()
}

func (pg *sendPage) setMaxAmount(c pageCommon) {

// Get spendable balance
sendWallet := c.info.Wallets[c.wallAcctSelector.selectedSendWallet]
sendAcct := sendWallet.Accounts[c.wallAcctSelector.selectedSendAccount]
atomValue := sendAcct.SpendableBalance

pg.updateAmountField(dcrutil.Amount(0).ToCoin())
pg.calculateValues(c, false)

if atomValue > 0 {
// Estimate max send value
amount, err := pg.txAuthor.EstimateMaxSendAmount()
if err == nil {
atomValue = amount.AtomValue
dcrValue := amount.DcrValue
pg.updateAmountField(dcrValue)
pg.calculateValues(c, false)
}

// Adjust value
step := int64(10)
for {
_, err := pg.txAuthor.EstimateFeeAndSize()
if err != nil {
atomValue -= step
pg.updateAmountField(dcrutil.Amount(atomValue).ToCoin())
pg.calculateValues(c, false)
} else {
pg.updateAmountField(dcrutil.Amount(atomValue).ToCoin())
pg.calculateValues(c, false)
break
}
}
}
}

func (pg *sendPage) updateAmountField(spendableBalanceDCR float64) {
if !pg.usdExchangeSet {
pg.leftAmountEditor.Editor.SetText(strconv.FormatFloat(spendableBalanceDCR, 'f', 7, 64))
} else {
pg.fetchExchangeValue()
pg.usdExchangeRate, _ = strconv.ParseFloat(pg.LastTradeRate, 64)
spendableBalanceUSD := spendableBalanceDCR * pg.usdExchangeRate

switch {
case pg.leftExchangeValue == "USD":
pg.leftAmountEditor.Editor.SetText(strconv.FormatFloat(spendableBalanceUSD, 'f', 7, 64))
pg.rightAmountEditor.Editor.SetText(strconv.FormatFloat(spendableBalanceDCR, 'f', 7, 64))
case pg.leftExchangeValue == "DCR":
pg.leftAmountEditor.Editor.SetText(strconv.FormatFloat(spendableBalanceDCR, 'f', 7, 64))
pg.rightAmountEditor.Editor.SetText(strconv.FormatFloat(spendableBalanceUSD, 'f', 7, 64))
}
}
}

func (pg *sendPage) Handle(c pageCommon) {
sendWallet := c.info.Wallets[c.wallAcctSelector.selectedSendWallet]
sendAcct := sendWallet.Accounts[c.wallAcctSelector.selectedSendAccount]
Expand All @@ -982,13 +1063,13 @@ func (pg *sendPage) Handle(c pageCommon) {
if pg.LastTradeRate == "" && pg.count == 0 {
pg.count = 1
pg.shouldInitializeTxAuthor = true
pg.calculateValues(c)
pg.calculateValues(c, true)
}

if (pg.LastTradeRate != "" && pg.count == 0) || (pg.LastTradeRate != "" && pg.count == 1) {
pg.count = 2
pg.shouldInitializeTxAuthor = true
pg.calculateValues(c)
pg.calculateValues(c, true)
}

pg.updateExchangeError()
Expand All @@ -1000,6 +1081,8 @@ func (pg *sendPage) Handle(c pageCommon) {
pg.sendToOption = pg.accountSwitch.SelectedOption()

if c.subPageBackButton.Button.Clicked() {
pg.resetErrorText()
pg.resetFields()
c.changePage(*c.returnPage)
}

Expand All @@ -1025,7 +1108,7 @@ func (pg *sendPage) Handle(c pageCommon) {
}

for range pg.destinationAddressEditor.Editor.Events() {
pg.calculateValues(c)
pg.calculateValues(c, true)
}

for pg.currencySwap.Clicked() {
Expand All @@ -1039,7 +1122,7 @@ func (pg *sendPage) Handle(c pageCommon) {
}
}

pg.calculateValues(c)
pg.calculateValues(c, true)
}

for _, evt := range pg.leftAmountEditor.Editor.Events() {
Expand Down Expand Up @@ -1067,6 +1150,11 @@ func (pg *sendPage) Handle(c pageCommon) {
pg.leftAmountEditor.SetError(pg.amountErrorText)
}

if pg.walletSelected != c.wallAcctSelector.selectedSendWallet {
pg.shouldInitializeTxAuthor = true
pg.walletSelected = c.wallAcctSelector.selectedSendWallet
}

if pg.shouldInitializeTxAuthor {
pg.shouldInitializeTxAuthor = false
pg.leftAmountEditor.Editor.SetText("")
Expand Down Expand Up @@ -1131,4 +1219,13 @@ func (pg *sendPage) Handle(c pageCommon) {
pg.isBroadcastingTransaction = false
default:
}

if pg.leftAmountEditor.CustomButton.Button.Clicked() {
pg.leftAmountEditor.Editor.Focus()
pg.setMaxAmount(c)
}
if pg.rightAmountEditor.CustomButton.Button.Clicked() {
pg.rightAmountEditor.Editor.Focus()
pg.setMaxAmount(c)
}
}