diff --git a/desktop-client/backend/backend.go b/desktop-client/backend/backend.go new file mode 100644 index 0000000..813a2c0 --- /dev/null +++ b/desktop-client/backend/backend.go @@ -0,0 +1,49 @@ +package backend + +import "strings" + +type Backend struct { + s *State + c chan Command + log []string +} + +func NewBackend() *Backend { + b := &Backend{ + s: NewState(), + c: make(chan Command), + log: make([]string, 0), + } + go b.Run() + + return b +} + +func (b *Backend) Out() *State { + return b.s +} + +func (b *Backend) In() chan Command { + return b.c +} + +func (b *Backend) Run() { + for cmd := range b.c { + switch cmd.Name { + case CommandAdd: + newName, ok := cmd.Args[ArgName] + if ok { + newNameStr, ok := newName.(string) + if ok { + b.s.Watched.Append(newNameStr) + b.Log("Item added: " + newNameStr) + } + } + } + } +} + +func (b *Backend) Log(msg string) { + b.log = append(b.log, msg) + b.s.Log.Set(strings.Join(b.log, "\n")) +} diff --git a/desktop-client/backend/command.go b/desktop-client/backend/command.go new file mode 100644 index 0000000..d53ea1a --- /dev/null +++ b/desktop-client/backend/command.go @@ -0,0 +1,14 @@ +package backend + +const ( + CommandAdd = "add" + + ArgName = "name" +) + +type CommandName string + +type Command struct { + Name string + Args map[string]any +} diff --git a/desktop-client/backend/state.go b/desktop-client/backend/state.go new file mode 100644 index 0000000..0e53f27 --- /dev/null +++ b/desktop-client/backend/state.go @@ -0,0 +1,19 @@ +package backend + +import "fyne.io/fyne/v2/data/binding" + +type State struct { + Watched binding.StringList + Log binding.String +} + +func NewState() *State { + watched := binding.BindStringList( + &[]string{"Item 1", "Item 2", "Item 3"}, + ) + + return &State{ + Watched: watched, + Log: binding.NewString(), + } +} diff --git a/desktop-client/gui/gui.go b/desktop-client/gui/gui.go index c2e2be5..974e3f5 100644 --- a/desktop-client/gui/gui.go +++ b/desktop-client/gui/gui.go @@ -1,11 +1,11 @@ package gui import ( - "log" - + "code.ewintr.nl/emdb/desktop-client/backend" "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" + "fyne.io/fyne/v2/data/binding" "fyne.io/fyne/v2/layout" "fyne.io/fyne/v2/widget" ) @@ -13,26 +13,32 @@ import ( type GUI struct { a fyne.App w fyne.Window + s *backend.State + c chan backend.Command } -func New() *GUI { +func New(c chan backend.Command, s *backend.State) *GUI { a := app.New() w := a.NewWindow("EMDB") - - w.SetContent(Layout()) w.Resize(fyne.NewSize(800, 600)) - return &GUI{ + g := &GUI{ a: a, w: w, + s: s, + c: c, } + + g.SetContent() + + return g } func (g *GUI) Run() { g.w.ShowAndRun() } -func Layout() fyne.CanvasObject { +func (g *GUI) SetContent() { label1 := widget.NewLabel("Label 1") label2 := widget.NewLabel("Label 2") value2 := widget.NewLabel("Something") @@ -43,19 +49,32 @@ func Layout() fyne.CanvasObject { form := container.New(layout.NewFormLayout(), label1, input, label2, value2) button := widget.NewButton("Save", func() { - log.Println("Content was:", input.Text) + g.c <- backend.Command{ + Name: backend.CommandAdd, + Args: map[string]any{ + backend.ArgName: input.Text, + }, + } }) grid := container.NewBorder(nil, button, nil, nil, form) - logLines := container.NewVScroll(widget.NewLabel("Log\n\n\n\n\n\n\n\nhoi\n\n\n\n\n\na lot")) + logLines := container.NewVScroll(widget.NewLabelWithData(g.s.Log)) //logLines.ScrollToBottom() + list := widget.NewListWithData(g.s.Watched, + func() fyne.CanvasObject { + return widget.NewLabel("template") + }, + func(i binding.DataItem, o fyne.CanvasObject) { + o.(*widget.Label).Bind(i.(binding.String)) + }) + tabs := container.NewAppTabs( - container.NewTabItem("Watched", widget.NewLabel("Watched")), + container.NewTabItem("Watched", list), container.NewTabItem("TheMovieDB", grid), container.NewTabItem("Log", logLines), ) - return tabs + g.w.SetContent(tabs) } diff --git a/desktop-client/main.go b/desktop-client/main.go index 46fd794..5ee5e1c 100644 --- a/desktop-client/main.go +++ b/desktop-client/main.go @@ -1,8 +1,12 @@ package main -import "code.ewintr.nl/emdb/desktop-client/gui" +import ( + "code.ewintr.nl/emdb/desktop-client/backend" + "code.ewintr.nl/emdb/desktop-client/gui" +) func main() { - g := gui.New() + b := backend.NewBackend() + g := gui.New(b.In(), b.Out()) g.Run() }