2023-12-23 18:43:38 +01:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
2023-12-24 11:06:04 +01:00
|
|
|
"fmt"
|
|
|
|
|
2023-12-24 11:03:32 +01:00
|
|
|
"ewintr.nl/emdb/client"
|
2023-12-23 18:43:38 +01:00
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
2023-12-24 12:55:38 +01:00
|
|
|
type tabEMDB struct {
|
2023-12-24 10:16:11 +01:00
|
|
|
initialized bool
|
|
|
|
list list.Model
|
2023-12-24 11:03:32 +01:00
|
|
|
emdb *client.EMDB
|
|
|
|
logger *Logger
|
2023-12-23 18:43:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 12:55:38 +01:00
|
|
|
func NewTabEMDB(emdb *client.EMDB, logger *Logger) (tea.Model, tea.Cmd) {
|
2023-12-24 10:16:11 +01:00
|
|
|
list := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
|
|
|
|
list.Title = "Movies"
|
|
|
|
list.SetShowHelp(false)
|
|
|
|
|
2023-12-24 12:55:38 +01:00
|
|
|
m := tabEMDB{
|
2023-12-24 11:03:32 +01:00
|
|
|
emdb: emdb,
|
|
|
|
logger: logger,
|
|
|
|
list: list,
|
2023-12-24 10:16:11 +01:00
|
|
|
}
|
2023-12-23 18:43:38 +01:00
|
|
|
|
2023-12-24 17:11:29 +01:00
|
|
|
logger.Log("search emdb...")
|
|
|
|
return m, FetchMovieList(emdb)
|
2023-12-23 18:43:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 12:55:38 +01:00
|
|
|
func (m tabEMDB) Init() tea.Cmd {
|
2023-12-23 18:43:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-24 12:55:38 +01:00
|
|
|
func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2023-12-23 18:43:38 +01:00
|
|
|
var cmd tea.Cmd
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case TabSizeMsgType:
|
2023-12-24 10:16:11 +01:00
|
|
|
if !m.initialized {
|
|
|
|
m.initialized = true
|
2023-12-23 18:43:38 +01:00
|
|
|
}
|
|
|
|
m.list.SetSize(msg.Width, msg.Height)
|
2023-12-24 10:16:11 +01:00
|
|
|
case Movies:
|
2023-12-24 17:11:29 +01:00
|
|
|
m.logger.Log(fmt.Sprintf("found %d movies in in emdb", len(msg)))
|
2023-12-24 10:16:11 +01:00
|
|
|
m.list.SetItems(msg.listItems())
|
2023-12-23 18:43:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m.list, cmd = m.list.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
2023-12-24 12:55:38 +01:00
|
|
|
func (m tabEMDB) View() string {
|
2023-12-23 18:43:38 +01:00
|
|
|
return m.list.View()
|
|
|
|
}
|
2023-12-24 11:03:32 +01:00
|
|
|
|
2023-12-24 12:55:38 +01:00
|
|
|
func (m *tabEMDB) Log(s string) {
|
2023-12-24 11:03:32 +01:00
|
|
|
m.logger.Log(s)
|
|
|
|
}
|
2023-12-24 11:06:04 +01:00
|
|
|
|
2023-12-24 17:11:29 +01:00
|
|
|
func FetchMovieList(emdb *client.EMDB) tea.Cmd {
|
2023-12-24 11:06:04 +01:00
|
|
|
return func() tea.Msg {
|
|
|
|
ems, err := emdb.GetMovies()
|
|
|
|
if err != nil {
|
2023-12-24 17:11:29 +01:00
|
|
|
return err
|
2023-12-24 11:06:04 +01:00
|
|
|
}
|
|
|
|
return Movies(ems)
|
|
|
|
}
|
|
|
|
}
|