/
agenda_vote_modal.go
256 lines (215 loc) · 6.78 KB
/
agenda_vote_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
package governance
import (
"sort"
"gioui.org/font/gofont"
"gioui.org/layout"
"gioui.org/text"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/planetdecred/dcrlibwallet"
"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"
)
type agendaVoteModal struct {
*load.Load
modal decredmaterial.Modal
// tickets that have not been spent by a vote or revocation (unspent) and that
// have not expired (unexpired).
votableTickets []*dcrlibwallet.Transaction
agenda *dcrlibwallet.Agenda
isVoting bool
modalUpdateCount int // this keeps track of the number of times the modal has been updated.
onPreferenceUpdated func()
walletSelector *WalletSelector
ticketSelector *ticketSelector
spendingPassword decredmaterial.Editor
materialLoader material.LoaderStyle
voteChoices []string
initialValue string
optionsRadioGroup *widget.Enum
voteBtn decredmaterial.Button
cancelBtn decredmaterial.Button
}
func newAgendaVoteModal(l *load.Load, agenda *dcrlibwallet.Agenda, onPreferenceUpdated func()) *agendaVoteModal {
avm := &agendaVoteModal{
Load: l,
modal: *l.Theme.ModalFloatTitle(),
agenda: agenda,
onPreferenceUpdated: onPreferenceUpdated,
materialLoader: material.Loader(material.NewTheme(gofont.Collection())),
optionsRadioGroup: new(widget.Enum),
spendingPassword: l.Theme.EditorPassword(new(widget.Editor), "Spending password"),
voteBtn: l.Theme.Button("Update Preference"),
cancelBtn: l.Theme.OutlineButton("Cancel"),
}
avm.voteBtn.Background = l.Theme.Color.Gray3
avm.voteBtn.Color = l.Theme.Color.Surface
avm.walletSelector = NewWalletSelector(l).
Title("Select wallet").
WalletSelected(func(w *dcrlibwallet.Wallet) {
avm.modalUpdateCount = 0 // modal just opened.
avm.FetchUnspentUnexpiredTickets(w.ID)
avm.modalUpdateCount++
// update agenda options prefrence to that of the selected wallet
consensusItems := components.LoadAgendas(avm.Load, w, false)
for _, consensusItem := range consensusItems {
if consensusItem.Agenda.AgendaID == agenda.AgendaID {
voteChoices := make([]string, len(consensusItem.Agenda.Choices))
for i := range consensusItem.Agenda.Choices {
voteChoices[i] = consensusItem.Agenda.Choices[i].Id
}
avm.voteChoices = voteChoices
avm.initialValue = consensusItem.Agenda.VotingPreference
avm.optionsRadioGroup.Value = avm.initialValue
}
}
}).
WalletValidator(func(w *dcrlibwallet.Wallet) bool {
return !w.IsWatchingOnlyWallet()
})
return avm
}
func (avm *agendaVoteModal) FetchUnspentUnexpiredTickets(walletID int) {
go func() {
wallet := avm.WL.MultiWallet.WalletWithID(walletID)
tickets, err := wallet.UnspentUnexpiredTickets()
if err != nil {
avm.Toast.NotifyError(err.Error())
return
}
// sort by newest first
sort.Slice(tickets[:], func(i, j int) bool {
var timeStampI = tickets[i].Timestamp
var timeStampJ = tickets[j].Timestamp
return timeStampI > timeStampJ
})
avm.votableTickets = make([]*dcrlibwallet.Transaction, len(tickets))
for i := range tickets {
avm.votableTickets[i] = &tickets[i]
}
avm.RefreshWindow()
}()
}
func (avm *agendaVoteModal) ModalID() string {
return ModalInputVote
}
func (avm *agendaVoteModal) OnResume() {
avm.walletSelector.SelectFirstValidWallet()
avm.initialValue = avm.agenda.VotingPreference
avm.optionsRadioGroup.Value = avm.initialValue
}
func (avm *agendaVoteModal) OnDismiss() {}
func (avm *agendaVoteModal) Show() {
avm.ShowModal(avm)
}
func (avm *agendaVoteModal) Dismiss() {
avm.DismissModal(avm)
}
func (avm *agendaVoteModal) Handle() {
for avm.cancelBtn.Clicked() {
if avm.isVoting {
continue
}
avm.Dismiss()
}
_, isChanged := decredmaterial.HandleEditorEvents(avm.spendingPassword.Editor)
if isChanged {
avm.spendingPassword.SetError("")
}
if len(avm.votableTickets) != 0 {
if avm.modalUpdateCount == 1 { // modal window has been updated once.
avm.modalUpdateCount++
avm.ticketSelector = newTicketSelector(avm.Load, avm.votableTickets).Title("Select a ticket")
}
}
validToVote := avm.optionsRadioGroup.Value != "" && avm.optionsRadioGroup.Value != avm.initialValue && avm.spendingPassword.Editor.Text() != ""
avm.voteBtn.SetEnabled(validToVote)
if avm.voteBtn.Enabled() {
avm.voteBtn.Background = avm.Theme.Color.Primary
}
for avm.voteBtn.Clicked() {
if avm.isVoting {
break
}
if !validToVote {
break
}
avm.isVoting = true
avm.sendVotes()
}
if avm.modal.BackdropClicked(true) {
avm.Dismiss()
}
}
// - Layout
func (avm *agendaVoteModal) Layout(gtx layout.Context) D {
w := []layout.Widget{
func(gtx C) D {
t := avm.Theme.H6("Update Voting Preference")
t.Font.Weight = text.SemiBold
return t.Layout(gtx)
},
avm.Theme.Body1("Select one of the options below to vote").Layout,
avm.walletSelector.Layout,
func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx, avm.layoutItems()...)
}),
)
},
func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(avm.spendingPassword.Layout),
)
},
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.MarginPadding8}.Layout(gtx, avm.cancelBtn.Layout)
}),
layout.Rigid(func(gtx C) D {
if avm.isVoting {
return avm.materialLoader.Layout(gtx)
}
return avm.voteBtn.Layout(gtx)
}),
)
})
},
}
return avm.modal.Layout(gtx, w)
}
func (avm *agendaVoteModal) layoutItems() []layout.FlexChild {
items := make([]layout.FlexChild, 0)
for _, voteChoice := range avm.voteChoices {
radioBtn := avm.Load.Theme.RadioButton(avm.optionsRadioGroup, voteChoice, voteChoice, avm.Load.Theme.Color.DeepBlue, avm.Load.Theme.Color.Primary)
radioItem := layout.Rigid(radioBtn.Layout)
items = append(items, radioItem)
}
return items
}
func (avm *agendaVoteModal) sendVotes() {
go func() {
password := []byte(avm.spendingPassword.Editor.Text())
defer func() {
avm.isVoting = false
}()
choiceID := avm.optionsRadioGroup.Value
err := avm.walletSelector.selectedWallet.SetVoteChoice(avm.agenda.AgendaID, choiceID, "", password)
if err != nil {
if err.Error() == dcrlibwallet.ErrInvalidPassphrase {
avm.spendingPassword.SetError("Invalid password")
} else {
avm.Toast.NotifyError(err.Error())
}
return
}
avm.Toast.Notify("Vote preference updated successfully")
avm.Dismiss()
avm.onPreferenceUpdated()
}()
}