fully commit to event-driven arch

This commit is contained in:
David 2021-07-28 21:36:45 -04:00
parent 4f4758f7fe
commit 90a9facc44
2 changed files with 136 additions and 44 deletions

89
cmd/manage/events.go Normal file
View File

@ -0,0 +1,89 @@
package main
import (
"git.yetaga.in/alazyreader/library/book"
"github.com/gdamore/tcell"
)
// save change to 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
}
// open new book in display
type EventLoadBook struct {
tcell.EventTime
ID int
}
func NewEventLoadBook(id int) *EventLoadBook {
e := &EventLoadBook{ID: id}
e.SetEventNow()
return e
}
// open new book in display
type EventEnterBook struct {
tcell.EventTime
}
func NewEventEnterBook() *EventEnterBook {
e := &EventEnterBook{}
e.SetEventNow()
return e
}
// switch back to menu control
type EventExitBook struct {
tcell.EventTime
}
func NewEventExitBook() *EventExitBook {
e := &EventExitBook{}
e.SetEventNow()
return e
}
// open import window
type EventOpenImport struct {
tcell.EventTime
}
func NewEventOpenImport() *EventOpenImport {
e := &EventOpenImport{}
e.SetEventNow()
return e
}
// close import window
type EventCloseImport struct {
tcell.EventTime
}
func NewEventCloseImport() *EventCloseImport {
e := &EventCloseImport{}
e.SetEventNow()
return e
}
// quit
type EventQuit struct {
tcell.EventTime
}
func NewEventQuit() *EventQuit {
e := &EventQuit{}
e.SetEventNow()
return e
}

View File

@ -46,37 +46,13 @@ func (s *State) Set(key string, value interface{}) {
s.stateMap[key] = value
}
// UI states
const (
IN_MENU = iota
IN_BOOK
IN_IMPORT
)
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)
@ -131,6 +107,7 @@ func main() {
}
}()
// book list and options menu (left column)
l := ui.NewList(Titles(state.Get("library").([]book.Book)), 0)
menu := ui.NewBox(
"library",
@ -145,6 +122,8 @@ func main() {
book := ui.NewBookDetails(&book.Book{
Title: "test title",
})
// book display (right column)
activeBook := ui.NewBox(
"book",
nil,
@ -156,6 +135,10 @@ func main() {
false,
)
// pop-up
popup := ui.NewBox("import", nil, nil, ui.StyleActive, false)
popup.SetVisible(false)
container := ui.NewContainer(
ui.Contents{
{Container: menu, Offsets: ui.Offsets{Percent: 1}},
@ -193,42 +176,62 @@ func main() {
l.SetSelected(l.Selected() + 1)
screen.PostEvent(NewEventLoadBook(l.SelectedID()))
}
if v.Rune() == 'q' {
screen.Fini()
fmt.Printf("Thank you for playing Wing Commander!\n\n")
return
}
if v.Key() == tcell.KeyEnter {
activeBook.SetStyle(ui.StyleActive)
menu.SetStyle(ui.StyleInactive)
state.Set("ui_state", IN_BOOK)
screen.PostEvent(NewEventEnterBook())
}
if v.Rune() == 'i' {
screen.PostEvent(NewEventOpenImport())
}
if v.Rune() == 'q' {
screen.PostEvent(NewEventQuit())
}
} else if curr == IN_BOOK {
if v.Key() == tcell.KeyEsc {
activeBook.SetStyle(ui.StyleInactive)
menu.SetStyle(ui.StyleActive)
state.Set("ui_state", IN_MENU)
screen.PostEvent(NewEventExitBook())
}
} else if curr == IN_IMPORT {
if v.Key() == tcell.KeyEsc {
screen.PostEvent(NewEventCloseImport())
}
}
screen.Clear()
container.Draw(screen)
case *tcell.EventResize: // screen redraw
w, h := screen.Size()
container.SetSize(0, 0, h, w)
screen.Clear()
container.Draw(screen)
case *EventBookUpdate:
// TK
case *EventEnterBook:
activeBook.SetStyle(ui.StyleActive)
menu.SetStyle(ui.StyleInactive)
state.Set("ui_state", IN_BOOK)
case *EventExitBook:
state.Set("ui_state", IN_MENU)
activeBook.SetStyle(ui.StyleInactive)
menu.SetStyle(ui.StyleActive)
case *EventLoadBook:
book.SetBook(GetBookByID(v.ID, books))
screen.Clear()
container.Draw(screen)
case *EventOpenImport:
state.Set("ui_state", IN_IMPORT)
menu.SetStyle(ui.StyleInactive)
popup.SetVisible(true)
popup.SetSize(6, 3, 10, 80)
case *EventCloseImport:
state.Set("ui_state", IN_MENU)
menu.SetStyle(ui.StyleActive)
popup.SetVisible(false)
case *EventQuit:
screen.Fini()
fmt.Printf("Thank you for playing Wing Commander!\n\n")
return
case *tcell.EventInterrupt:
case *tcell.EventMouse:
case *tcell.EventTime:
default:
}
screen.Show() // repaint
// repaint
screen.Clear()
container.Draw(screen)
popup.Draw(screen)
screen.Show()
}
}