it now displays books from the DB!

This commit is contained in:
2021-07-06 21:40:12 -04:00
parent 3bff06aad7
commit 8c6c9325da
3 changed files with 224 additions and 27 deletions

View File

@@ -1,26 +1,113 @@
package main
import (
"context"
"fmt"
"log"
"os"
"sync"
"git.yetaga.in/alazyreader/library/book"
"git.yetaga.in/alazyreader/library/config"
"git.yetaga.in/alazyreader/library/database"
"git.yetaga.in/alazyreader/library/ui"
"github.com/gdamore/tcell"
"github.com/kelseyhightower/envconfig"
)
// State holds the UI state keys=>value map and manages access to the map with a mutex
type State struct {
m sync.Mutex
stateMap map[string]interface{}
}
// key, present
func (s *State) Get(key string) interface{} {
s.m.Lock()
defer s.m.Unlock()
if s.stateMap == nil {
s.stateMap = make(map[string]interface{})
}
k, ok := s.stateMap[key]
if !ok {
return nil
}
return k
}
// key, value
func (s *State) Set(key string, value interface{}) {
s.m.Lock()
defer s.m.Unlock()
if s.stateMap == nil {
s.stateMap = make(map[string]interface{})
}
s.stateMap[key] = value
}
const (
IN_MENU = iota
IN_BOOK
)
type EventBookUpdate struct {
tcell.EventTime
book *book.Book
}
func NewEventBookUpdate(b *book.Book) *EventBookUpdate {
e := &EventBookUpdate{book: b}
e.SetEventNow()
return e
}
func (e *EventBookUpdate) Book() *book.Book {
return e.book
}
type EventLoadBook struct {
tcell.EventTime
ID int
}
func NewEventLoadBook(id int) *EventLoadBook {
e := &EventLoadBook{ID: id}
e.SetEventNow()
return e
}
func main() {
var c config.Config
err := envconfig.Process("library", &c)
if err != nil {
log.Fatalln(err)
}
// create state
state := State{}
// set up DB connection
if c.DBUser == "" || c.DBPass == "" || c.DBHost == "" || c.DBPort == "" || c.DBName == "" {
log.Fatalf("vars: %+v", c)
}
lib, err := database.NewMySQLConnection(c.DBUser, c.DBPass, c.DBHost, c.DBPort, c.DBName)
if err != nil {
log.Fatalln(err)
}
err = lib.PrepareDatabase(context.Background())
if err != nil {
log.Fatalln(err)
}
_, _, err = lib.RunMigrations(context.Background())
if err != nil {
log.Fatalln(err)
}
books, err := lib.GetAllBooks(context.Background())
if err != nil {
log.Fatalln(err)
}
state.Set("library", books)
screen, err := tcell.NewScreen()
if err != nil {
log.Fatalln(err)
@@ -30,21 +117,23 @@ func main() {
log.Fatalln(err)
}
l := ui.NewList([]string{"foo", "bar", "baz"}, 0)
l := ui.NewList(Titles(state.Get("library").([]book.Book)), 0)
menu := ui.NewBox(
"library",
[]string{"(q)uit"},
[]string{"(n)ew", "(i)mport", "(q)uit"},
ui.Contents{{
Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2},
Container: l,
}},
)
book := ui.NewBookDetails()
book := ui.NewBookDetails(&book.Book{
Title: "test title",
})
activeBook := ui.NewBox(
"book",
[]string{"test"},
nil,
ui.Contents{{
Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2},
Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: 0, Right: 0},
Container: book,
}},
)
@@ -64,16 +153,19 @@ func main() {
container.Draw(screen)
screen.Sync()
state := IN_MENU
// init UI state
state.Set("ui_state", IN_MENU)
// UI loop
for {
e := screen.PollEvent()
switch v := e.(type) {
case *tcell.EventError:
fmt.Fprintf(os.Stderr, "%v", v)
screen.Beep()
case *tcell.EventKey: // input handling
if state == IN_MENU {
curr := state.Get("ui_state").(int)
if curr == IN_MENU {
if v.Key() == tcell.KeyUp && l.Selected() > 0 {
l.SetSelected(l.Selected() - 1)
}
@@ -86,11 +178,12 @@ func main() {
return
}
if v.Key() == tcell.KeyRight {
state = IN_BOOK
screen.PostEvent(NewEventLoadBook(l.SelectedID()))
state.Set("ui_state", IN_BOOK)
}
} else if state == IN_BOOK {
} else if curr == IN_BOOK {
if v.Key() == tcell.KeyLeft {
state = IN_MENU
state.Set("ui_state", IN_MENU)
}
}
screen.Clear()
@@ -100,6 +193,12 @@ func main() {
container.SetSize(0, 0, h, w)
screen.Clear()
container.Draw(screen)
case *EventBookUpdate:
// TK
case *EventLoadBook:
book.SetBook(GetBookByID(v.ID, books))
screen.Clear()
container.Draw(screen)
case *tcell.EventInterrupt:
case *tcell.EventMouse:
case *tcell.EventTime:
@@ -108,3 +207,23 @@ func main() {
screen.Show() // repaint
}
}
func Titles(lb []book.Book) []ui.ListKeyValue {
r := []ui.ListKeyValue{}
for i := range lb {
r = append(r, ui.ListKeyValue{
Key: lb[i].ID,
Value: lb[i].Title,
})
}
return r
}
func GetBookByID(id int, lb []book.Book) *book.Book {
for i := range lb {
if lb[i].ID == id {
return &lb[i]
}
}
return &book.Book{}
}