2022-12-28 22:22:36 -05:00
|
|
|
package posts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-01-03 13:19:21 -05:00
|
|
|
"strings"
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
2022-12-30 03:22:54 -05:00
|
|
|
"github.com/mrusme/gobbs/aggregator"
|
2022-12-28 22:22:36 -05:00
|
|
|
"github.com/mrusme/gobbs/models/post"
|
2023-01-02 13:25:09 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/cmd"
|
2022-12-28 22:22:36 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/ctx"
|
2023-01-02 15:33:12 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/windows/postshow"
|
2022-12-28 22:22:36 -05:00
|
|
|
)
|
|
|
|
|
2022-12-31 11:55:06 -05:00
|
|
|
var (
|
2023-01-02 15:29:29 -05:00
|
|
|
VIEW_ID = "posts"
|
2022-12-28 22:22:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type KeyMap struct {
|
2022-12-30 23:57:03 -05:00
|
|
|
Refresh key.Binding
|
|
|
|
Select key.Binding
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultKeyMap = KeyMap{
|
|
|
|
Refresh: key.NewBinding(
|
2023-01-01 20:13:02 -05:00
|
|
|
key.WithKeys("ctrl+r"),
|
|
|
|
key.WithHelp("ctrl+r", "refresh"),
|
2022-12-28 22:22:36 -05:00
|
|
|
),
|
|
|
|
Select: key.NewBinding(
|
2023-01-01 20:13:02 -05:00
|
|
|
key.WithKeys("r", "enter"),
|
|
|
|
key.WithHelp("r/enter", "read"),
|
2022-12-28 22:22:36 -05:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
type Model struct {
|
2023-01-03 13:19:21 -05:00
|
|
|
ctx *ctx.Ctx
|
|
|
|
keymap KeyMap
|
|
|
|
focused bool
|
2022-12-30 21:15:25 -05:00
|
|
|
|
2023-01-03 13:19:21 -05:00
|
|
|
list list.Model
|
|
|
|
items []list.Item
|
2023-01-01 22:01:05 -05:00
|
|
|
|
2023-01-03 13:19:21 -05:00
|
|
|
a *aggregator.Aggregator
|
2023-01-02 01:57:28 -05:00
|
|
|
|
2023-01-01 20:53:28 -05:00
|
|
|
viewcache string
|
|
|
|
viewcacheTextareaXY []int
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Init() tea.Cmd {
|
2022-12-31 21:09:43 -05:00
|
|
|
return nil
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewModel(c *ctx.Ctx) Model {
|
|
|
|
m := Model{
|
2023-01-02 13:25:09 -05:00
|
|
|
ctx: c,
|
|
|
|
keymap: DefaultKeyMap,
|
|
|
|
focused: false,
|
2023-01-01 22:01:05 -05:00
|
|
|
|
2023-01-01 20:53:28 -05:00
|
|
|
viewcache: "",
|
|
|
|
viewcacheTextareaXY: []int{0, 0, 0, 0},
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
2022-12-31 18:32:04 -05:00
|
|
|
listDelegate := list.NewDefaultDelegate()
|
|
|
|
listDelegate.Styles.NormalTitle = m.ctx.Theme.PostsList.Item.Focused
|
|
|
|
listDelegate.Styles.DimmedTitle = m.ctx.Theme.PostsList.Item.Blurred
|
|
|
|
listDelegate.Styles.SelectedTitle = m.ctx.Theme.PostsList.Item.Selected
|
|
|
|
listDelegate.Styles.NormalDesc = m.ctx.Theme.PostsList.ItemDetail.Focused
|
|
|
|
listDelegate.Styles.DimmedDesc = m.ctx.Theme.PostsList.ItemDetail.Blurred
|
|
|
|
listDelegate.Styles.SelectedDesc = m.ctx.Theme.PostsList.ItemDetail.Selected
|
|
|
|
|
|
|
|
m.list = list.New(m.items, listDelegate, 0, 0)
|
2022-12-31 16:57:11 -05:00
|
|
|
m.list.SetShowTitle(false)
|
|
|
|
m.list.SetShowStatusBar(false)
|
2022-12-31 20:58:45 -05:00
|
|
|
|
2022-12-30 03:30:03 -05:00
|
|
|
m.a, _ = aggregator.New(m.ctx)
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch {
|
2023-01-01 22:01:05 -05:00
|
|
|
|
2022-12-28 22:22:36 -05:00
|
|
|
case key.Matches(msg, m.keymap.Refresh):
|
2023-01-02 13:25:09 -05:00
|
|
|
m.ctx.Loading = true
|
|
|
|
cmds = append(cmds, m.refresh())
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
case key.Matches(msg, m.keymap.Select):
|
2023-01-02 13:25:09 -05:00
|
|
|
i, ok := m.list.SelectedItem().(post.Post)
|
|
|
|
if ok {
|
2023-01-02 18:56:00 -05:00
|
|
|
m.viewcache = m.buildView(false)
|
2023-01-02 15:33:12 -05:00
|
|
|
cmd := cmd.New(cmd.WinOpen, postshow.WIN_ID, cmd.Arg{
|
2023-01-02 13:25:09 -05:00
|
|
|
Name: "post",
|
|
|
|
Value: &i,
|
|
|
|
})
|
|
|
|
cmds = append(cmds, cmd.Tea())
|
2022-12-30 21:15:25 -05:00
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case tea.WindowSizeMsg:
|
2022-12-30 21:15:25 -05:00
|
|
|
listWidth := m.ctx.Content[0] - 2
|
2022-12-28 22:22:36 -05:00
|
|
|
listHeight := m.ctx.Content[1] - 1
|
|
|
|
|
2022-12-31 17:18:53 -05:00
|
|
|
m.ctx.Theme.PostsList.List.Focused.Width(listWidth)
|
|
|
|
m.ctx.Theme.PostsList.List.Blurred.Width(listWidth)
|
|
|
|
m.ctx.Theme.PostsList.List.Focused.Height(listHeight)
|
|
|
|
m.ctx.Theme.PostsList.List.Blurred.Height(listHeight)
|
2022-12-28 22:22:36 -05:00
|
|
|
m.list.SetSize(
|
2023-01-04 21:43:11 -05:00
|
|
|
listWidth-2+60,
|
2022-12-28 22:22:36 -05:00
|
|
|
listHeight-2,
|
|
|
|
)
|
2023-01-04 21:43:11 -05:00
|
|
|
msg.Width = listWidth + 60
|
|
|
|
msg.Height = listHeight
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2023-01-02 13:25:09 -05:00
|
|
|
case cmd.Command:
|
|
|
|
switch msg.Call {
|
|
|
|
case cmd.ViewFocus:
|
2023-01-02 15:29:29 -05:00
|
|
|
if msg.Target == VIEW_ID ||
|
|
|
|
msg.Target == "*" {
|
2023-01-03 13:19:21 -05:00
|
|
|
m.Focus()
|
2023-01-02 13:25:09 -05:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
case cmd.ViewBlur:
|
2023-01-02 15:29:29 -05:00
|
|
|
if msg.Target == VIEW_ID ||
|
|
|
|
msg.Target == "*" {
|
2023-01-03 13:19:21 -05:00
|
|
|
m.Blur()
|
2023-01-02 13:25:09 -05:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
case cmd.ViewRefreshData:
|
2023-01-02 15:29:29 -05:00
|
|
|
if msg.Target == VIEW_ID ||
|
|
|
|
msg.Target == "*" {
|
2023-01-02 13:25:09 -05:00
|
|
|
m.ctx.Loading = true
|
|
|
|
cmds = append(cmds, m.refresh())
|
|
|
|
}
|
2023-01-02 15:29:29 -05:00
|
|
|
case cmd.ViewFreshData:
|
|
|
|
if msg.Target == VIEW_ID ||
|
|
|
|
msg.Target == "*" {
|
|
|
|
m.items = msg.GetArg("items").([]list.Item)
|
|
|
|
m.list.SetItems(m.items)
|
|
|
|
m.ctx.Loading = false
|
|
|
|
return m, nil
|
|
|
|
}
|
2023-01-02 13:25:09 -05:00
|
|
|
}
|
2022-12-31 13:42:30 -05:00
|
|
|
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
2023-01-02 13:25:09 -05:00
|
|
|
var lcmd tea.Cmd
|
|
|
|
m.list, lcmd = m.list.Update(msg)
|
|
|
|
cmds = append(cmds, lcmd)
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) refresh() tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
|
|
|
var items []list.Item
|
|
|
|
|
2022-12-30 03:30:03 -05:00
|
|
|
posts, errs := m.a.ListPosts()
|
2022-12-30 03:22:54 -05:00
|
|
|
if len(errs) > 0 {
|
|
|
|
fmt.Printf("%s", errs) // TODO: Implement error message
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
for _, post := range posts {
|
|
|
|
items = append(items, post)
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:29:29 -05:00
|
|
|
c := cmd.New(
|
|
|
|
cmd.ViewFreshData,
|
|
|
|
VIEW_ID,
|
|
|
|
cmd.Arg{Name: "items", Value: items},
|
|
|
|
)
|
|
|
|
|
|
|
|
return *c
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 13:19:21 -05:00
|
|
|
func (m *Model) Focus() {
|
|
|
|
m.focused = true
|
|
|
|
m.viewcache = m.buildView(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) Blur() {
|
|
|
|
m.focused = false
|
|
|
|
m.viewcache = m.buildView(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) View() string {
|
|
|
|
return m.buildView(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) buildView(cached bool) string {
|
|
|
|
var view strings.Builder = strings.Builder{}
|
|
|
|
|
|
|
|
if cached && m.focused == false && m.viewcache != "" {
|
|
|
|
m.ctx.Logger.Debugln("Cached View()")
|
|
|
|
|
|
|
|
return m.viewcache
|
2022-12-31 13:42:30 -05:00
|
|
|
}
|
2023-01-03 13:19:21 -05:00
|
|
|
|
|
|
|
m.ctx.Logger.Debugln("Posts.View()")
|
|
|
|
var l string = ""
|
|
|
|
if m.focused {
|
|
|
|
l = m.ctx.Theme.PostsList.List.Focused.Render(m.list.View())
|
|
|
|
} else {
|
|
|
|
l = m.ctx.Theme.PostsList.List.Blurred.Render(m.list.View())
|
|
|
|
}
|
|
|
|
view.WriteString(lipgloss.JoinHorizontal(
|
|
|
|
lipgloss.Top,
|
|
|
|
l,
|
|
|
|
))
|
|
|
|
|
|
|
|
m.viewcache = view.String()
|
|
|
|
return m.viewcache
|
2022-12-31 13:42:30 -05:00
|
|
|
}
|