/
send_amount.go
244 lines (197 loc) · 6.45 KB
/
send_amount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package send
import (
"fmt"
"strconv"
"gioui.org/layout"
"gioui.org/widget"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/planetdecred/dcrlibwallet"
"github.com/planetdecred/godcr/ui/decredmaterial"
"github.com/planetdecred/godcr/ui/load"
"github.com/planetdecred/godcr/ui/values"
)
const invalidAmountErr = "Invalid amount" //TODO: use localized strings
type sendAmount struct {
*load.Load
dcrAmountEditor decredmaterial.Editor
usdAmountEditor decredmaterial.Editor
SendMax bool
dcrSendMaxChangeEvent bool
usdSendMaxChangeEvent bool
amountChanged func()
amountErrorText string
exchangeRate float64
}
func newSendAmount(l *load.Load) *sendAmount {
sa := &sendAmount{
Load: l,
exchangeRate: -1,
}
sa.dcrAmountEditor = l.Theme.Editor(new(widget.Editor), "Amount (DCR)")
sa.dcrAmountEditor.Editor.SetText("")
sa.dcrAmountEditor.HasCustomButton = true
sa.dcrAmountEditor.Editor.SingleLine = true
sa.dcrAmountEditor.CustomButton.Inset = layout.UniformInset(values.MarginPadding2)
sa.dcrAmountEditor.CustomButton.Text = "Max"
sa.dcrAmountEditor.CustomButton.CornerRadius = values.MarginPadding0
sa.usdAmountEditor = l.Theme.Editor(new(widget.Editor), "Amount (USD)")
sa.usdAmountEditor.Editor.SetText("")
sa.usdAmountEditor.HasCustomButton = true
sa.usdAmountEditor.Editor.SingleLine = true
sa.usdAmountEditor.CustomButton.Inset = layout.UniformInset(values.MarginPadding2)
sa.usdAmountEditor.CustomButton.Text = "Max"
sa.usdAmountEditor.CustomButton.CornerRadius = values.MarginPadding0
sa.styleWidgets()
return sa
}
// styleWidgets sets the appropriate colors for the amount widgets.
func (sa *sendAmount) styleWidgets() {
sa.dcrAmountEditor.CustomButton.Background = sa.Theme.Color.Gray1
sa.dcrAmountEditor.CustomButton.Color = sa.Theme.Color.Surface
sa.dcrAmountEditor.EditorStyle.Color = sa.Theme.Color.Text
sa.usdAmountEditor.CustomButton.Background = sa.Theme.Color.Gray1
sa.usdAmountEditor.CustomButton.Color = sa.Theme.Color.Surface
sa.usdAmountEditor.EditorStyle.Color = sa.Theme.Color.Text
}
func (sa *sendAmount) setExchangeRate(exchangeRate float64) {
sa.exchangeRate = exchangeRate
sa.validateDCRAmount() // convert dcr input to usd
}
func (sa *sendAmount) setAmount(amount int64) {
// TODO: this workaround ignores the change events from the
// amount input to avoid construct tx cycle.
sa.dcrSendMaxChangeEvent = sa.SendMax
sa.dcrAmountEditor.Editor.SetText(fmt.Sprintf("%.8f", dcrutil.Amount(amount).ToCoin()))
if sa.exchangeRate != -1 {
usdAmount := load.DCRToUSD(sa.exchangeRate, dcrutil.Amount(amount).ToCoin())
sa.usdSendMaxChangeEvent = true
sa.usdAmountEditor.Editor.SetText(fmt.Sprintf("%.2f", usdAmount))
}
}
func (sa *sendAmount) amountIsValid() bool {
_, err := strconv.ParseFloat(sa.dcrAmountEditor.Editor.Text(), 64)
amountEditorErrors := sa.amountErrorText == ""
return err == nil && amountEditorErrors || sa.SendMax
}
func (sa *sendAmount) validAmount() (int64, bool, error) {
if sa.SendMax {
return 0, sa.SendMax, nil
}
amount, err := strconv.ParseFloat(sa.dcrAmountEditor.Editor.Text(), 64)
if err != nil {
return -1, sa.SendMax, err
}
return dcrlibwallet.AmountAtom(amount), sa.SendMax, nil
}
func (sa *sendAmount) validateDCRAmount() {
sa.amountErrorText = ""
if sa.inputsNotEmpty(sa.dcrAmountEditor.Editor) {
dcrAmount, err := strconv.ParseFloat(sa.dcrAmountEditor.Editor.Text(), 64)
if err != nil {
// empty usd input
sa.usdAmountEditor.Editor.SetText("")
sa.amountErrorText = invalidAmountErr
// todo: invalid decimal places error
return
}
if sa.exchangeRate != -1 {
usdAmount := load.DCRToUSD(sa.exchangeRate, dcrAmount)
sa.usdAmountEditor.Editor.SetText(fmt.Sprintf("%.2f", usdAmount)) // 2 decimal places
}
return
}
// empty usd input since this is empty
sa.usdAmountEditor.Editor.SetText("")
}
// validateUSDAmount is called when usd text changes
func (sa *sendAmount) validateUSDAmount() bool {
sa.amountErrorText = ""
if sa.inputsNotEmpty(sa.usdAmountEditor.Editor) {
usdAmount, err := strconv.ParseFloat(sa.usdAmountEditor.Editor.Text(), 64)
if err != nil {
// empty dcr input
sa.dcrAmountEditor.Editor.SetText("")
sa.amountErrorText = invalidAmountErr
return false
}
if sa.exchangeRate != -1 {
dcrAmount := load.USDToDCR(sa.exchangeRate, usdAmount)
sa.dcrAmountEditor.Editor.SetText(fmt.Sprintf("%.8f", dcrAmount)) // 8 decimal places
}
return true
}
// empty dcr input since this is empty
sa.dcrAmountEditor.Editor.SetText("")
return false
}
func (sa *sendAmount) inputsNotEmpty(editors ...*widget.Editor) bool {
for _, e := range editors {
if e.Text() == "" {
return false
}
}
return true
}
func (sa *sendAmount) setError(err string) {
sa.amountErrorText = err
}
func (sa *sendAmount) resetFields() {
sa.SendMax = false
sa.clearAmount()
}
func (sa *sendAmount) clearAmount() {
sa.amountErrorText = ""
sa.dcrAmountEditor.Editor.SetText("")
sa.usdAmountEditor.Editor.SetText("")
}
func (sa *sendAmount) handle() {
sa.dcrAmountEditor.SetError(sa.amountErrorText)
if sa.amountErrorText != "" {
sa.dcrAmountEditor.LineColor = sa.Theme.Color.Danger
sa.usdAmountEditor.LineColor = sa.Theme.Color.Danger
} else {
sa.dcrAmountEditor.LineColor = sa.Theme.Color.Gray2
sa.usdAmountEditor.LineColor = sa.Theme.Color.Gray2
}
if sa.SendMax {
sa.dcrAmountEditor.CustomButton.Background = sa.Theme.Color.Primary
sa.usdAmountEditor.CustomButton.Background = sa.Theme.Color.Primary
} else if len(sa.dcrAmountEditor.Editor.Text()) < 1 || !sa.SendMax {
sa.dcrAmountEditor.CustomButton.Background = sa.Theme.Color.Gray1
sa.usdAmountEditor.CustomButton.Background = sa.Theme.Color.Gray1
}
for _, evt := range sa.dcrAmountEditor.Editor.Events() {
if sa.dcrAmountEditor.Editor.Focused() {
switch evt.(type) {
case widget.ChangeEvent:
if sa.dcrSendMaxChangeEvent {
sa.dcrSendMaxChangeEvent = false
continue
}
sa.SendMax = false
sa.validateDCRAmount()
sa.amountChanged()
}
}
}
for _, evt := range sa.usdAmountEditor.Editor.Events() {
if sa.usdAmountEditor.Editor.Focused() {
switch evt.(type) {
case widget.ChangeEvent:
if sa.usdSendMaxChangeEvent {
sa.usdSendMaxChangeEvent = false
continue
}
sa.SendMax = false
sa.validateUSDAmount()
sa.amountChanged()
}
}
}
}
func (sa *sendAmount) IsMaxClicked() bool {
if sa.dcrAmountEditor.CustomButton.Clicked() || sa.usdAmountEditor.CustomButton.Clicked() {
return true
}
return false
}