2022-10-18 16:58:12 +02:00
|
|
|
package screen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fyne.io/fyne/v2"
|
2022-10-24 15:57:55 +02:00
|
|
|
"fyne.io/fyne/v2/container"
|
2022-10-18 16:58:12 +02:00
|
|
|
"fyne.io/fyne/v2/widget"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2022-10-24 15:57:55 +02:00
|
|
|
fields []*FormField
|
|
|
|
commands chan interface{}
|
2022-10-31 15:59:58 +01:00
|
|
|
show chan ShowRequest
|
2022-10-24 15:57:55 +02:00
|
|
|
root *fyne.Container
|
2022-10-18 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
2022-10-31 15:59:58 +01:00
|
|
|
func NewConfig(commands chan interface{}, show chan ShowRequest) *Config {
|
2022-10-18 16:58:12 +02:00
|
|
|
fields := []*FormField{}
|
|
|
|
for _, f := range [][2]string{
|
|
|
|
{"ConfigIMAPURL", "imap url"},
|
|
|
|
{"ConfigIMAPUser", "imap user"},
|
|
|
|
{"ConfigIMAPPassword", "imap password"},
|
|
|
|
{"ConfigIMAPFolderPrefix", "imap folder prefix"},
|
|
|
|
{"ConfigSMTPURL", "smtp url"},
|
|
|
|
{"ConfigSMTPUser", "smtp user"},
|
|
|
|
{"ConfigSMTPPassword", "smtp password"},
|
|
|
|
{"ConfigGTEToName", "to name"},
|
|
|
|
{"ConfigGTEToAddress", "to address"},
|
|
|
|
{"ConfigGTEFromName", "from name"},
|
|
|
|
{"ConfigGTEFromAddress", "from address"},
|
|
|
|
{"ConfigGTELocalDBPath", "local db path"},
|
|
|
|
} {
|
|
|
|
fields = append(fields, NewFormField(f[0], f[1]))
|
|
|
|
}
|
|
|
|
|
2022-10-24 15:57:55 +02:00
|
|
|
config := &Config{
|
|
|
|
fields: fields,
|
|
|
|
commands: commands,
|
|
|
|
show: show,
|
2022-10-18 16:58:12 +02:00
|
|
|
}
|
2022-10-24 15:57:55 +02:00
|
|
|
config.Init()
|
|
|
|
|
|
|
|
return config
|
2022-10-18 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *Config) Save() {
|
|
|
|
req := SaveConfigRequest{
|
|
|
|
Fields: map[string]string{},
|
|
|
|
}
|
|
|
|
for _, f := range cf.fields {
|
|
|
|
req.Fields[f.Key] = f.GetValue()
|
|
|
|
}
|
2022-10-24 15:57:55 +02:00
|
|
|
cf.commands <- req
|
2022-10-31 15:59:58 +01:00
|
|
|
cf.show <- ShowRequest{Screen: "tasks"}
|
2022-10-18 16:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *Config) Refresh(state State) {
|
|
|
|
for _, f := range cf.fields {
|
|
|
|
if v, ok := state.Config[f.Key]; ok {
|
|
|
|
f.SetValue(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 15:57:55 +02:00
|
|
|
func (cf *Config) Init() {
|
2022-10-18 16:58:12 +02:00
|
|
|
configForm := widget.NewForm()
|
|
|
|
for _, f := range cf.fields {
|
|
|
|
w := widget.NewEntry()
|
|
|
|
w.Bind(f.Value)
|
|
|
|
configForm.Append(f.Label, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
configForm.SubmitText = "save"
|
|
|
|
configForm.OnSubmit = cf.Save
|
|
|
|
configForm.Enable()
|
|
|
|
|
2022-10-24 15:57:55 +02:00
|
|
|
cf.root = container.NewBorder(
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
configForm,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *Config) Content() *fyne.Container {
|
|
|
|
return cf.root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *Config) Hide() {
|
|
|
|
cf.root.Hide()
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:59:58 +01:00
|
|
|
func (cf *Config) Show(_ Task) {
|
2022-10-24 15:57:55 +02:00
|
|
|
cf.root.Show()
|
2022-10-18 16:58:12 +02:00
|
|
|
}
|