/
recipient.go
417 lines (360 loc) · 12.3 KB
/
recipient.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package send
import (
"fmt"
"gioui.org/font"
"gioui.org/layout"
"gioui.org/widget"
"github.com/crypto-power/cryptopower/app"
sharedW "github.com/crypto-power/cryptopower/libwallet/assets/wallet"
libUtil "github.com/crypto-power/cryptopower/libwallet/utils"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
"github.com/crypto-power/cryptopower/ui/load"
"github.com/crypto-power/cryptopower/ui/values"
)
type recipient struct {
*load.Load
id int
deleteBtn *cryptomaterial.Clickable
description cryptomaterial.Editor
selectedWallet sharedW.Asset
selectedSourceAccount *sharedW.Account
sendDestination *destination
amount *sendAmount
pageParam getPageFields
deleteRecipient func(int)
}
func newRecipient(l *load.Load, selectedWallet sharedW.Asset, pageParam getPageFields, id int) *recipient {
rp := &recipient{
Load: l,
selectedWallet: selectedWallet,
pageParam: pageParam,
id: id,
deleteBtn: l.Theme.NewClickable(false),
}
assetType := rp.selectedWallet.GetAssetType()
rp.amount = newSendAmount(l.Theme, assetType)
rp.amount.amountEditor.TextSize = values.TextSizeTransform(l.IsMobileView(), values.TextSize16)
rp.sendDestination = newSendDestination(l, assetType)
rp.description = rp.Theme.Editor(new(widget.Editor), values.String(values.StrNote))
rp.description.Editor.SingleLine = false
rp.description.Editor.SetText("")
rp.description.IsTitleLabel = false
// Set the maximum characters the editor can accept.
rp.description.Editor.MaxLen = MaxTxLabelSize
rp.description.TextSize = values.TextSizeTransform(l.IsMobileView(), values.TextSize16)
return rp
}
func (rp *recipient) onAddressChanged(addressChanged func()) {
rp.sendDestination.addressChanged = addressChanged
}
func (rp *recipient) onAmountChanged(amountChanged func()) {
rp.amount.amountChanged = amountChanged
}
func (rp *recipient) onDeleteRecipient(onDelete func(int)) {
rp.deleteRecipient = onDelete
}
func (rp *recipient) cleanAllErrors() {
rp.amount.setError("")
rp.sendDestination.setError("")
}
func (rp *recipient) setDestinationAssetType(assetType libUtil.AssetType) {
rp.amount.setAssetType(assetType)
rp.sendDestination.initDestinationWalletSelector(assetType)
}
func (rp *recipient) isAccountValid(sourceAccount, account *sharedW.Account) bool {
accountIsValid := account.Number != load.MaxInt32
// Filter mixed wallet
destinationWallet := rp.sendDestination.destinationAccountSelector.SelectedWallet()
isMixedAccount := load.MixedAccountNumber(destinationWallet) == account.Number
// Filter the sending account.
sourceWalletID := sourceAccount.WalletID
isSameAccount := sourceWalletID == account.WalletID && account.Number == sourceAccount.Number
if !accountIsValid || isSameAccount || isMixedAccount {
return false
}
return true
}
func (rp *recipient) initializeAccountSelectors(sourceAccount *sharedW.Account) {
rp.selectedSourceAccount = sourceAccount
rp.sendDestination.destinationAccountSelector = rp.sendDestination.destinationAccountSelector.AccountValidator(func(account *sharedW.Account) bool {
return rp.isAccountValid(sourceAccount, account)
})
rp.sendDestination.destinationAccountSelector.AccountSelected(func(selectedWallet *sharedW.Account) {
rp.sendDestination.addressChanged()
})
rp.sendDestination.destinationWalletSelector.WalletSelected(func(selectedWallet sharedW.Asset) {
rp.sendDestination.destinationAccountSelector.SelectFirstValidAccount(selectedWallet)
})
// destinationAccountSelector does not have a default value,
// so assign it an initial value here
rp.sendDestination.destinationAccountSelector.SelectFirstValidAccount(rp.sendDestination.destinationWalletSelector.SelectedWallet())
}
func (rp *recipient) isShowSendToWallet() bool {
sourceWalletSelected := rp.sendDestination.destinationWalletSelector.SelectedWallet()
var wallets []sharedW.Asset
switch sourceWalletSelected.GetAssetType() {
case libUtil.BTCWalletAsset:
wallets = append(wallets, rp.AssetsManager.AllBTCWallets()...)
case libUtil.DCRWalletAsset:
wallets = append(wallets, rp.AssetsManager.AllDCRWallets()...)
case libUtil.LTCWalletAsset:
wallets = append(wallets, rp.AssetsManager.AllLTCWallets()...)
}
if len(wallets) == 1 {
account, err := wallets[0].GetAccountsRaw()
if err != nil {
log.Errorf("Error getting accounts:", err)
return false
}
accountValids := make([]sharedW.Account, 0)
for _, acc := range account.Accounts {
if rp.isAccountValid(rp.selectedSourceAccount, acc) {
accountValids = append(accountValids, *acc)
}
}
return len(accountValids) > 1
}
if len(wallets) > 1 {
return true
}
return false
}
func (rp *recipient) isSendToAddress() bool {
return rp.sendDestination.sendToAddress
}
func (rp *recipient) isValidated() bool {
amountIsValid := rp.amount.amountIsValid()
addressIsValid := rp.sendDestination.validate()
// No need for checking the err message since it is as result of amount and
// address validation.
// validForSending
return amountIsValid && addressIsValid
}
func (rp *recipient) resetFields() {
rp.sendDestination.clearAddressInput()
rp.description.Editor.SetText("")
rp.amount.resetFields()
}
func (rp *recipient) destinationAddress() string {
address, err := rp.sendDestination.destinationAddress()
if err != nil {
rp.addressValidationError(err.Error())
return ""
}
return address
}
func (rp *recipient) destinationAccount() *sharedW.Account {
return rp.sendDestination.destinationAccount()
}
func (rp *recipient) descriptionText() string {
return rp.description.Editor.Text()
}
func (rp *recipient) validAmount() (int64, bool) {
amountAtom, sendMax, err := rp.amount.validAmount()
if err != nil {
rp.amountValidationError(err.Error())
return -1, false
}
return amountAtom, sendMax
}
func (rp *recipient) setAmount(amount int64) {
rp.amount.setAmount(amount)
}
func (rp *recipient) amountValidationError(err string) {
rp.amount.setError(err)
}
func (rp *recipient) addressValidationError(err string) {
rp.sendDestination.setError(err)
}
func (rp *recipient) recipientLayout(index int, showIcon bool, window app.WindowNavigator) layout.Widget {
rp.handle()
return func(gtx C) D {
return cryptomaterial.LinearLayout{
Width: cryptomaterial.WrapContent,
Height: cryptomaterial.WrapContent,
Orientation: layout.Vertical,
}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if showIcon {
return layout.Inset{Bottom: values.MarginPadding12}.Layout(gtx, func(gtx C) D {
return rp.topLayout(gtx, index)
})
}
return D{}
}),
layout.Rigid(func(gtx C) D {
layoutBody := func(gtx C) D {
txt := fmt.Sprintf("%s %s", values.String(values.StrDestination), values.String(values.StrAddress))
return rp.contentWrapper(gtx, txt, rp.sendDestination.destinationAddressEditor.Layout)
}
if !rp.isShowSendToWallet() {
return layoutBody(gtx)
}
if !rp.sendDestination.sendToAddress {
layoutBody = rp.walletAccountlayout(window)
}
return rp.sendDestination.accountSwitch.Layout(gtx, layoutBody, rp.IsMobileView())
}),
layout.Rigid(rp.addressAndAmountlayout),
layout.Rigid(rp.txLabelSection),
)
}
}
func (rp *recipient) topLayout(gtx C, index int) D {
txt := fmt.Sprintf("%s: %s %v", values.String(values.StrTo), values.String(values.StrRecipient), index)
titleTxt := rp.Theme.Label(values.TextSizeTransform(rp.IsMobileView(), values.TextSize16), txt)
titleTxt.Color = rp.Theme.Color.GrayText2
return layout.Flex{}.Layout(gtx,
layout.Rigid(titleTxt.Layout),
layout.Flexed(1, func(gtx C) D {
return layout.E.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return rp.deleteBtn.Layout(gtx, rp.Theme.Icons.DeleteIcon.Layout20dp)
})
}),
)
}
func (rp *recipient) walletAccountlayout(window app.WindowNavigator) layout.Widget {
return func(gtx C) D {
return cryptomaterial.LinearLayout{
Width: cryptomaterial.MatchParent,
Height: cryptomaterial.WrapContent,
Orientation: layout.Vertical,
}.Layout(gtx,
layout.Rigid(func(gtx C) D {
txt := fmt.Sprintf("%s %s", values.String(values.StrDestination), values.String(values.StrWallet))
return rp.contentWrapper(gtx, txt, func(gtx C) D {
return rp.sendDestination.destinationWalletSelector.Layout(window, gtx)
})
}),
layout.Rigid(func(gtx C) D {
txt := fmt.Sprintf("%s %s", values.String(values.StrDestination), values.String(values.StrAccount))
return rp.contentWrapper(gtx, txt, func(gtx C) D {
return rp.sendDestination.destinationAccountSelector.Layout(window, gtx)
})
}),
)
}
}
func (rp *recipient) contentWrapper(gtx C, title string, content layout.Widget) D {
return layout.Inset{
Bottom: values.MarginPadding16,
}.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
lbl := rp.Theme.Label(values.TextSizeTransform(rp.IsMobileView(), values.TextSize16), title)
lbl.Font.Weight = font.SemiBold
return layout.Inset{
Bottom: values.MarginPadding4,
}.Layout(gtx, lbl.Layout)
}),
layout.Rigid(content),
)
})
}
func (rp *recipient) addressAndAmountlayout(gtx C) D {
widget := func(gtx C) D { return rp.amount.amountEditor.Layout(gtx) }
if rp.pageParam().exchangeRate != -1 && rp.pageParam().usdExchangeSet {
widget = func(gtx C) D {
icon := cryptomaterial.NewIcon(rp.Theme.Icons.ActionSwapHoriz)
axis := layout.Horizontal
amountHeight := 0
align := layout.Middle
if !rp.IsMobileView() {
align = layout.Start
}
flexChilds := []layout.FlexChild{
layout.Flexed(0.45, func(gtx C) D {
dims := rp.amount.amountEditor.Layout(gtx)
amountHeight = dims.Size.Y
return dims
}),
layout.Flexed(0.1, func(gtx C) D {
if rp.IsMobileView() {
return layout.Center.Layout(gtx, func(gtx C) D {
return icon.Layout(gtx, values.MarginPadding16)
})
}
return layout.Inset{Top: values.MarginPadding13}.Layout(gtx, func(gtx C) D {
return icon.Layout(gtx, values.MarginPadding16)
})
}),
layout.Flexed(0.45, func(gtx layout.Context) layout.Dimensions {
if rp.amount.amountEditor.HasError() {
gtx.Constraints.Min.Y = amountHeight
}
return rp.amount.usdAmountEditor.Layout(gtx)
}),
}
if rp.IsMobileView() {
axis = layout.Vertical
icon = cryptomaterial.NewIcon(rp.Theme.Icons.ActionSwapVertical)
flexChilds = []layout.FlexChild{
layout.Rigid(rp.amount.amountEditor.Layout),
layout.Rigid(layout.Spacer{Height: values.MarginPadding10}.Layout),
layout.Rigid(func(gtx C) D {
return icon.Layout(gtx, values.MarginPadding16)
}),
layout.Rigid(layout.Spacer{Height: values.MarginPadding10}.Layout),
layout.Rigid(rp.amount.usdAmountEditor.Layout),
}
}
return layout.Flex{
Axis: axis,
Alignment: align,
}.Layout(gtx, flexChilds...)
}
}
return rp.contentWrapper(gtx, values.String(values.StrAmount), widget)
}
func (rp *recipient) txLabelSection(gtx C) D {
count := len(rp.description.Editor.Text())
txt := fmt.Sprintf("%s (%d/%d)", values.String(values.StrDescriptionNote), count, rp.description.Editor.MaxLen)
return rp.contentWrapper(gtx, txt, rp.description.Layout)
}
func (rp *recipient) validateAmount() {
if len(rp.amount.amountEditor.Editor.Text()) > 0 {
rp.amount.validateAmount()
}
}
func (rp *recipient) restyleWidgets() {
rp.amount.styleWidgets()
rp.sendDestination.styleWidgets()
}
func (rp *recipient) handle() {
rp.sendDestination.handle()
rp.amount.handle()
if rp.amount.IsMaxClicked() {
rp.amount.setError("")
rp.amount.SendMax = true
rp.amount.amountChanged()
}
if rp.deleteBtn.Clicked() {
rp.deleteRecipient(rp.id)
}
// if destination switch is equal to Address
if rp.sendDestination.sendToAddress {
if rp.sendDestination.validate() {
if !rp.AssetsManager.ExchangeRateFetchingEnabled() {
if len(rp.amount.amountEditor.Editor.Text()) == 0 {
rp.amount.SendMax = false
}
} else {
if len(rp.amount.amountEditor.Editor.Text()) == 0 {
rp.amount.usdAmountEditor.Editor.SetText("")
rp.amount.SendMax = false
}
}
}
} else {
if !rp.AssetsManager.ExchangeRateFetchingEnabled() {
if len(rp.amount.amountEditor.Editor.Text()) == 0 {
rp.amount.SendMax = false
}
} else {
if len(rp.amount.amountEditor.Editor.Text()) == 0 {
rp.amount.usdAmountEditor.Editor.SetText("")
rp.amount.SendMax = false
}
}
}
}