2023-12-24 12:55:38 +01:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
2023-12-24 13:54:02 +01:00
|
|
|
"fmt"
|
|
|
|
|
2023-12-24 12:55:38 +01:00
|
|
|
"ewintr.nl/emdb/client"
|
2024-01-20 13:49:19 +01:00
|
|
|
"ewintr.nl/emdb/cmd/api-service/moviestore"
|
2023-12-24 12:55:38 +01:00
|
|
|
"github.com/charmbracelet/bubbles/list"
|
2023-12-24 13:54:02 +01:00
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
2023-12-24 12:55:38 +01:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tabTMDB struct {
|
2023-12-25 09:21:00 +01:00
|
|
|
emdb *client.EMDB
|
|
|
|
tmdb *client.TMDB
|
2023-12-24 13:54:02 +01:00
|
|
|
initialized bool
|
|
|
|
focused string
|
|
|
|
searchInput textinput.Model
|
|
|
|
searchResults list.Model
|
|
|
|
logger *Logger
|
2023-12-24 12:55:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-25 09:21:00 +01:00
|
|
|
func NewTabTMDB(emdb *client.EMDB, tmdb *client.TMDB, logger *Logger) (tea.Model, tea.Cmd) {
|
2023-12-24 12:55:38 +01:00
|
|
|
m := tabTMDB{
|
2023-12-25 09:21:00 +01:00
|
|
|
emdb: emdb,
|
2023-12-24 12:55:38 +01:00
|
|
|
tmdb: tmdb,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m tabTMDB) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m tabTMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
2023-12-25 13:29:43 +01:00
|
|
|
case TabSizeMsg:
|
2023-12-24 13:54:02 +01:00
|
|
|
if !m.initialized {
|
2024-01-14 12:07:25 +01:00
|
|
|
m.initialModel(msg.Width, msg.Height-2)
|
2023-12-24 13:54:02 +01:00
|
|
|
}
|
|
|
|
m.initialized = true
|
2024-01-14 12:07:25 +01:00
|
|
|
m.searchResults.SetSize(msg.Width, msg.Height-2)
|
2023-12-25 13:29:43 +01:00
|
|
|
case TabResetMsg:
|
|
|
|
m.searchInput.SetValue("")
|
|
|
|
m.searchResults.SetItems([]list.Item{})
|
|
|
|
m.searchInput.Focus()
|
|
|
|
m.focused = "search"
|
2023-12-24 12:55:38 +01:00
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
2023-12-28 15:55:18 +01:00
|
|
|
case "ctrl+c", "esc":
|
2023-12-27 08:25:09 +01:00
|
|
|
return m, tea.Quit
|
2023-12-28 15:55:18 +01:00
|
|
|
case "q":
|
|
|
|
if m.focused == "result" {
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
2023-12-27 08:25:09 +01:00
|
|
|
case "right", "tab":
|
2023-12-28 15:55:18 +01:00
|
|
|
cmds = append(cmds, SelectNextTab(), m.ResetCmd())
|
2023-12-27 08:25:09 +01:00
|
|
|
case "left", "shift+tab":
|
2023-12-28 15:55:18 +01:00
|
|
|
cmds = append(cmds, SelectPrevTab(), m.ResetCmd())
|
2023-12-24 17:11:29 +01:00
|
|
|
case "enter":
|
2024-01-06 13:00:29 +01:00
|
|
|
if m.focused == "search" {
|
2023-12-25 13:29:43 +01:00
|
|
|
cmds = append(cmds, m.SearchTMDBCmd(m.searchInput.Value()))
|
2023-12-24 17:11:29 +01:00
|
|
|
m.searchInput.Blur()
|
|
|
|
m.Log("search tmdb...")
|
2024-01-06 13:00:29 +01:00
|
|
|
}
|
|
|
|
case "i":
|
|
|
|
if m.focused == "result" {
|
2023-12-25 09:21:00 +01:00
|
|
|
movie := m.searchResults.SelectedItem().(Movie)
|
2023-12-25 13:29:43 +01:00
|
|
|
cmds = append(cmds, m.ImportMovieCmd(movie), m.ResetCmd())
|
2024-01-06 13:00:29 +01:00
|
|
|
m.Log(fmt.Sprintf("imported movie %s", movie.Title()))
|
2023-12-24 17:11:29 +01:00
|
|
|
}
|
2023-12-24 12:55:38 +01:00
|
|
|
}
|
2023-12-24 17:11:29 +01:00
|
|
|
case Movies:
|
|
|
|
m.Log(fmt.Sprintf("found %d movies in in tmdb", len(msg)))
|
|
|
|
m.searchResults.SetItems(msg.listItems())
|
|
|
|
m.focused = "result"
|
2023-12-24 12:55:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 13:54:02 +01:00
|
|
|
switch m.focused {
|
|
|
|
case "search":
|
|
|
|
m.searchInput, cmd = m.searchInput.Update(msg)
|
|
|
|
case "result":
|
|
|
|
m.searchResults, cmd = m.searchResults.Update(msg)
|
|
|
|
}
|
2023-12-24 12:55:38 +01:00
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m tabTMDB) View() string {
|
2023-12-24 13:54:02 +01:00
|
|
|
return fmt.Sprintf("%s\n%s\n", m.searchInput.View(), m.searchResults.View())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *tabTMDB) Log(s string) {
|
|
|
|
m.logger.Log(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *tabTMDB) initialModel(width, height int) {
|
|
|
|
si := textinput.New()
|
|
|
|
si.Placeholder = "title"
|
|
|
|
si.CharLimit = 156
|
|
|
|
si.Width = 20
|
|
|
|
m.searchInput = si
|
|
|
|
m.searchInput.Focus()
|
|
|
|
|
2024-01-14 12:07:25 +01:00
|
|
|
m.searchResults = list.New([]list.Item{}, list.NewDefaultDelegate(), width, height-1)
|
2023-12-24 13:54:02 +01:00
|
|
|
m.searchResults.Title = "Search results"
|
|
|
|
m.searchResults.SetShowHelp(false)
|
|
|
|
|
|
|
|
m.focused = "search"
|
2023-12-24 12:55:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-25 13:29:43 +01:00
|
|
|
func (m *tabTMDB) SearchTMDBCmd(query string) tea.Cmd {
|
2023-12-24 17:11:29 +01:00
|
|
|
return func() tea.Msg {
|
2023-12-25 13:29:43 +01:00
|
|
|
tms, err := m.tmdb.Search(query)
|
2023-12-24 17:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return Movies(tms)
|
|
|
|
}
|
|
|
|
}
|
2023-12-25 09:21:00 +01:00
|
|
|
|
2023-12-25 13:29:43 +01:00
|
|
|
func (m *tabTMDB) ImportMovieCmd(movie Movie) tea.Cmd {
|
2023-12-25 09:21:00 +01:00
|
|
|
return func() tea.Msg {
|
2023-12-26 13:19:23 +01:00
|
|
|
newMovie, err := m.emdb.CreateMovie(movie.m)
|
2023-12-25 09:21:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-20 13:49:19 +01:00
|
|
|
if err := m.emdb.CreateJob(newMovie.ID, string(moviestore.ActionRefreshIMDBReviews)); err != nil {
|
2024-01-18 08:57:56 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-12-25 09:21:00 +01:00
|
|
|
|
|
|
|
return NewMovie(Movie{m: newMovie})
|
|
|
|
}
|
|
|
|
}
|
2023-12-25 13:29:43 +01:00
|
|
|
|
|
|
|
func (m *tabTMDB) ResetCmd() tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
|
|
|
return TabResetMsg("tmdb")
|
|
|
|
}
|
|
|
|
}
|