-
Notifications
You must be signed in to change notification settings - Fork 23
/
order_settings_modal.go
557 lines (485 loc) · 20.1 KB
/
order_settings_modal.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package exchange
import (
"context"
"gioui.org/font"
"gioui.org/io/clipboard"
"gioui.org/layout"
"gioui.org/widget"
sharedW "github.com/crypto-power/cryptopower/libwallet/assets/wallet"
libutils "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/modal"
"github.com/crypto-power/cryptopower/ui/page/components"
"github.com/crypto-power/cryptopower/ui/values"
)
type callbackParams struct {
sourceAccountSelector *components.WalletAndAccountSelector
sourceWalletSelector *components.WalletAndAccountSelector
destinationAccountSelector *components.WalletAndAccountSelector
destinationWalletSelector *components.WalletAndAccountSelector
}
type orderSettingsModal struct {
*load.Load
*cryptomaterial.Modal
ctx context.Context // page context
ctxCancel context.CancelFunc
pageContainer *widget.List
settingsSaved func(params *callbackParams)
onCancel func()
cancelBtn cryptomaterial.Button
saveBtn cryptomaterial.Button
sourceInfoButton cryptomaterial.IconButton
destinationInfoButton cryptomaterial.IconButton
addressEditor cryptomaterial.Editor
copyRedirect *cryptomaterial.Clickable
feeRateSelector *components.FeeRateSelector
*orderData
sourceAccountSelector *components.WalletAndAccountSelector
sourceWalletSelector *components.WalletAndAccountSelector
destinationAccountSelector *components.WalletAndAccountSelector
destinationWalletSelector *components.WalletAndAccountSelector
}
func newOrderSettingsModalModal(l *load.Load, data *orderData) *orderSettingsModal {
osm := &orderSettingsModal{
Load: l,
Modal: l.Theme.ModalFloatTitle(values.String(values.StrSettings), l.IsMobileView()),
orderData: data,
copyRedirect: l.Theme.NewClickable(false),
}
osm.cancelBtn = l.Theme.OutlineButton(values.String(values.StrCancel))
osm.cancelBtn.Font.Weight = font.Medium
osm.saveBtn = l.Theme.Button(values.String(values.StrSave))
osm.saveBtn.Font.Weight = font.Medium
osm.saveBtn.SetEnabled(false)
osm.sourceInfoButton = l.Theme.IconButton(l.Theme.Icons.ActionInfo)
osm.destinationInfoButton = l.Theme.IconButton(l.Theme.Icons.ActionInfo)
osm.sourceInfoButton.Size, osm.destinationInfoButton.Size = values.MarginPadding14, values.MarginPadding14
buttonInset := layout.UniformInset(values.MarginPadding0)
osm.sourceInfoButton.Inset, osm.destinationInfoButton.Inset = buttonInset, buttonInset
osm.addressEditor = l.Theme.IconEditor(new(widget.Editor), "", l.Theme.Icons.ContentCopy, true)
osm.addressEditor.Editor.SingleLine = true
osm.pageContainer = &widget.List{
List: layout.List{
Axis: layout.Vertical,
Alignment: layout.Middle,
},
}
callbackFunc := func() libutils.AssetType {
return osm.orderData.fromCurrency
}
osm.feeRateSelector = components.NewFeeRateSelector(l, callbackFunc)
osm.initWalletSelectors()
return osm
}
func (osm *orderSettingsModal) OnSettingsSaved(settingsSaved func(params *callbackParams)) *orderSettingsModal {
osm.settingsSaved = settingsSaved
return osm
}
func (osm *orderSettingsModal) OnCancel(cancel func()) *orderSettingsModal {
osm.onCancel = cancel
return osm
}
func (osm *orderSettingsModal) OnResume() {
osm.ctx, osm.ctxCancel = context.WithCancel(context.TODO())
osm.sourceWalletSelector.WalletSelected(func(selectedWallet sharedW.Asset) {
osm.sourceAccountSelector.SelectFirstValidAccount(selectedWallet)
})
address, err := osm.destinationWalletSelector.SelectedWallet().CurrentAddress(osm.destinationAccountSelector.SelectedAccount().Number)
if err != nil {
log.Error(err)
}
osm.addressEditor.Editor.SetText(address)
osm.destinationWalletSelector.WalletSelected(func(selectedWallet sharedW.Asset) {
osm.destinationAccountSelector.SelectFirstValidAccount(selectedWallet)
address, err := osm.destinationWalletSelector.SelectedWallet().CurrentAddress(osm.destinationAccountSelector.SelectedAccount().Number)
if err != nil {
log.Error(err)
}
osm.addressEditor.Editor.SetText(address)
})
osm.destinationAccountSelector.AccountSelected(func(selectedAccount *sharedW.Account) {
address, err := osm.destinationWalletSelector.SelectedWallet().CurrentAddress(osm.destinationAccountSelector.SelectedAccount().Number)
if err != nil {
log.Error(err)
}
osm.addressEditor.Editor.SetText(address)
})
go osm.feeRateSelector.UpdatedFeeRate(osm.sourceWalletSelector.SelectedWallet())
}
func (osm *orderSettingsModal) SetLoading(loading bool) {
osm.Modal.SetDisabled(loading)
}
func (osm *orderSettingsModal) OnDismiss() {
osm.ctxCancel()
}
func (osm *orderSettingsModal) Handle() {
osm.saveBtn.SetEnabled(osm.canSave())
if osm.saveBtn.Clicked() {
params := &callbackParams{
sourceAccountSelector: osm.sourceAccountSelector,
sourceWalletSelector: osm.sourceWalletSelector,
destinationAccountSelector: osm.destinationAccountSelector,
destinationWalletSelector: osm.destinationWalletSelector,
}
configInfo := sharedW.ExchangeConfig{
SourceAsset: osm.orderData.fromCurrency,
DestinationAsset: osm.orderData.toCurrency,
SourceWalletID: int32(params.sourceWalletSelector.SelectedWallet().GetWalletID()),
DestinationWalletID: int32(params.destinationWalletSelector.SelectedWallet().GetWalletID()),
SourceAccountNumber: params.sourceAccountSelector.SelectedAccount().Number,
DestinationAccountNumber: params.destinationAccountSelector.SelectedAccount().Number,
}
osm.AssetsManager.SetExchangeConfig(configInfo)
osm.settingsSaved(params)
osm.Dismiss()
}
if osm.cancelBtn.Clicked() || osm.Modal.BackdropClicked(true) {
osm.onCancel()
osm.Dismiss()
}
if osm.sourceInfoButton.Button.Clicked() {
info := modal.NewCustomModal(osm.Load).
PositiveButtonStyle(osm.Theme.Color.Primary, osm.Theme.Color.Surface).
SetContentAlignment(layout.W, layout.W, layout.Center).
SetupWithTemplate(modal.SourceModalInfoTemplate).
Title(values.String(values.StrSource))
osm.ParentWindow().ShowModal(info)
}
if osm.destinationInfoButton.Button.Clicked() {
info := modal.NewCustomModal(osm.Load).
PositiveButtonStyle(osm.Theme.Color.Primary, osm.Theme.Color.Surface).
SetContentAlignment(layout.W, layout.W, layout.Center).
Body(values.String(values.StrDestinationModalInfo)).
Title(values.String(values.StrDestination))
osm.ParentWindow().ShowModal(info)
}
if osm.feeRateSelector.SaveRate.Clicked() {
osm.feeRateSelector.OnEditRateClicked(osm.sourceWalletSelector.SelectedWallet())
}
}
func (osm *orderSettingsModal) handleCopyEvent(gtx C) {
osm.addressEditor.EditorIconButtonEvent = func() {
clipboard.WriteOp{Text: osm.addressEditor.Editor.Text()}.Add(gtx.Ops)
osm.Toast.Notify(values.String(values.StrCopied))
}
}
func (osm *orderSettingsModal) canSave() bool {
if osm.sourceWalletSelector.SelectedWallet() == nil {
return false
}
if osm.sourceAccountSelector.SelectedAccount() == nil {
return false
}
if osm.destinationWalletSelector.SelectedWallet() == nil {
return false
}
if osm.destinationAccountSelector.SelectedAccount() == nil {
return false
}
if osm.addressEditor.Editor.Text() == "" {
return false
}
if !osm.sourceWalletSelector.SelectedWallet().IsSynced() {
return false
}
return true
}
func (osm *orderSettingsModal) Layout(gtx layout.Context) D {
osm.handleCopyEvent(gtx)
w := []layout.Widget{
func(gtx C) D {
return layout.Stack{Alignment: layout.S}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return layout.Stack{Alignment: layout.NE}.Layout(gtx,
layout.Expanded(func(gtx C) 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 {
return layout.Inset{
Bottom: values.MarginPadding8,
}.Layout(gtx, func(gtx C) D {
txt := osm.Theme.Label(values.TextSize20, values.String(values.StrSettings))
txt.Font.Weight = font.SemiBold
return txt.Layout(gtx)
})
}),
layout.Rigid(func(gtx C) D {
return osm.Theme.List(osm.pageContainer).Layout(gtx, 1, func(gtx C, i int) D {
return cryptomaterial.LinearLayout{
Width: cryptomaterial.MatchParent,
Height: cryptomaterial.WrapContent,
Direction: layout.Center,
}.Layout2(gtx, func(gtx C) D {
return cryptomaterial.LinearLayout{
Width: cryptomaterial.MatchParent,
Height: cryptomaterial.WrapContent,
}.Layout2(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{
Bottom: values.MarginPadding16,
}.Layout(gtx, func(gtx C) D {
return cryptomaterial.LinearLayout{
Width: cryptomaterial.MatchParent,
Height: cryptomaterial.WrapContent,
Orientation: layout.Vertical,
Margin: layout.Inset{Bottom: values.MarginPadding16},
}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
txt := osm.Theme.Label(values.TextSize16, values.String(values.StrSource))
txt.Font.Weight = font.SemiBold
return txt.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Top: values.MarginPadding4,
Left: values.MarginPadding4,
}.Layout(gtx, osm.sourceInfoButton.Layout)
}),
)
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Bottom: values.MarginPadding16,
}.Layout(gtx, func(gtx C) D {
return osm.sourceWalletSelector.Layout(osm.ParentWindow(), gtx)
})
}),
layout.Rigid(func(gtx C) D {
return osm.sourceAccountSelector.Layout(osm.ParentWindow(), gtx)
}),
layout.Rigid(func(gtx C) D {
if !osm.sourceWalletSelector.SelectedWallet().IsSynced() {
txt := osm.Theme.Label(values.TextSize14, values.String(values.StrSourceWalletNotSynced))
txt.Font.Weight = font.SemiBold
txt.Color = osm.Theme.Color.Danger
return txt.Layout(gtx)
}
return D{}
}),
)
})
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Bottom: values.MarginPadding16,
}.Layout(gtx, func(gtx C) D {
return cryptomaterial.LinearLayout{
Width: cryptomaterial.MatchParent,
Height: cryptomaterial.WrapContent,
Orientation: layout.Vertical,
Margin: layout.Inset{Bottom: values.MarginPadding16},
}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
txt := osm.Theme.Label(values.TextSize16, values.String(values.StrDestination))
txt.Font.Weight = font.SemiBold
return txt.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Top: values.MarginPadding4,
Left: values.MarginPadding4,
}.Layout(gtx, osm.destinationInfoButton.Layout)
}),
)
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Bottom: values.MarginPadding16,
}.Layout(gtx, func(gtx C) D {
return osm.destinationWalletSelector.Layout(osm.ParentWindow(), gtx)
})
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Bottom: values.MarginPadding16,
}.Layout(gtx, func(gtx C) D {
return osm.destinationAccountSelector.Layout(osm.ParentWindow(), gtx)
})
}),
layout.Rigid(func(gtx C) D {
border := widget.Border{Color: osm.Load.Theme.Color.Gray2, CornerRadius: values.MarginPadding10, Width: values.MarginPadding2}
wrapper := osm.Load.Theme.Card()
wrapper.Color = osm.Load.Theme.Color.Background
return border.Layout(gtx, func(gtx C) D {
return wrapper.Layout(gtx, func(gtx C) D {
return layout.UniformInset(values.MarginPadding10).Layout(gtx, func(gtx C) D {
return layout.Flex{}.Layout(gtx,
layout.Flexed(0.9, osm.Load.Theme.Body1(osm.addressEditor.Editor.Text()).Layout),
layout.Flexed(0.1, func(gtx C) D {
return layout.E.Layout(gtx, func(gtx C) D {
mGtx := gtx
if osm.addressEditor.Editor.Text() == "" {
mGtx = gtx.Disabled()
}
if osm.copyRedirect.Clicked() {
clipboard.WriteOp{Text: osm.addressEditor.Editor.Text()}.Add(mGtx.Ops)
osm.Load.Toast.Notify(values.String(values.StrCopied))
}
return osm.copyRedirect.Layout(mGtx, osm.Load.Theme.Icons.CopyIcon.Layout24dp)
})
}),
)
})
})
})
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Bottom: values.MarginPadding16,
}.Layout(gtx, func(gtx C) D {
if !osm.destinationWalletSelector.SelectedWallet().IsSynced() {
txt := osm.Theme.Label(values.TextSize14, values.String(values.StrDestinationWalletNotSynced))
txt.Font.Weight = font.SemiBold
txt.Color = osm.Theme.Color.Danger
return txt.Layout(gtx)
}
return D{}
})
}),
layout.Rigid(func(gtx C) D {
if osm.sourceWalletSelector.SelectedWallet().GetAssetType() != libutils.BTCWalletAsset {
return D{}
}
return osm.feeRateSelector.Layout(gtx)
}),
)
})
}),
)
})
})
})
}),
)
})
}),
)
}),
layout.Stacked(func(gtx C) D {
gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
return layout.S.Layout(gtx, func(gtx C) D {
return layout.Inset{
Top: values.MarginPadding16,
}.Layout(gtx, func(gtx C) D {
c := osm.Theme.Card()
c.Radius = cryptomaterial.Radius(0)
return c.Layout(gtx, func(gtx C) D {
inset := layout.Inset{
Top: values.MarginPadding16,
}
return inset.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Flexed(1, func(gtx C) D {
return layout.E.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{
Right: values.MarginPadding4,
}.Layout(gtx, osm.cancelBtn.Layout)
}),
layout.Rigid(osm.saveBtn.Layout),
)
})
}),
)
})
})
})
})
}),
)
},
}
return osm.Modal.Layout(gtx, w)
}
func (osm *orderSettingsModal) initWalletSelectors() {
if osm.AssetsManager.IsExchangeConfigSet() {
exchangeConfig := osm.AssetsManager.GetExchangeConfig()
sourceWallet := osm.AssetsManager.WalletWithID(int(exchangeConfig.SourceWalletID))
destinationWallet := osm.AssetsManager.WalletWithID(int(exchangeConfig.DestinationWalletID))
sourceCurrency := exchangeConfig.SourceAsset
toCurrency := exchangeConfig.DestinationAsset
if sourceWallet != nil {
_, err := sourceWallet.GetAccount(exchangeConfig.SourceAccountNumber)
if err != nil {
log.Error(err)
}
// Source wallet picker
osm.sourceWalletSelector = components.NewWalletAndAccountSelector(osm.Load, sourceCurrency).
Title(values.String(values.StrSource))
osm.sourceWalletSelector.SetSelectedWallet(sourceWallet)
// Source account picker
osm.sourceAccountSelector = components.NewWalletAndAccountSelector(osm.Load).
Title(values.String(values.StrAccount)).
AccountValidator(func(account *sharedW.Account) bool {
accountIsValid := account.Number != load.MaxInt32
return accountIsValid
})
osm.sourceAccountSelector.SelectAccount(osm.sourceWalletSelector.SelectedWallet(), exchangeConfig.SourceAccountNumber)
osm.sourceWalletSelector.WalletSelected(func(selectedWallet sharedW.Asset) {
osm.sourceAccountSelector.SelectFirstValidAccount(selectedWallet)
})
}
if destinationWallet != nil {
_, err := destinationWallet.GetAccount(exchangeConfig.DestinationAccountNumber)
if err != nil {
log.Error(err)
}
// Destination wallet picker
osm.destinationWalletSelector = components.NewWalletAndAccountSelector(osm.Load, toCurrency).
Title(values.String(values.StrDestination)).
EnableWatchOnlyWallets(true)
osm.destinationWalletSelector.SetSelectedWallet(destinationWallet)
// Destination account picker
osm.destinationAccountSelector = components.NewWalletAndAccountSelector(osm.Load).
Title(values.String(values.StrAccount)).
AccountValidator(func(account *sharedW.Account) bool {
// Imported accounts and watch only accounts are imvalid
accountIsValid := account.Number != load.MaxInt32
return accountIsValid
})
osm.destinationAccountSelector.SelectAccount(osm.destinationWalletSelector.SelectedWallet(), exchangeConfig.DestinationAccountNumber)
osm.destinationWalletSelector.WalletSelected(func(selectedWallet sharedW.Asset) {
osm.destinationAccountSelector.SelectFirstValidAccount(selectedWallet)
})
}
} else {
// Source wallet picker
osm.sourceWalletSelector = components.NewWalletAndAccountSelector(osm.Load, libutils.DCRWalletAsset).
Title(values.String(values.StrFrom))
// Source account picker
osm.sourceAccountSelector = components.NewWalletAndAccountSelector(osm.Load).
Title(values.String(values.StrAccount)).
AccountValidator(func(account *sharedW.Account) bool {
accountIsValid := account.Number != load.MaxInt32
return accountIsValid
})
osm.sourceAccountSelector.SelectFirstValidAccount(osm.sourceWalletSelector.SelectedWallet())
osm.sourceWalletSelector.WalletSelected(func(selectedWallet sharedW.Asset) {
osm.sourceAccountSelector.SelectFirstValidAccount(selectedWallet)
})
// Destination wallet picker
osm.destinationWalletSelector = components.NewWalletAndAccountSelector(osm.Load, libutils.BTCWalletAsset).
Title(values.String(values.StrTo)).
EnableWatchOnlyWallets(true)
// Destination account picker
osm.destinationAccountSelector = components.NewWalletAndAccountSelector(osm.Load).
Title(values.String(values.StrAccount)).
AccountValidator(func(account *sharedW.Account) bool {
accountIsValid := account.Number != load.MaxInt32
return accountIsValid
})
osm.destinationAccountSelector.SelectFirstValidAccount(osm.destinationWalletSelector.SelectedWallet())
osm.destinationWalletSelector.WalletSelected(func(selectedWallet sharedW.Asset) {
osm.destinationAccountSelector.SelectFirstValidAccount(selectedWallet)
})
}
}