split backend and frontend
This commit is contained in:
parent
51e555e2ec
commit
3116166a62
|
@ -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"))
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package backend
|
||||||
|
|
||||||
|
const (
|
||||||
|
CommandAdd = "add"
|
||||||
|
|
||||||
|
ArgName = "name"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CommandName string
|
||||||
|
|
||||||
|
type Command struct {
|
||||||
|
Name string
|
||||||
|
Args map[string]any
|
||||||
|
}
|
|
@ -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(),
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,11 @@
|
||||||
package gui
|
package gui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"code.ewintr.nl/emdb/desktop-client/backend"
|
||||||
|
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
"fyne.io/fyne/v2/app"
|
"fyne.io/fyne/v2/app"
|
||||||
"fyne.io/fyne/v2/container"
|
"fyne.io/fyne/v2/container"
|
||||||
|
"fyne.io/fyne/v2/data/binding"
|
||||||
"fyne.io/fyne/v2/layout"
|
"fyne.io/fyne/v2/layout"
|
||||||
"fyne.io/fyne/v2/widget"
|
"fyne.io/fyne/v2/widget"
|
||||||
)
|
)
|
||||||
|
@ -13,26 +13,32 @@ import (
|
||||||
type GUI struct {
|
type GUI struct {
|
||||||
a fyne.App
|
a fyne.App
|
||||||
w fyne.Window
|
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()
|
a := app.New()
|
||||||
w := a.NewWindow("EMDB")
|
w := a.NewWindow("EMDB")
|
||||||
|
|
||||||
w.SetContent(Layout())
|
|
||||||
w.Resize(fyne.NewSize(800, 600))
|
w.Resize(fyne.NewSize(800, 600))
|
||||||
|
|
||||||
return &GUI{
|
g := &GUI{
|
||||||
a: a,
|
a: a,
|
||||||
w: w,
|
w: w,
|
||||||
|
s: s,
|
||||||
|
c: c,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g.SetContent()
|
||||||
|
|
||||||
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GUI) Run() {
|
func (g *GUI) Run() {
|
||||||
g.w.ShowAndRun()
|
g.w.ShowAndRun()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Layout() fyne.CanvasObject {
|
func (g *GUI) SetContent() {
|
||||||
label1 := widget.NewLabel("Label 1")
|
label1 := widget.NewLabel("Label 1")
|
||||||
label2 := widget.NewLabel("Label 2")
|
label2 := widget.NewLabel("Label 2")
|
||||||
value2 := widget.NewLabel("Something")
|
value2 := widget.NewLabel("Something")
|
||||||
|
@ -43,19 +49,32 @@ func Layout() fyne.CanvasObject {
|
||||||
form := container.New(layout.NewFormLayout(), label1, input, label2, value2)
|
form := container.New(layout.NewFormLayout(), label1, input, label2, value2)
|
||||||
|
|
||||||
button := widget.NewButton("Save", func() {
|
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)
|
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()
|
//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(
|
tabs := container.NewAppTabs(
|
||||||
container.NewTabItem("Watched", widget.NewLabel("Watched")),
|
container.NewTabItem("Watched", list),
|
||||||
container.NewTabItem("TheMovieDB", grid),
|
container.NewTabItem("TheMovieDB", grid),
|
||||||
container.NewTabItem("Log", logLines),
|
container.NewTabItem("Log", logLines),
|
||||||
)
|
)
|
||||||
|
|
||||||
return tabs
|
g.w.SetContent(tabs)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package main
|
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() {
|
func main() {
|
||||||
g := gui.New()
|
b := backend.NewBackend()
|
||||||
|
g := gui.New(b.In(), b.Out())
|
||||||
g.Run()
|
g.Run()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue