-
Notifications
You must be signed in to change notification settings - Fork 3
/
action-state.go
267 lines (243 loc) · 6.07 KB
/
action-state.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
package cryptonym
import (
"fmt"
"fyne.io/fyne"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
errs "github.com/blockpane/cryptonym/errLog"
"github.com/fioprotocol/fio-go"
"reflect"
"strings"
"sync"
)
type AbiFormItem struct {
Contract string
Action string
Order int
Name *string
Type *widget.Select
SendAs *widget.Select
Variation *widget.Select
Len *widget.Select
Input *widget.Entry
Value *interface{}
IsSlice bool
SliceValue []*interface{}
convert func(s interface{}) interface{}
typeOverride string
noJsonEscape bool // if true uses fmt, otherwise json-encodes the value ... fmt is useful for some numeric values
}
type Abi struct {
mux sync.RWMutex
lookUp map[string]int
Rows []AbiFormItem
Action string
Contract string
}
func NewAbi(length int) *Abi {
return &Abi{
Rows: make([]AbiFormItem, length),
lookUp: make(map[string]int),
}
}
func (abi *Abi) AppendRow(myName string, account *fio.Account, form *widget.Form) {
go func() {
if myName == "" {
myName = fmt.Sprintf("new_row_%d", len(abi.Rows))
}
if abi.Rows == nil {
abi.Rows = make([]AbiFormItem, 0)
}
typeSelect := &widget.Select{}
in := widget.NewEntry()
num := &widget.Select{}
inputBox := widget.NewHBox(
widget.NewLabel("Input:"),
in,
)
variation := &widget.Select{}
sendAs := &widget.Select{}
field := AbiFormItem{
Contract: abi.Contract,
Action: abi.Action,
Order: len(abi.Rows),
Name: &myName,
Type: typeSelect,
SendAs: sendAs,
Variation: variation,
Len: num,
Input: in,
}
in.OnChanged = func(s string) {
FormState.UpdateInput(myName, in)
}
typeSelect = widget.NewSelect(abiSelectTypes("string"), func(s string) {
FormState.UpdateType("string", typeSelect)
})
typeSelect.SetSelected("string")
sendAs = widget.NewSelect(sendAsSelectTypes, func(send string) {
if !strings.Contains(send, "form value") {
inputBox.Hide()
} else {
inputBox.Show()
}
var sel string
variation.Options, sel = sendAsVariant(send)
variation.SetSelected(sel)
FormState.UpdateSendAs(myName, sendAs)
})
sendAs.SetSelected("bytes/string")
variation = widget.NewSelect(bytesVar, func(s string) {
showNum, numVals, sel := getLength(s)
if showNum {
num.Show()
} else {
num.Hide()
}
num.Options = numVals
num.SetSelected(sel)
FormState.UpdateLen(myName, num)
FormState.UpdateVariation(myName, variation)
})
variation.SetSelected("many AAAA...")
num = widget.NewSelect(bytesLen, func(s string) {
FormState.UpdateLen(myName, num)
})
num.SetSelected("131,072")
form.Append(*field.Name,
widget.NewVBox(
fyne.NewContainerWithLayout(layout.NewGridLayout(5),
typeSelect,
sendAs,
variation,
num,
),
inputBox,
),
)
abi.mux.Lock()
abi.Rows = append(abi.Rows, field)
abi.lookUp[myName] = len(abi.Rows) - 1
abi.mux.Unlock()
abi.UpdateInput(myName, in)
abi.UpdateSendAs(myName, sendAs)
abi.UpdateType(myName, typeSelect)
abi.UpdateLen(myName, num)
}()
}
func (abi *Abi) AddNewRowButton(name *widget.Entry, account *fio.Account, form *widget.Form) *widget.Button {
b := &widget.Button{}
b = widget.NewButtonWithIcon("Add Row", theme.ContentAddIcon(), func() {
defer name.SetText("")
abi.mux.RLock()
if abi.Rows != nil && len(abi.Rows) > 0 {
if abi.lookUp[name.Text] > 0 || *abi.Rows[0].Name == name.Text {
errs.ErrChan <- "cannot add duplicate name for abi struct"
return
abi.mux.RUnlock()
}
}
abi.mux.RUnlock()
abi.AppendRow(name.Text, account, form)
})
return b
}
func (abi *Abi) UpdateValueWithConvert(index *int, value interface{}, isSlice bool, abiType string, noJsonEscape bool) {
if len(abi.Rows) == 0 {
return
}
newVal := value
abi.mux.Lock()
if abiType != "" {
abi.Rows[*index].typeOverride = abiType
if abiType == "string" {
// reflect on our interface to see if it should be converted from a number to a quoted number:
t := reflect.TypeOf(value)
if strings.Contains(t.String(), "int") || strings.Contains(t.String(), "float") {
newVal = fmt.Sprintf("%v", value)
}
}
} else {
abi.Rows[*index].typeOverride = ""
}
abi.mux.Unlock()
abi.UpdateValue(index, newVal, isSlice, noJsonEscape)
}
func (abi *Abi) UpdateValue(index *int, value interface{}, isSlice bool, noJsonEscape bool) {
if len(abi.Rows) == 0 {
return
}
abi.Rows[*index].noJsonEscape = noJsonEscape
abi.mux.Lock()
if !isSlice {
abi.Rows[*index].Value = &value
} else {
sl := make([]*interface{}, 0)
sl = append(sl, &value)
abi.Rows[*index].IsSlice = true
abi.Rows[*index].SliceValue = sl
}
abi.mux.Unlock()
}
func (abi *Abi) Update(index *int, abiForm AbiFormItem) {
abi.mux.Lock()
defer abi.mux.Unlock()
if len(abi.Rows) == 0 {
return
}
if index != nil {
abi.lookUp[*abiForm.Name] = *index
}
abi.Rows[abi.lookUp[*abiForm.Name]] = abiForm
}
func (abi *Abi) UpdateType(name string, t *widget.Select) {
if len(abi.Rows) == 0 {
return
}
abi.mux.Lock()
if abi.lookUp[name] != 0 || *abi.Rows[0].Name == name {
abi.Rows[abi.lookUp[name]].Type = t
}
abi.mux.Unlock()
}
func (abi *Abi) UpdateLen(name string, t *widget.Select) {
if len(abi.Rows) == 0 {
return
}
abi.mux.Lock()
if abi.lookUp[name] != 0 || *abi.Rows[0].Name == name {
abi.Rows[abi.lookUp[name]].Len = t
}
abi.mux.Unlock()
}
func (abi *Abi) UpdateVariation(name string, t *widget.Select) {
if len(abi.Rows) == 0 {
return
}
abi.mux.Lock()
if abi.lookUp[name] != 0 || *abi.Rows[0].Name == name {
abi.Rows[abi.lookUp[name]].Variation = t
}
abi.mux.Unlock()
}
func (abi *Abi) UpdateSendAs(name string, t *widget.Select) {
if len(abi.Rows) == 0 {
return
}
abi.mux.Lock()
if abi.lookUp[name] != 0 || *abi.Rows[0].Name == name {
abi.Rows[abi.lookUp[name]].SendAs = t
}
abi.mux.Unlock()
}
func (abi *Abi) UpdateInput(name string, t *widget.Entry) {
if len(abi.Rows) == 0 {
return
}
abi.mux.Lock()
if abi.lookUp[name] != 0 || *abi.Rows[0].Name == name {
abi.Rows[abi.lookUp[name]].Input = t
}
abi.mux.Unlock()
}