Skip to content

Commit

Permalink
Fixed pagination on home screen
Browse files Browse the repository at this point in the history
 and finder screen.
Added search Count repository
  • Loading branch information
grrlopes committed Aug 1, 2023
1 parent fc3e837 commit 47b3808
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
1 change: 1 addition & 0 deletions repositories/isqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type ISqliteRepository interface {
Count() (int, error)
InsertParsed(string) (int64, error)
Search(string, int, int) ([]entity.SqliteCommand, int, error)
SearchCount(string) (int, error)
}
8 changes: 8 additions & 0 deletions repositories/sqlite/sqliterepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,11 @@ func (sql *SQLiteRepository) Search(filter string, limit int, skip int) ([]entit

return data, count, nil
}

func (sql *SQLiteRepository) SearchCount(filter string) (int, error) {
var count int

sql.db.QueryRow("SELECT COUNT(*) FROM command WHERE Title LIKE ?", "%"+filter+"%").Scan(&count)

return count, nil
}
7 changes: 6 additions & 1 deletion ui/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import (
"github.com/grrlopes/storydb/entity"
)

func finderCmd(filter string, limit int, offset int ) ([]entity.SqliteCommand, int) {
func finderCmd(filter string, limit int, offset int) ([]entity.SqliteCommand, int) {
data, total, _ := usecaseFinder.Execute(filter, limit, offset)
return data, total
}

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

func finderUpdate(msg tea.Msg, m ModelHome) (*ModelHome, tea.Cmd) {
var cmd tea.Cmd

Expand Down
36 changes: 24 additions & 12 deletions ui/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/grrlopes/storydb/usecase/fhistory"
"github.com/grrlopes/storydb/usecase/fhistorytotal"
"github.com/grrlopes/storydb/usecase/finder"
findercount "github.com/grrlopes/storydb/usecase/finderCount"
"github.com/grrlopes/storydb/usecase/pager"
)

Expand All @@ -29,6 +30,7 @@ var (
usecaseHistory fhistory.InputBoundary = fhistory.NewFHistory(frepository, repository)
usecaseHistoryTotal fhistorytotal.InputBoundary = fhistorytotal.NewFHistoryTotal(frepository, repository)
usecaseFinder finder.InputBoundary = finder.NewFinder(repository)
usecaseFinderCount findercount.InputBoundary = findercount.NewFinderCount(repository)
)

type ModelHome struct {
Expand All @@ -39,13 +41,13 @@ func NewHome(m *entity.Command) *ModelHome {
count := usecaseCount.Execute()
ftotal := usecaseHistoryTotal.Execute()
p := paginator.New()
p.PerPage = 18
p.PerPage = 20
p.SetTotalPages(count)
pro := progress.New(progress.WithDefaultGradient())
txt := textinput.New()
txt.Placeholder = "type"
txt.CharLimit = 156
txt.Width = 20
txt.Width = 50
txt.Prompt = "Finder: "

home := ModelHome{
Expand Down Expand Up @@ -83,23 +85,33 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
return synced, cmd
}

// if m.home.ActiveFinderScreen {
// m.home.Ready = true
// finder, cmd := finderUpdate(msg, m)
// return finder, cmd
// }

var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
if m.home.Finder.Focused() {
switch msg.String() {
case "up", "k":
if m.home.Cursor > 0 {
m.home.Content = "arrow"
m.home.Cursor--
}
case "down", "j":
if m.home.Cursor < m.home.PageTotal-1 {
m.home.Content = "arrow"
m.home.Cursor++
}
}
if msg.String() == "ctrl+c" {
m.home.Finder.SetValue("")
m.home.Finder.Blur()
}
m.home.Store, *m.home.Count = finderCmd(m.home.Finder.Value(), 18, m.home.Start)

*m.home.Pagination, cmd = m.home.Pagination.Update(msg)
*m.home.Count = finderCount(m.home.Finder.Value())
start, end := m.updatepagination()
m.home.Start = start
m.home.End = end
m.home.Store, _ = finderCmd(m.home.Finder.Value(), 20, m.home.Start)
m.home.Finder, cmd = m.home.Finder.Update(msg)
} else {
switch msg.String() {
Expand Down Expand Up @@ -138,8 +150,8 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
m.home.Ready = true
}

*m.home.Pagination, cmd = m.home.Pagination.Update(msg)
if !m.home.Finder.Focused() {
*m.home.Pagination, cmd = m.home.Pagination.Update(msg)
start, end := m.updatepagination()
m.home.Start = start
m.home.End = end
Expand Down Expand Up @@ -178,9 +190,9 @@ func (m *ModelHome) GetDataView() string {
)

if !m.home.Finder.Focused() {
m.home.Store, _ = usecasePager.Execute(18, m.home.Start)
m.home.Store, _ = usecasePager.Execute(20, m.home.Start)
m.home.PageTotal = len(m.home.Store)
}
m.home.PageTotal = len(m.home.Store)
m.home.Pagination.SetTotalPages(*m.home.Count)
var (
result []string
Expand Down
30 changes: 30 additions & 0 deletions usecase/finderCount/searchcount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package findercount

import (
"log"

"github.com/grrlopes/storydb/repositories"
)

type InputBoundary interface {
Execute(string) int
}

type execute struct {
repository repositories.ISqliteRepository
}

func NewFinderCount(repo repositories.ISqliteRepository) InputBoundary {
return execute{
repository: repo,
}
}

func (e execute) Execute(filter string) int {
count, err := e.repository.SearchCount(filter)
if err != nil {
log.Fatal("Search count:", err)
}

return count
}

0 comments on commit 47b3808

Please sign in to comment.