full loop with backend and gui state
This commit is contained in:
parent
3116166a62
commit
d22ff45092
|
@ -1,49 +1,80 @@
|
|||
package backend
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.ewintr.nl/emdb/client"
|
||||
"code.ewintr.nl/emdb/storage"
|
||||
)
|
||||
|
||||
type Backend struct {
|
||||
s *State
|
||||
c chan Command
|
||||
log []string
|
||||
in chan Command
|
||||
out chan State
|
||||
logLines []string
|
||||
movieRepo *storage.MovieRepository
|
||||
tmdb *client.TMDB
|
||||
}
|
||||
|
||||
func NewBackend() *Backend {
|
||||
func NewBackend(movieRepo *storage.MovieRepository, tmdb *client.TMDB) *Backend {
|
||||
b := &Backend{
|
||||
s: NewState(),
|
||||
c: make(chan Command),
|
||||
log: make([]string, 0),
|
||||
in: make(chan Command),
|
||||
out: make(chan State),
|
||||
logLines: make([]string, 0),
|
||||
movieRepo: movieRepo,
|
||||
tmdb: tmdb,
|
||||
}
|
||||
go b.Run()
|
||||
|
||||
b.in <- Command{Name: CommandRefreshWatched}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Backend) Out() *State {
|
||||
return b.s
|
||||
func (b *Backend) Out() chan State {
|
||||
return b.out
|
||||
}
|
||||
|
||||
func (b *Backend) In() chan Command {
|
||||
return b.c
|
||||
return b.in
|
||||
}
|
||||
|
||||
func (b *Backend) Run() {
|
||||
for cmd := range b.c {
|
||||
for cmd := range b.in {
|
||||
switch cmd.Name {
|
||||
case CommandAdd:
|
||||
newName, ok := cmd.Args[ArgName]
|
||||
if ok {
|
||||
newNameStr, ok := newName.(string)
|
||||
if ok {
|
||||
b.s.Watched.Append(newNameStr)
|
||||
//b.s.Watched.Append(newNameStr)
|
||||
b.Log("Item added: " + newNameStr)
|
||||
}
|
||||
}
|
||||
case CommandRefreshWatched:
|
||||
b.RefreshWatched()
|
||||
default:
|
||||
b.Error(fmt.Errorf("unknown command: %s", cmd.Name))
|
||||
}
|
||||
|
||||
b.out <- *b.s
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Backend) Log(msg string) {
|
||||
b.log = append(b.log, msg)
|
||||
b.s.Log.Set(strings.Join(b.log, "\n"))
|
||||
func (b *Backend) RefreshWatched() {
|
||||
watched, err := b.movieRepo.FindAll()
|
||||
if err != nil {
|
||||
b.Error(fmt.Errorf("could not refresh watched: %w", err))
|
||||
}
|
||||
b.s.Watched = watched
|
||||
}
|
||||
|
||||
func (b *Backend) Error(err error) {
|
||||
b.Log(fmt.Sprintf("ERROR: %s", err))
|
||||
}
|
||||
|
||||
func (b *Backend) Log(msg string) {
|
||||
b.logLines = append(b.logLines, msg)
|
||||
b.s.Log = b.logLines
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package backend
|
|||
|
||||
const (
|
||||
CommandAdd = "add"
|
||||
CommandRefreshWatched = "refreshWatched"
|
||||
|
||||
ArgName = "name"
|
||||
)
|
||||
|
|
|
@ -1,19 +1,17 @@
|
|||
package backend
|
||||
|
||||
import "fyne.io/fyne/v2/data/binding"
|
||||
import (
|
||||
"code.ewintr.nl/emdb/storage"
|
||||
)
|
||||
|
||||
type State struct {
|
||||
Watched binding.StringList
|
||||
Log binding.String
|
||||
Watched []storage.Movie
|
||||
Log []string
|
||||
}
|
||||
|
||||
func NewState() *State {
|
||||
watched := binding.BindStringList(
|
||||
&[]string{"Item 1", "Item 2", "Item 3"},
|
||||
)
|
||||
|
||||
return &State{
|
||||
Watched: watched,
|
||||
Log: binding.NewString(),
|
||||
Watched: make([]storage.Movie, 0),
|
||||
Log: make([]string, 0),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package gui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"code.ewintr.nl/emdb/desktop-client/backend"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/app"
|
||||
|
@ -13,11 +15,12 @@ import (
|
|||
type GUI struct {
|
||||
a fyne.App
|
||||
w fyne.Window
|
||||
s *backend.State
|
||||
c chan backend.Command
|
||||
in chan backend.State
|
||||
out chan backend.Command
|
||||
s *State
|
||||
}
|
||||
|
||||
func New(c chan backend.Command, s *backend.State) *GUI {
|
||||
func New(out chan backend.Command, in chan backend.State) *GUI {
|
||||
a := app.New()
|
||||
w := a.NewWindow("EMDB")
|
||||
w.Resize(fyne.NewSize(800, 600))
|
||||
|
@ -25,8 +28,9 @@ func New(c chan backend.Command, s *backend.State) *GUI {
|
|||
g := &GUI{
|
||||
a: a,
|
||||
w: w,
|
||||
s: s,
|
||||
c: c,
|
||||
in: in,
|
||||
out: out,
|
||||
s: NewState(),
|
||||
}
|
||||
|
||||
g.SetContent()
|
||||
|
@ -35,9 +39,27 @@ func New(c chan backend.Command, s *backend.State) *GUI {
|
|||
}
|
||||
|
||||
func (g *GUI) Run() {
|
||||
go func() {
|
||||
for s := range g.in {
|
||||
g.Update(s)
|
||||
}
|
||||
}()
|
||||
g.w.ShowAndRun()
|
||||
}
|
||||
|
||||
func (g *GUI) Update(bs backend.State) {
|
||||
// watched
|
||||
watched := make([]string, 0, len(bs.Watched))
|
||||
for _, m := range bs.Watched {
|
||||
watched = append(watched, m.Title)
|
||||
}
|
||||
g.s.Watched.Set(watched)
|
||||
|
||||
// log
|
||||
g.s.Log.Set(strings.Join(bs.Log, "\n"))
|
||||
|
||||
}
|
||||
|
||||
func (g *GUI) SetContent() {
|
||||
label1 := widget.NewLabel("Label 1")
|
||||
label2 := widget.NewLabel("Label 2")
|
||||
|
@ -49,7 +71,7 @@ func (g *GUI) SetContent() {
|
|||
form := container.New(layout.NewFormLayout(), label1, input, label2, value2)
|
||||
|
||||
button := widget.NewButton("Save", func() {
|
||||
g.c <- backend.Command{
|
||||
g.out <- backend.Command{
|
||||
Name: backend.CommandAdd,
|
||||
Args: map[string]any{
|
||||
backend.ArgName: input.Text,
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package gui
|
||||
|
||||
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,12 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"code.ewintr.nl/emdb/client"
|
||||
"code.ewintr.nl/emdb/desktop-client/backend"
|
||||
"code.ewintr.nl/emdb/desktop-client/gui"
|
||||
"code.ewintr.nl/emdb/storage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := backend.NewBackend()
|
||||
tmdb, err := client.NewTMDB(os.Getenv("TMDB_API_KEY"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
dbHost := os.Getenv("EMDB_DB_HOST")
|
||||
dbName := os.Getenv("EMDB_DB_NAME")
|
||||
dbUser := os.Getenv("EMDB_DB_USER")
|
||||
dbPassword := os.Getenv("EMDB_DB_PASSWORD")
|
||||
pgConnStr := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable", dbHost, dbUser, dbPassword, dbName)
|
||||
dbPostgres, err := storage.NewPostgres(pgConnStr)
|
||||
if err != nil {
|
||||
fmt.Printf("could not create new postgres repo: %s", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
movieRepo := storage.NewMovieRepository(dbPostgres)
|
||||
|
||||
b := backend.NewBackend(movieRepo, tmdb)
|
||||
g := gui.New(b.In(), b.Out())
|
||||
g.Run()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue