Skip to content

Commit

Permalink
Fixed finder screen pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
grrlopes committed Aug 14, 2023
1 parent f6948d7 commit b4532fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
26 changes: 21 additions & 5 deletions ui/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,34 @@ import (
"strconv"
"strings"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/grrlopes/storydb/entity"
)

func finderCmd(filter string, limit int, offset int) ([]entity.Commands, int) {
data, total, _ := usecaseFinder.Execute(filter, limit, offset)
return data, total
type (
finderMsg []entity.Commands
finderCountMsg int
)

func finderCmd(filter textinput.Model, limit int, offset int) tea.Cmd {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
cmd = func() tea.Msg {
data, _, _ := usecaseFinder.Execute(filter.Value(), limit, offset)
return finderMsg(data)
}
cmds = append(cmds, cmd)
return tea.Batch(cmds...)
}

func finderCount(filter string) int {
func finderCount(filter string) tea.Cmd {
count := usecaseFinderCount.Execute(filter)
return count
return func() tea.Msg {
return finderCountMsg(count)
}
}

func finderUpdate(msg tea.Msg, m ModelHome) (*ModelHome, tea.Cmd) {
Expand Down
23 changes: 17 additions & 6 deletions ui/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
}

switch msg := msg.(type) {
case finderCountMsg:
if *m.home.Count != int(msg) {
m.home.Pagination.Page = 0
}
if int(msg) == 0 {
m.home.Pagination.SetTotalPages(1)
}
*m.home.Count = int(msg)
case finderMsg:
m.home.Store = msg
case tea.KeyMsg:
if m.home.Finder.Focused() {
switch msg.String() {
Expand All @@ -108,15 +118,15 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
m.home.Content = "arrow"
m.home.Cursor--
}
case "down", "j", "tab":
case "down", "j", "tab":
if m.home.Cursor < m.home.PageTotal-1 {
m.home.Content = "arrow"
m.home.Cursor++
}
case "enter":
return &m, tea.Quit
case "ctrl+r":
m.home.Finder.Reset()
case "ctrl+r":
m.home.Finder.Reset()
}
if msg.String() == "ctrl+c" {
m.home.Finder.Reset()
Expand All @@ -125,9 +135,10 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
*m.home.Pagination, cmd = m.home.Pagination.Update(msg)
cmds = append(cmds, cmd)
m.home.Finder, cmd = m.home.Finder.Update(msg)
*m.home.Count = finderCount(m.home.Finder.Value())
cmd = finderCount(m.home.Finder.Value())
cmds = append(cmds, cmd)
m.home.Start, m.home.End = m.updatepagination()
m.home.Store, _ = finderCmd(m.home.Finder.Value(), m.home.Viewport.Height-2, m.home.Start)
cmd = finderCmd(m.home.Finder, m.home.Viewport.Height-2, m.home.Start)
cmds = append(cmds, cmd)
} else {
switch msg.String() {
Expand All @@ -138,7 +149,7 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
m.home.Content = "arrow"
m.home.Cursor--
}
case "down", "j", "tab":
case "down", "j", "tab":
if m.home.Cursor < m.home.PageTotal-1 {
m.home.Content = "arrow"
m.home.Cursor++
Expand Down

0 comments on commit b4532fc

Please sign in to comment.