/
validate_address.go
292 lines (254 loc) · 7.96 KB
/
validate_address.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
package page
import (
"image/color"
"gioui.org/text"
"gioui.org/layout"
"gioui.org/widget"
"github.com/planetdecred/godcr/ui/decredmaterial"
"github.com/planetdecred/godcr/ui/load"
"github.com/planetdecred/godcr/ui/page/components"
"github.com/planetdecred/godcr/ui/values"
)
const ValidateAddressPageID = "ValidateAddress"
const (
none = iota
valid
invalid
notOwned
)
type ValidateAddressPage struct {
*load.Load
addressEditor decredmaterial.Editor
clearBtn, validateBtn decredmaterial.Button
stateValidate int
walletName string
backButton decredmaterial.IconButton
}
func NewValidateAddressPage(l *load.Load) *ValidateAddressPage {
pg := &ValidateAddressPage{
Load: l,
}
pg.backButton, _ = components.SubpageHeaderButtons(l)
pg.addressEditor = l.Theme.Editor(new(widget.Editor), "Address")
pg.addressEditor.Editor.SingleLine = true
pg.addressEditor.Editor.Submit = true
buttonTextSize := values.TextSize14
pg.validateBtn = l.Theme.Button(new(widget.Clickable), "Validate")
pg.validateBtn.TextSize = buttonTextSize
pg.validateBtn.Background = pg.Theme.Color.Hint
pg.validateBtn.Font.Weight = text.Bold
pg.clearBtn = l.Theme.Button(new(widget.Clickable), "Clear")
pg.clearBtn.Color = pg.Theme.Color.Hint
pg.clearBtn.Font.Weight = text.Bold
pg.clearBtn.Background = color.NRGBA{}
pg.clearBtn.TextSize = buttonTextSize
pg.stateValidate = none
return pg
}
func (pg *ValidateAddressPage) ID() string {
return ValidateAddressPageID
}
func (pg *ValidateAddressPage) OnResume() {
}
func (pg *ValidateAddressPage) Layout(gtx layout.Context) layout.Dimensions {
body := func(gtx C) D {
sp := components.SubPage{
Load: pg.Load,
Title: "Validate address",
BackButton: pg.backButton,
Back: func() {
pg.PopFragment()
},
Body: func(gtx C) D {
return layout.Inset{Top: values.MarginPadding5}.Layout(gtx, func(gtx C) D {
return layout.Flex{Spacing: layout.SpaceBetween}.Layout(gtx,
layout.Rigid(pg.addressSection()),
)
})
},
}
return sp.Layout(gtx)
}
return components.UniformPadding(gtx, body)
}
func (pg *ValidateAddressPage) addressSection() layout.Widget {
return func(gtx C) D {
return pg.pageSections(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(pg.description()),
layout.Rigid(pg.addressEditor.Layout),
layout.Rigid(pg.actionButtons()),
layout.Rigid(pg.showDisplayResult()),
)
})
}
}
func (pg *ValidateAddressPage) description() layout.Widget {
return func(gtx layout.Context) layout.Dimensions {
desc := pg.Theme.Caption("Enter an address to validate:")
desc.Color = pg.Theme.Color.Gray
return layout.Inset{Bottom: values.MarginPadding20}.Layout(gtx, desc.Layout)
}
}
func (pg *ValidateAddressPage) actionButtons() layout.Widget {
return func(gtx C) D {
dims := layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Flexed(1, func(gtx C) D {
return layout.E.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Top: values.MarginPadding15}.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.MarginPadding10}.Layout(gtx, pg.clearBtn.Layout)
}),
layout.Rigid(pg.validateBtn.Layout),
)
})
})
}),
)
return dims
}
}
func (pg *ValidateAddressPage) lineSeparator(gtx layout.Context) layout.Dimensions {
m := values.MarginPadding10
return layout.Inset{Top: m, Bottom: m}.Layout(gtx, pg.Theme.Separator().Layout)
}
func (pg *ValidateAddressPage) showDisplayResult() layout.Widget {
if pg.stateValidate == none {
return func(gtx C) D {
return layout.Dimensions{}
}
}
return func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(pg.lineSeparator),
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{Right: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
if pg.stateValidate == invalid {
return pg.Icons.NavigationCancel.Layout(gtx, values.MarginPadding25)
}
return pg.Icons.ActionCheckCircle.Layout(gtx, values.MarginPadding25)
})
}),
layout.Rigid(func(gtx C) D {
if pg.stateValidate == invalid {
txt := pg.Theme.Body1("Invalid Address")
txt.Color = pg.Theme.Color.Danger
txt.TextSize = values.TextSize16
return txt.Layout(gtx)
}
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
txt := pg.Theme.Body1("Valid address")
txt.Color = pg.Theme.Color.Success
txt.TextSize = values.TextSize16
return txt.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
var text string
if pg.stateValidate == valid {
text = "Owned by you in"
} else {
text = "Not owned by you"
}
txt := pg.Theme.Body1(text)
txt.TextSize = values.TextSize14
txt.Color = pg.Theme.Color.Gray
return txt.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
if pg.stateValidate == valid {
if components.StringNotEmpty(pg.walletName) {
return layout.Inset{Left: values.MarginPadding5}.Layout(gtx, func(gtx C) D {
return decredmaterial.Card{
Color: pg.Theme.Color.LightGray,
}.Layout(gtx, func(gtx C) D {
return layout.UniformInset(values.MarginPadding2).Layout(gtx, func(gtx C) D {
walletText := pg.Theme.Caption(pg.walletName)
walletText.Color = pg.Theme.Color.Gray
return walletText.Layout(gtx)
})
})
})
}
}
return layout.Dimensions{}
}),
)
}),
)
}),
)
}),
)
}
}
func (pg *ValidateAddressPage) pageSections(gtx layout.Context, body layout.Widget) layout.Dimensions {
return layout.Inset{Bottom: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
return pg.Theme.Card().Layout(gtx, func(gtx C) D {
return layout.UniformInset(values.MarginPadding15).Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical, Alignment: layout.Middle, Spacing: layout.SpaceAround}.Layout(gtx,
layout.Rigid(body),
)
})
})
})
}
func (pg *ValidateAddressPage) Handle() {
pg.updateButtonColors()
for _, evt := range pg.addressEditor.Editor.Events() {
if pg.addressEditor.Editor.Focused() {
switch evt.(type) {
case widget.ChangeEvent:
pg.stateValidate = none
}
}
}
if pg.validateBtn.Button.Clicked() {
pg.validateAddress()
}
if pg.clearBtn.Button.Clicked() {
pg.clearPage()
}
}
func (pg *ValidateAddressPage) clearPage() {
pg.stateValidate = none
pg.addressEditor.Editor.SetText("")
}
func (pg *ValidateAddressPage) validateAddress() {
address := pg.addressEditor.Editor.Text()
pg.addressEditor.SetError("")
if !components.StringNotEmpty(address) {
pg.addressEditor.SetError("Please enter a valid address")
return
}
if !pg.WL.MultiWallet.IsAddressValid(address) {
pg.stateValidate = invalid
return
}
exist, walletName := pg.WL.Wallet.HaveAddress(address)
if !exist {
pg.stateValidate = notOwned
return
}
pg.stateValidate = valid
pg.walletName = walletName
}
func (pg *ValidateAddressPage) updateButtonColors() {
if !components.StringNotEmpty(pg.addressEditor.Editor.Text()) {
pg.validateBtn.Background = pg.Theme.Color.Hint
pg.clearBtn.Color = pg.Theme.Color.Hint
} else {
pg.validateBtn.Background = pg.Theme.Color.Primary
pg.clearBtn.Color = pg.Theme.Color.Primary
}
}
func (pg *ValidateAddressPage) clearInputs() {
pg.validateBtn.Background = pg.Theme.Color.Hint
pg.addressEditor.Editor.SetText("")
}
func (pg *ValidateAddressPage) OnClose() {}