/
list_preference.go
243 lines (200 loc) · 6.37 KB
/
list_preference.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
package preference
import (
"gioui.org/font"
"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/values"
"github.com/crypto-power/cryptopower/ui/values/localizable"
)
type (
C = layout.Context
D = layout.Dimensions
)
var (
NetworkTypes = []ItemPreference{
{Key: string(libutils.Mainnet), Value: libutils.Mainnet.Display()},
{Key: string(libutils.Testnet), Value: libutils.Testnet.Display()},
}
// ExchOptions holds the configurable options for exchange servers.
ExchOptions = []ItemPreference{
{Key: values.BinanceExchange, Value: values.StrUsdBinance},
{Key: values.BittrexExchange, Value: values.StrUsdBittrex},
{Key: values.DefaultExchangeValue, Value: values.StrNone},
}
// LangOptions stores the configurable language options.
LangOptions = []ItemPreference{
{Key: localizable.ENGLISH, Value: values.StrEnglish},
{Key: localizable.FRENCH, Value: values.StrFrench},
{Key: localizable.SPANISH, Value: values.StrSpanish},
}
// LogOptions are the selectable debug levels.
LogOptions = []ItemPreference{
{Key: libutils.LogLevelTrace, Value: values.StrLogLevelTrace},
{Key: libutils.LogLevelDebug, Value: values.StrLogLevelDebug},
{Key: libutils.LogLevelInfo, Value: values.StrLogLevelInfo},
{Key: libutils.LogLevelWarn, Value: values.StrLogLevelWarn},
{Key: libutils.LogLevelError, Value: values.StrLogLevelError},
{Key: libutils.LogLevelCritical, Value: values.StrLogLevelCritical},
}
)
type ListPreferenceModal struct {
*load.Load
*cryptomaterial.Modal
optionsRadioGroup *widget.Enum
btnSave cryptomaterial.Button
btnCancel cryptomaterial.Button
customWidget layout.Widget
title string
preferenceKey string
defaultValue string // str-key
initialValue string
currentValue string
isWalletAccount bool
preferenceItems []ItemPreference
updateButtonClicked func(string)
}
// ItemPreference models the options shown by the list
// preference modal.
type ItemPreference struct {
Key string // option's key
Value string // option's value
}
func NewListPreference(l *load.Load, preferenceKey, defaultValue string, items []ItemPreference) *ListPreferenceModal {
lp := ListPreferenceModal{
Load: l,
preferenceKey: preferenceKey,
defaultValue: defaultValue,
btnSave: l.Theme.Button(values.String(values.StrSave)),
btnCancel: l.Theme.OutlineButton(values.String(values.StrCancel)),
preferenceItems: items,
optionsRadioGroup: new(widget.Enum),
Modal: l.Theme.ModalFloatTitle("list_preference", l.IsMobileView()),
}
lp.btnSave.Font.Weight = font.Medium
lp.btnCancel.Font.Weight = font.Medium
return &lp
}
func (lp *ListPreferenceModal) ReadPreferenceKeyedValue() string {
switch lp.preferenceKey {
case sharedW.CurrencyConversionConfigKey:
return lp.AssetsManager.GetCurrencyConversionExchange()
case sharedW.LanguagePreferenceKey:
return lp.AssetsManager.GetLanguagePreference()
case sharedW.LogLevelConfigKey:
return lp.AssetsManager.GetLogLevels()
default:
return ""
}
}
func (lp *ListPreferenceModal) SavePreferenceKeyedValue() {
val := lp.optionsRadioGroup.Value
switch lp.preferenceKey {
case sharedW.CurrencyConversionConfigKey:
lp.AssetsManager.SetCurrencyConversionExchange(val)
case sharedW.LanguagePreferenceKey:
// TODO: We should be able to update dex core's language when the user
// changes language.
lp.AssetsManager.SetLanguagePreference(val)
case sharedW.LogLevelConfigKey:
lp.AssetsManager.SetLogLevels(val)
}
}
func (lp *ListPreferenceModal) OnResume() {
initialValue := lp.ReadPreferenceKeyedValue()
if initialValue == "" {
initialValue = lp.defaultValue
}
lp.initialValue = initialValue
lp.currentValue = initialValue
lp.optionsRadioGroup.Value = lp.currentValue
}
func (lp *ListPreferenceModal) OnDismiss() {}
func (lp *ListPreferenceModal) Title(title string) *ListPreferenceModal {
lp.title = title
return lp
}
func (lp *ListPreferenceModal) UseCustomWidget(layout layout.Widget) *ListPreferenceModal {
lp.customWidget = layout
return lp
}
func (lp *ListPreferenceModal) IsWallet(setAccount bool) *ListPreferenceModal {
lp.isWalletAccount = setAccount
return lp
}
func (lp *ListPreferenceModal) UpdateValues(clicked func(val string)) *ListPreferenceModal {
lp.updateButtonClicked = clicked
return lp
}
func (lp *ListPreferenceModal) Handle() {
for lp.btnSave.Button.Clicked() {
lp.currentValue = lp.optionsRadioGroup.Value
lp.SavePreferenceKeyedValue()
lp.updateButtonClicked(lp.optionsRadioGroup.Value)
lp.RefreshTheme(lp.ParentWindow())
lp.Dismiss()
}
for lp.btnCancel.Button.Clicked() {
lp.Modal.Dismiss()
}
if lp.Modal.BackdropClicked(true) {
lp.Modal.Dismiss()
}
}
func (lp *ListPreferenceModal) Layout(gtx C) D {
var w []layout.Widget
title := func(gtx C) D {
txt := lp.Theme.H6(values.String(lp.title))
txt.Color = lp.Theme.Color.Text
return txt.Layout(gtx)
}
items := []layout.Widget{
func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx, lp.layoutItems()...)
},
func(gtx C) D {
return layout.E.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(lp.btnCancel.Layout),
layout.Rigid(lp.btnSave.Layout),
)
})
},
}
if lp.title != "" {
w = append(w, title)
}
if lp.customWidget != nil {
w = append(w, lp.customWidget)
}
for i := 0; i < len(items); i++ {
w = append(w, items[i])
}
return lp.Modal.Layout(gtx, w)
}
func (lp *ListPreferenceModal) layoutItems() []layout.FlexChild {
items := make([]layout.FlexChild, 0)
for _, v := range lp.preferenceItems {
text := values.String(v.Value)
if lp.isWalletAccount {
text = v.Value
}
radioItem := layout.Rigid(lp.Theme.RadioButton(lp.optionsRadioGroup, v.Key, text, lp.Theme.Color.DeepBlue, lp.Theme.Color.Primary).Layout)
items = append(items, radioItem)
}
return items
}
// GetKeyValue return the value for a key within a set of prefence options.
// The key is case sensitive, `Key` != `key`.
// Returns the empty string if the key is not found.
func GetKeyValue(key string, options []ItemPreference) string {
for _, option := range options {
if option.Key == key {
return option.Value
}
}
return ""
}