load list items in the background

This commit is contained in:
abs3nt 2023-01-10 20:46:15 -08:00
parent b3b33733b0
commit 43491a2209
3 changed files with 94 additions and 56 deletions

View File

@ -15,7 +15,10 @@ import (
"github.com/zmb3/spotify/v2" "github.com/zmb3/spotify/v2"
) )
var docStyle = lipgloss.NewStyle().Margin(1, 2) var (
track_updates chan *model
docStyle = lipgloss.NewStyle().Margin(1, 2)
)
type item struct { type item struct {
Name string Name string
@ -39,16 +42,14 @@ type model struct {
} }
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
track_updates = make(chan *model)
return nil return nil
} }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m *model) LoadMoreItems() {
if m.list.Paginator.OnLastPage() {
// if last request was still full request more
if len(m.list.Items())%50 == 0 {
tracks, err := commands.TrackList(m.ctx, m.client, (m.page + 1)) tracks, err := commands.TrackList(m.ctx, m.client, (m.page + 1))
if err != nil { if err != nil {
return m, tea.Quit return
} }
m.page++ m.page++
items := []list.Item{} items := []list.Item{}
@ -63,6 +64,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
for _, item := range items { for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item) m.list.InsertItem(len(m.list.Items())+1, item)
} }
track_updates <- m
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
select {
case msg := <-track_updates:
m.list.SetItems(msg.list.Items())
default:
}
if m.list.Paginator.Page == m.list.Paginator.TotalPages-2 {
// if last request was still full request more
if len(m.list.Items())%50 == 0 {
go m.LoadMoreItems()
} }
} }
switch msg := msg.(type) { switch msg := msg.(type) {

View File

@ -12,6 +12,8 @@ import (
"github.com/zmb3/spotify/v2" "github.com/zmb3/spotify/v2"
) )
var main_updates chan *mainModel
type mainItem struct { type mainItem struct {
Name string Name string
Desc string Desc string
@ -33,13 +35,10 @@ func (m mainModel) Init() tea.Cmd {
return nil return nil
} }
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m *mainModel) LoadMoreItems() {
if m.list.Paginator.OnLastPage() {
// if the last request was not full
if len(m.list.Items())%50 == 0 {
playlists, err := commands.Playlists(m.ctx, m.client, (m.page + 1)) playlists, err := commands.Playlists(m.ctx, m.client, (m.page + 1))
if err != nil { if err != nil {
return m, tea.Quit return
} }
m.page++ m.page++
items := []list.Item{} items := []list.Item{}
@ -53,6 +52,19 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
for _, item := range items { for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item) m.list.InsertItem(len(m.list.Items())+1, item)
} }
main_updates <- m
}
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
select {
case msg := <-main_updates:
m.list.SetItems(msg.list.Items())
default:
}
if m.list.Paginator.Page == m.list.Paginator.TotalPages-2 {
// if last request was still full request more
if len(m.list.Items())%50 == 0 {
go m.LoadMoreItems()
} }
} }
switch msg := msg.(type) { switch msg := msg.(type) {

View File

@ -14,6 +14,8 @@ import (
"github.com/zmb3/spotify/v2" "github.com/zmb3/spotify/v2"
) )
var list_updates chan *playlistTracksModel
type track struct { type track struct {
Name string Name string
Duration string Duration string
@ -37,31 +39,20 @@ type playlistTracksModel struct {
} }
func (m playlistTracksModel) Init() tea.Cmd { func (m playlistTracksModel) Init() tea.Cmd {
list_updates = make(chan *playlistTracksModel)
return nil return nil
} }
func (m playlistTracksModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m playlistTracksModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.list.Paginator.OnLastPage() { select {
case msg := <-list_updates:
m.list.SetItems(msg.list.Items())
default:
}
if m.list.Paginator.Page == m.list.Paginator.TotalPages-2 {
// if last request was still full request more // if last request was still full request more
if len(m.list.Items())%50 == 0 { if len(m.list.Items())%50 == 0 {
tracks, err := commands.PlaylistTracks(m.ctx, m.client, m.playlist.ID, (m.page + 1)) go m.LoadMoreItems()
if err != nil {
return m, tea.Quit
}
m.page++
items := []list.Item{}
for _, track := range tracks.Tracks {
items = append(items, item{
Name: track.Track.Name,
Artist: track.Track.Artists[0],
Duration: track.Track.TimeDuration().Round(time.Second).String(),
ID: track.Track.ID,
})
}
for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item)
}
} }
} }
switch msg := msg.(type) { switch msg := msg.(type) {
@ -115,6 +106,27 @@ func (m playlistTracksModel) View() string {
return docStyle.Render(m.list.View()) return docStyle.Render(m.list.View())
} }
func (m *playlistTracksModel) LoadMoreItems() {
tracks, err := commands.PlaylistTracks(m.ctx, m.client, m.playlist.ID, (m.page + 1))
if err != nil {
return
}
m.page++
items := []list.Item{}
for _, track := range tracks.Tracks {
items = append(items, item{
Name: track.Track.Name,
Artist: track.Track.Artists[0],
Duration: track.Track.TimeDuration().Round(time.Second).String(),
ID: track.Track.ID,
})
}
for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item)
}
list_updates <- m
}
func PlaylistTracks(ctx *gctx.Context, client *spotify.Client, playlist spotify.SimplePlaylist) error { func PlaylistTracks(ctx *gctx.Context, client *spotify.Client, playlist spotify.SimplePlaylist) error {
items := []list.Item{} items := []list.Item{}
tracks, err := commands.PlaylistTracks(ctx, client, playlist.ID, 1) tracks, err := commands.PlaylistTracks(ctx, client, playlist.ID, 1)