Skip to content

Commit

Permalink
Implement max button functionality on send page (planetdecred#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuhalang authored and song50119 committed May 29, 2021
1 parent 7b3b9c2 commit 1c2036f
Showing 1 changed file with 110 additions and 13 deletions.
123 changes: 110 additions & 13 deletions ui/send_page.go
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 @@ -748,7 +760,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 @@ -778,13 +790,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 @@ -799,10 +811,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 @@ -927,7 +941,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 @@ -936,7 +950,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 @@ -956,6 +970,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 @@ -967,6 +992,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 @@ -978,13 +1059,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 @@ -996,6 +1077,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 @@ -1021,7 +1104,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 @@ -1035,7 +1118,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 @@ -1063,6 +1146,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 @@ -1127,4 +1215,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)
}
}

0 comments on commit 1c2036f

Please sign in to comment.