Skip to content

Commit

Permalink
feat: add callback
Browse files Browse the repository at this point in the history
fix: values in fieldgroups are now accessable
  • Loading branch information
virus-rpi committed May 23, 2024
1 parent b9defda commit 2d1be38
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
9 changes: 8 additions & 1 deletion gui/setupDialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"CUBUS-core/shared/forms"
"CUBUS-core/shared/translation"
"CUBUS-core/shared/types"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
Expand All @@ -23,9 +24,15 @@ func setupDialog(window fyne.Window) types.CubeConfig { // TODO: use onSubmit of
cubeSetupForm := forms.GetCubeSetupForm()
box := container.New(layout.NewVBoxLayout())

formSubmitCallback := func(values map[string]string) {
for key, value := range values {
fmt.Printf("%s: %s \n", key, value)
}
}

formPopup := dialog.NewCustomWithoutButtons(T("Setup"), box, window)
formPopup.Resize(fyne.NewSize(700, 400))
forms.FormToFyneForm(cubeSetupForm, box, formPopup, window)
forms.FormToFyneForm(cubeSetupForm, box, formPopup, window, formSubmitCallback)
formPopup.Show()

return cubeConfig
Expand Down
34 changes: 32 additions & 2 deletions shared/forms/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package forms

import (
"CUBUS-core/shared/types"
"encoding/json"
"net"
"regexp"
"strconv"
Expand Down Expand Up @@ -397,7 +398,8 @@ func (m *MultipleChoiceField) IsValid() bool {

type FieldGroup struct {
*FieldBaseType
Fields []Field
Fields []Field
heading string
}

func (f *FieldGroup) GetFieldsToDisplay() []Field {
Expand All @@ -419,6 +421,34 @@ func (f *FieldGroup) GetFieldById(id string) Field {
return nil
}

func (f *FieldGroup) GetValue() string {
fieldValues := make(map[string]string)
for _, field := range f.Fields {
fieldValues[field.GetId()] = field.GetValue()
}
jsonFieldValues, _ := json.Marshal(fieldValues)
return string(jsonFieldValues)
}

func (f *FieldGroup) GetHeading() string {
return f.heading
}

func (f *FieldGroup) SetValue(value string) {
var fieldValues map[string]string
err := json.Unmarshal([]byte(value), &fieldValues)
if err != nil {
return
}
for _, field := range f.Fields {
field.SetValue(fieldValues[field.GetId()])
}
}

func (f *FieldGroup) SetHeading(heading string) {
f.heading = heading
}

// Defining the Form Type

type Form struct {
Expand Down Expand Up @@ -497,7 +527,7 @@ func NewForm(fields ...Field) *Form {
}

func NewFieldGroup(id string, displayConditions []DisplayCondition, validators []Validator, heading string, fields ...Field) *FieldGroup {
return &FieldGroup{FieldBaseType: &FieldBaseType{Id: id, DisplayConditions: displayConditions, Validators: validators, Value: heading}, Fields: fields}
return &FieldGroup{FieldBaseType: &FieldBaseType{Id: id, DisplayConditions: displayConditions, Validators: validators}, Fields: fields, heading: heading}
}

func NewTextField(id string, displayConditions []DisplayCondition, validators []Validator, placeholder string, prompt string, defaultValue string) *TextField {
Expand Down
7 changes: 4 additions & 3 deletions shared/forms/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func fieldsToFyneForm(fields []Field, form *Form, box *fyne.Container, fyneForm
}
formItems = append(formItems, widget.NewFormItem(field.GetPrompt(), entry))
case *FieldGroup:
if field.GetValue() != "" {
formItems = append(formItems, widget.NewFormItem(field.GetValue(), widget.NewLabel("")))
if field.GetHeading() != "" {
formItems = append(formItems, widget.NewFormItem(field.GetHeading(), widget.NewLabel("")))
}
formItems = append(formItems, fieldsToFyneForm(field.GetFieldsToDisplay(), form, box, fyneForm)...)
default:
Expand All @@ -80,12 +80,13 @@ func fieldsToFyneForm(fields []Field, form *Form, box *fyne.Container, fyneForm
return formItems
}

func FormToFyneForm(form *Form, box *fyne.Container, parentDialog *dialog.CustomDialog, window fyne.Window) {
func FormToFyneForm(form *Form, box *fyne.Container, parentDialog *dialog.CustomDialog, window fyne.Window, onSubmit func(map[string]string)) {
fields := form.GetFieldsToDisplay()
fyneForm := widget.NewForm()
fyneForm.Items = fieldsToFyneForm(fields, form, box, fyneForm)
fyneForm.OnSubmit = func() {
if form.IsValid() {
onSubmit(form.GetFieldValues())
parentDialog.Hide()
} else {
dialog.ShowError(form.GetError(), window)
Expand Down

0 comments on commit 2d1be38

Please sign in to comment.