65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package pages
|
|
|
|
import (
|
|
"tasksquire/common"
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type MainPage struct {
|
|
common *common.Common
|
|
activePage common.Component
|
|
|
|
taskPage common.Component
|
|
timePage common.Component
|
|
}
|
|
|
|
func NewMainPage(common *common.Common) *MainPage {
|
|
m := &MainPage{
|
|
common: common,
|
|
}
|
|
|
|
m.taskPage = NewReportPage(common, common.TW.GetReport(common.TW.GetConfig().Get("uda.tasksquire.report.default")))
|
|
m.timePage = NewTimePage(common)
|
|
|
|
m.activePage = m.taskPage
|
|
|
|
return m
|
|
}
|
|
|
|
func (m *MainPage) Init() tea.Cmd {
|
|
return tea.Batch(m.taskPage.Init(), m.timePage.Init())
|
|
}
|
|
|
|
func (m *MainPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
var cmd tea.Cmd
|
|
|
|
switch msg := msg.(type) {
|
|
case tea.WindowSizeMsg:
|
|
m.common.SetSize(msg.Width, msg.Height)
|
|
case tea.KeyMsg:
|
|
if key.Matches(msg, m.common.Keymap.Next) {
|
|
if m.activePage == m.taskPage {
|
|
m.activePage = m.timePage
|
|
} else {
|
|
m.activePage = m.taskPage
|
|
}
|
|
// Re-size the new active page just in case
|
|
m.activePage.SetSize(m.common.Width(), m.common.Height())
|
|
// Trigger a refresh/init on switch? Maybe not needed if we keep state.
|
|
// But we might want to refresh data.
|
|
return m, m.activePage.Init()
|
|
}
|
|
}
|
|
|
|
activePage, cmd := m.activePage.Update(msg)
|
|
m.activePage = activePage.(common.Component)
|
|
|
|
return m, cmd
|
|
}
|
|
|
|
func (m *MainPage) View() string {
|
|
return m.activePage.View()
|
|
}
|