/
contact_form.go
333 lines (287 loc) · 9.4 KB
/
contact_form.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
package page_wallet
import (
"fmt"
"image"
"time"
"gioui.org/font"
"gioui.org/layout"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/deroproject/derohe/globals"
"github.com/g45t345rt/g45w/app_icons"
"github.com/g45t345rt/g45w/components"
"github.com/g45t345rt/g45w/containers/confirm_modal"
"github.com/g45t345rt/g45w/containers/notification_modal"
"github.com/g45t345rt/g45w/containers/qrcode_scan_modal"
"github.com/g45t345rt/g45w/lang"
"github.com/g45t345rt/g45w/prefabs"
"github.com/g45t345rt/g45w/router"
"github.com/g45t345rt/g45w/theme"
"github.com/g45t345rt/g45w/wallet_manager"
"golang.org/x/exp/shiny/materialdesign/icons"
)
type PageContactForm struct {
isActive bool
headerPageAnimation *prefabs.PageHeaderAnimation
buttonSave *components.Button
buttonDelete *components.Button
txtName *prefabs.TextField
txtAddr *prefabs.Input
txtNote *prefabs.TextField
buttonScanAddr *components.Button
txtDims layout.Dimensions
contact *wallet_manager.Contact
list *widget.List
}
var _ router.Page = &PageContactForm{}
func NewPageContactForm() *PageContactForm {
list := new(widget.List)
list.Axis = layout.Vertical
saveIcon, _ := widget.NewIcon(icons.ContentSave)
buttonSave := components.NewButton(components.ButtonStyle{
Rounded: components.UniformRounded(unit.Dp(5)),
Icon: saveIcon,
TextSize: unit.Sp(14),
IconGap: unit.Dp(10),
Inset: layout.UniformInset(unit.Dp(10)),
Animation: components.NewButtonAnimationDefault(),
})
buttonSave.Label.Alignment = text.Middle
buttonSave.Style.Font.Weight = font.Bold
deleteIcon, _ := widget.NewIcon(icons.ActionDelete)
buttonDelete := components.NewButton(components.ButtonStyle{
Rounded: components.UniformRounded(unit.Dp(5)),
Icon: deleteIcon,
TextSize: unit.Sp(14),
IconGap: unit.Dp(10),
Inset: layout.UniformInset(unit.Dp(10)),
Animation: components.NewButtonAnimationDefault(),
})
buttonDelete.Label.Alignment = text.Middle
buttonDelete.Style.Font.Weight = font.Bold
scanIcon, _ := widget.NewIcon(app_icons.QRCodeScanner)
buttonScanAddr := components.NewButton(components.ButtonStyle{
Rounded: components.UniformRounded(unit.Dp(5)),
Icon: scanIcon,
Animation: components.NewButtonAnimationDefault(),
})
txtName := prefabs.NewTextField()
txtAddr := prefabs.NewInput()
txtNote := prefabs.NewTextField()
txtNote.Editor().SingleLine = false
txtNote.Editor().Submit = false
headerPageAnimation := prefabs.NewPageHeaderAnimation(PAGE_CONTACT_FORM)
return &PageContactForm{
headerPageAnimation: headerPageAnimation,
buttonSave: buttonSave,
buttonDelete: buttonDelete,
buttonScanAddr: buttonScanAddr,
txtName: txtName,
txtAddr: txtAddr,
txtNote: txtNote,
list: list,
}
}
func (p *PageContactForm) IsActive() bool {
return p.isActive
}
func (p *PageContactForm) Enter() {
p.isActive = p.headerPageAnimation.Enter(page_instance.header)
if p.contact != nil {
page_instance.header.Title = func() string { return lang.Translate("Edit Contact") }
p.txtName.SetValue(p.contact.Name)
p.txtAddr.SetValue(p.contact.Addr)
p.txtNote.SetValue(p.contact.Note)
} else {
page_instance.header.Title = func() string { return lang.Translate("New Contact") }
}
page_instance.header.Subtitle = nil
page_instance.header.LeftLayout = nil
page_instance.header.RightLayout = nil
}
func (p *PageContactForm) Leave() {
p.isActive = p.headerPageAnimation.Leave(page_instance.header)
p.contact = nil
}
func (p *PageContactForm) Layout(gtx layout.Context, th *material.Theme) layout.Dimensions {
defer p.headerPageAnimation.Update(gtx, func() { p.isActive = false }).Push(gtx.Ops).Pop()
if p.buttonSave.Clicked(gtx) {
err := p.submitForm()
if err != nil {
notification_modal.Open(notification_modal.Params{
Type: notification_modal.ERROR,
Title: lang.Translate("Error"),
Text: err.Error(),
})
} else {
notification_modal.Open(notification_modal.Params{
Type: notification_modal.SUCCESS,
Title: lang.Translate("Success"),
Text: lang.Translate("New contact added."),
CloseAfter: notification_modal.CLOSE_AFTER_DEFAULT,
})
page_instance.pageContacts.Load()
page_instance.header.GoBack()
p.ClearForm()
}
}
if p.buttonDelete.Clicked(gtx) {
go func() {
yesChan := confirm_modal.Instance.Open(confirm_modal.ConfirmText{})
if <-yesChan {
wallet := wallet_manager.OpenedWallet
err := wallet.DelContact(p.contact.Addr)
if err != nil {
notification_modal.Open(notification_modal.Params{
Type: notification_modal.ERROR,
Title: lang.Translate("Error"),
Text: err.Error(),
})
} else {
notification_modal.Open(notification_modal.Params{
Type: notification_modal.SUCCESS,
Title: lang.Translate("Success"),
Text: lang.Translate("Contact deleted."),
CloseAfter: notification_modal.CLOSE_AFTER_DEFAULT,
})
page_instance.pageContacts.Load()
page_instance.header.GoBack()
p.ClearForm()
}
}
}()
}
if p.buttonScanAddr.Clicked(gtx) {
go qrcode_scan_modal.Instance.Open()
}
{
sent, value := qrcode_scan_modal.Instance.Value()
if sent {
p.txtAddr.SetValue(value)
}
}
widgets := []layout.Widget{
func(gtx layout.Context) layout.Dimensions {
return p.txtName.Layout(gtx, th, lang.Translate("Name"), "")
},
func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
lbl := material.Label(th, unit.Sp(20), lang.Translate("Address"))
lbl.Font.Weight = font.Bold
return lbl.Layout(gtx)
}),
layout.Rigid(layout.Spacer{Height: unit.Dp(3)}.Layout),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
p.txtAddr.Colors = theme.Current.InputColors
p.txtDims = p.txtAddr.Layout(gtx, th, "")
return p.txtDims
}),
layout.Rigid(layout.Spacer{Width: unit.Dp(10)}.Layout),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
p.buttonScanAddr.Style.Colors = theme.Current.ButtonPrimaryColors
p.buttonScanAddr.Flex = true
size := image.Pt(p.txtDims.Size.Y, p.txtDims.Size.Y)
gtx.Constraints.Min = size
gtx.Constraints.Max = size
return p.buttonScanAddr.Layout(gtx, th)
}),
)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
addr, err := globals.ParseValidateAddress(p.txtAddr.Value())
if err == nil {
status := lang.Translate("This is a valid address.")
if addr.IsIntegratedAddress() {
status = lang.Translate("This is an integrated address.")
}
return layout.Inset{Top: unit.Dp(3)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
lbl := material.Label(th, unit.Sp(14), status)
lbl.Color = theme.Current.TextMuteColor
return lbl.Layout(gtx)
})
}
return layout.Dimensions{}
}),
)
},
func(gtx layout.Context) layout.Dimensions {
p.txtNote.Input.EditorMinY = gtx.Dp(75)
return p.txtNote.Layout(gtx, th, lang.Translate("Note"), "")
},
func(gtx layout.Context) layout.Dimensions {
if p.contact != nil {
p.buttonSave.Text = lang.Translate("SAVE CONTACT")
} else {
p.buttonSave.Text = lang.Translate("ADD CONTACT")
}
p.buttonSave.Style.Colors = theme.Current.ButtonPrimaryColors
return p.buttonSave.Layout(gtx, th)
},
}
if p.contact != nil {
widgets = append(widgets, func(gtx layout.Context) layout.Dimensions {
return prefabs.Divider(gtx, unit.Dp(5))
})
widgets = append(widgets, func(gtx layout.Context) layout.Dimensions {
p.buttonDelete.Text = lang.Translate("DELETE CONTACT")
p.buttonDelete.Style.Colors = theme.Current.ButtonDangerColors
return p.buttonDelete.Layout(gtx, th)
})
}
widgets = append(widgets, func(gtx layout.Context) layout.Dimensions {
return layout.Spacer{Height: unit.Dp(30)}.Layout(gtx)
})
listStyle := material.List(th, p.list)
listStyle.AnchorStrategy = material.Overlay
if p.txtName.Input.Clickable.Clicked(gtx) {
p.list.ScrollTo(0)
}
if p.txtAddr.Input.Clickable.Clicked(gtx) {
p.list.ScrollTo(1)
}
if p.txtNote.Input.Clickable.Clicked(gtx) {
p.list.ScrollTo(2)
}
return listStyle.Layout(gtx, len(widgets), func(gtx layout.Context, index int) layout.Dimensions {
return layout.Inset{
Top: unit.Dp(0), Bottom: unit.Dp(20),
Left: theme.PagePadding, Right: theme.PagePadding,
}.Layout(gtx, widgets[index])
})
}
func (p *PageContactForm) ClearForm() {
p.contact = nil
p.txtName.Editor().SetText("")
p.txtAddr.Editor.SetText("")
p.txtNote.Editor().SetText("")
}
func (p *PageContactForm) submitForm() error {
txtName := p.txtName.Editor()
txtAddr := p.txtAddr.Editor
txtNote := p.txtNote.Editor()
// don't validate addr because of service names
//_, err := globals.ParseValidateAddress(txtAddr.Text())
// if err != nil {
// return errors.New("invalid address")
//}
if txtName.Text() == "" {
return fmt.Errorf("name cannot be empty")
}
wallet := wallet_manager.OpenedWallet
err := wallet.StoreContact(wallet_manager.Contact{
Name: txtName.Text(),
Addr: txtAddr.Text(),
Note: txtNote.Text(),
Timestamp: time.Now().Unix(),
})
if err != nil {
return err
}
p.ClearForm()
return nil
}