full loop with backend and gui state
This commit is contained in:
parent
3116166a62
commit
d22ff45092
|
@ -1,49 +1,80 @@
|
||||||
package backend
|
package backend
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"code.ewintr.nl/emdb/client"
|
||||||
|
"code.ewintr.nl/emdb/storage"
|
||||||
|
)
|
||||||
|
|
||||||
type Backend struct {
|
type Backend struct {
|
||||||
s *State
|
s *State
|
||||||
c chan Command
|
in chan Command
|
||||||
log []string
|
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{
|
b := &Backend{
|
||||||
s: NewState(),
|
s: NewState(),
|
||||||
c: make(chan Command),
|
in: make(chan Command),
|
||||||
log: make([]string, 0),
|
out: make(chan State),
|
||||||
|
logLines: make([]string, 0),
|
||||||
|
movieRepo: movieRepo,
|
||||||
|
tmdb: tmdb,
|
||||||
}
|
}
|
||||||
go b.Run()
|
go b.Run()
|
||||||
|
|
||||||
|
b.in <- Command{Name: CommandRefreshWatched}
|
||||||
|
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Backend) Out() *State {
|
func (b *Backend) Out() chan State {
|
||||||
return b.s
|
return b.out
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Backend) In() chan Command {
|
func (b *Backend) In() chan Command {
|
||||||
return b.c
|
return b.in
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Backend) Run() {
|
func (b *Backend) Run() {
|
||||||
for cmd := range b.c {
|
for cmd := range b.in {
|
||||||
switch cmd.Name {
|
switch cmd.Name {
|
||||||
case CommandAdd:
|
case CommandAdd:
|
||||||
newName, ok := cmd.Args[ArgName]
|
newName, ok := cmd.Args[ArgName]
|
||||||
if ok {
|
if ok {
|
||||||
newNameStr, ok := newName.(string)
|
newNameStr, ok := newName.(string)
|
||||||
if ok {
|
if ok {
|
||||||
b.s.Watched.Append(newNameStr)
|
//b.s.Watched.Append(newNameStr)
|
||||||
b.Log("Item added: " + 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) {
|
func (b *Backend) RefreshWatched() {
|
||||||
b.log = append(b.log, msg)
|
watched, err := b.movieRepo.FindAll()
|
||||||
b.s.Log.Set(strings.Join(b.log, "\n"))
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package backend
|
package backend
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CommandAdd = "add"
|
CommandAdd = "add"
|
||||||
|
CommandRefreshWatched = "refreshWatched"
|
||||||
|
|
||||||
ArgName = "name"
|
ArgName = "name"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,19 +1,17 @@
|
||||||
package backend
|
package backend
|
||||||
|
|
||||||
import "fyne.io/fyne/v2/data/binding"
|
import (
|
||||||
|
"code.ewintr.nl/emdb/storage"
|
||||||
|
)
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
Watched binding.StringList
|
Watched []storage.Movie
|
||||||
Log binding.String
|
Log []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewState() *State {
|
func NewState() *State {
|
||||||
watched := binding.BindStringList(
|
|
||||||
&[]string{"Item 1", "Item 2", "Item 3"},
|
|
||||||
)
|
|
||||||
|
|
||||||
return &State{
|
return &State{
|
||||||
Watched: watched,
|
Watched: make([]storage.Movie, 0),
|
||||||
Log: binding.NewString(),
|
Log: make([]string, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package gui
|
package gui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
"code.ewintr.nl/emdb/desktop-client/backend"
|
"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"
|
||||||
|
@ -11,22 +13,24 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type GUI struct {
|
type GUI struct {
|
||||||
a fyne.App
|
a fyne.App
|
||||||
w fyne.Window
|
w fyne.Window
|
||||||
s *backend.State
|
in chan backend.State
|
||||||
c chan backend.Command
|
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()
|
a := app.New()
|
||||||
w := a.NewWindow("EMDB")
|
w := a.NewWindow("EMDB")
|
||||||
w.Resize(fyne.NewSize(800, 600))
|
w.Resize(fyne.NewSize(800, 600))
|
||||||
|
|
||||||
g := &GUI{
|
g := &GUI{
|
||||||
a: a,
|
a: a,
|
||||||
w: w,
|
w: w,
|
||||||
s: s,
|
in: in,
|
||||||
c: c,
|
out: out,
|
||||||
|
s: NewState(),
|
||||||
}
|
}
|
||||||
|
|
||||||
g.SetContent()
|
g.SetContent()
|
||||||
|
@ -35,9 +39,27 @@ func New(c chan backend.Command, s *backend.State) *GUI {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GUI) Run() {
|
func (g *GUI) Run() {
|
||||||
|
go func() {
|
||||||
|
for s := range g.in {
|
||||||
|
g.Update(s)
|
||||||
|
}
|
||||||
|
}()
|
||||||
g.w.ShowAndRun()
|
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() {
|
func (g *GUI) SetContent() {
|
||||||
label1 := widget.NewLabel("Label 1")
|
label1 := widget.NewLabel("Label 1")
|
||||||
label2 := widget.NewLabel("Label 2")
|
label2 := widget.NewLabel("Label 2")
|
||||||
|
@ -49,7 +71,7 @@ func (g *GUI) SetContent() {
|
||||||
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() {
|
||||||
g.c <- backend.Command{
|
g.out <- backend.Command{
|
||||||
Name: backend.CommandAdd,
|
Name: backend.CommandAdd,
|
||||||
Args: map[string]any{
|
Args: map[string]any{
|
||||||
backend.ArgName: input.Text,
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"code.ewintr.nl/emdb/client"
|
||||||
"code.ewintr.nl/emdb/desktop-client/backend"
|
"code.ewintr.nl/emdb/desktop-client/backend"
|
||||||
"code.ewintr.nl/emdb/desktop-client/gui"
|
"code.ewintr.nl/emdb/desktop-client/gui"
|
||||||
|
"code.ewintr.nl/emdb/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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 := gui.New(b.In(), b.Out())
|
||||||
g.Run()
|
g.Run()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue