2023-12-23 18:43:38 +01:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-12-24 11:03:32 +01:00
|
|
|
"ewintr.nl/emdb/client"
|
2023-12-23 18:43:38 +01:00
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
|
|
|
type baseModel struct {
|
2023-12-24 11:03:32 +01:00
|
|
|
emdb *client.EMDB
|
|
|
|
tmdb *client.TMDB
|
2023-12-24 13:33:15 +01:00
|
|
|
tabs *TabSet
|
2023-12-24 10:16:11 +01:00
|
|
|
initialized bool
|
2023-12-24 11:03:32 +01:00
|
|
|
logger *Logger
|
2023-12-23 18:43:38 +01:00
|
|
|
logViewport viewport.Model
|
|
|
|
windowSize tea.WindowSizeMsg
|
|
|
|
contentSize tea.WindowSizeMsg
|
|
|
|
}
|
|
|
|
|
2023-12-24 11:03:32 +01:00
|
|
|
func NewBaseModel(emdb *client.EMDB, tmdb *client.TMDB, logger *Logger) (tea.Model, tea.Cmd) {
|
2023-12-24 10:16:11 +01:00
|
|
|
logViewport := viewport.New(0, 0)
|
|
|
|
logViewport.KeyMap = viewport.KeyMap{}
|
|
|
|
|
|
|
|
m := baseModel{
|
2023-12-24 11:03:32 +01:00
|
|
|
emdb: emdb,
|
|
|
|
tmdb: tmdb,
|
2023-12-24 13:33:15 +01:00
|
|
|
tabs: NewTabSet(),
|
2023-12-24 10:16:11 +01:00
|
|
|
logViewport: logViewport,
|
2023-12-24 11:03:32 +01:00
|
|
|
logger: logger,
|
2023-12-24 10:16:11 +01:00
|
|
|
}
|
|
|
|
m.setSize()
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2023-12-23 18:43:38 +01:00
|
|
|
func (m baseModel) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m baseModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
2023-12-27 08:25:09 +01:00
|
|
|
case NextTabSelected:
|
|
|
|
m.tabs.Next()
|
|
|
|
case PrevTabSelected:
|
|
|
|
m.tabs.Previous()
|
2023-12-23 18:43:38 +01:00
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
m.windowSize = msg
|
2023-12-24 10:16:11 +01:00
|
|
|
if !m.initialized {
|
2023-12-24 13:33:15 +01:00
|
|
|
var emdbTab, tmdbTab tea.Model
|
|
|
|
emdbTab, cmd = NewTabEMDB(m.emdb, m.logger)
|
|
|
|
cmds = append(cmds, cmd)
|
2023-12-25 09:21:00 +01:00
|
|
|
tmdbTab, cmd = NewTabTMDB(m.emdb, m.tmdb, m.logger)
|
2023-12-24 10:16:11 +01:00
|
|
|
cmds = append(cmds, cmd)
|
2024-01-06 14:49:45 +01:00
|
|
|
reviewTab, cmd := NewTabReview(m.emdb, m.logger)
|
|
|
|
cmds = append(cmds, cmd)
|
2023-12-25 13:29:43 +01:00
|
|
|
m.tabs.AddTab("emdb", "Watched movies", emdbTab)
|
2024-01-06 14:49:45 +01:00
|
|
|
m.tabs.AddTab("review", "Review", reviewTab)
|
2023-12-24 13:33:15 +01:00
|
|
|
m.tabs.AddTab("tmdb", "TMDB", tmdbTab)
|
2023-12-24 10:16:11 +01:00
|
|
|
m.initialized = true
|
2023-12-23 18:43:38 +01:00
|
|
|
}
|
2023-12-24 11:03:32 +01:00
|
|
|
m.Log(fmt.Sprintf("new window size: %dx%d", msg.Width, msg.Height))
|
2023-12-23 18:43:38 +01:00
|
|
|
m.setSize()
|
2023-12-25 13:29:43 +01:00
|
|
|
tabSize := TabSizeMsg{
|
2023-12-23 18:43:38 +01:00
|
|
|
Width: m.contentSize.Width,
|
|
|
|
Height: m.contentSize.Height,
|
|
|
|
}
|
2023-12-24 13:33:15 +01:00
|
|
|
cmds = append(cmds, m.tabs.Update(tabSize))
|
2023-12-25 09:21:00 +01:00
|
|
|
case NewMovie:
|
|
|
|
m.Log(fmt.Sprintf("imported movie %s", msg.m.Title))
|
2023-12-25 13:29:43 +01:00
|
|
|
m.tabs.Select("emdb")
|
|
|
|
cmds = append(cmds, FetchMovieList(m.emdb))
|
2023-12-24 17:11:29 +01:00
|
|
|
case error:
|
|
|
|
m.Log(fmt.Sprintf("ERROR: %s", msg.Error()))
|
2023-12-24 13:33:15 +01:00
|
|
|
default:
|
|
|
|
cmds = append(cmds, m.tabs.Update(msg))
|
2023-12-23 18:43:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 11:03:32 +01:00
|
|
|
m.logViewport.SetContent(strings.Join(m.logger.Lines, "\n"))
|
2023-12-23 18:43:38 +01:00
|
|
|
m.logViewport.GotoBottom()
|
|
|
|
m.logViewport, cmd = m.logViewport.Update(msg)
|
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
2023-12-24 11:03:32 +01:00
|
|
|
func (m *baseModel) Log(msg string) {
|
|
|
|
m.logger.Log(msg)
|
|
|
|
}
|
|
|
|
|
2023-12-23 18:43:38 +01:00
|
|
|
func (m baseModel) View() string {
|
2023-12-24 10:16:11 +01:00
|
|
|
if !m.initialized {
|
2023-12-23 18:43:38 +01:00
|
|
|
return "\n Initializing..."
|
|
|
|
}
|
|
|
|
|
2023-12-24 17:11:29 +01:00
|
|
|
logWindow := windowStyle.Width(m.contentSize.Width).Height(logLineCount).Render(m.logViewport.View())
|
|
|
|
return docStyle.Render(fmt.Sprintf("%s\n%s", m.tabs.View(), logWindow))
|
2023-12-23 18:43:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *baseModel) setSize() {
|
|
|
|
logHeight := logLineCount + docStyle.GetVerticalFrameSize()
|
|
|
|
menuHeight := 1
|
|
|
|
|
|
|
|
m.contentSize.Width = m.windowSize.Width - windowStyle.GetHorizontalFrameSize() - docStyle.GetHorizontalFrameSize()
|
|
|
|
m.contentSize.Height = m.windowSize.Height - windowStyle.GetVerticalFrameSize() - docStyle.GetVerticalFrameSize() - logHeight - menuHeight
|
|
|
|
|
|
|
|
m.logViewport.Width = m.contentSize.Width
|
|
|
|
m.logViewport.Height = logLineCount
|
|
|
|
}
|