diff --git a/internal/tui/list.go b/internal/tui/list.go index d066d44..cab7e28 100644 --- a/internal/tui/list.go +++ b/internal/tui/list.go @@ -15,7 +15,10 @@ import ( "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 { Name string @@ -39,30 +42,41 @@ type model struct { } func (m model) Init() tea.Cmd { + track_updates = make(chan *model) return nil } +func (m *model) LoadMoreItems() { + tracks, err := commands.TrackList(m.ctx, m.client, (m.page + 1)) + if err != nil { + return + } + m.page++ + items := []list.Item{} + for _, track := range tracks.Tracks { + items = append(items, item{ + Name: track.Name, + Artist: track.Artists[0], + Duration: track.TimeDuration().Round(time.Second).String(), + ID: track.ID, + }) + } + for _, item := range items { + m.list.InsertItem(len(m.list.Items())+1, item) + } + track_updates <- m +} + func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - if m.list.Paginator.OnLastPage() { + 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 { - tracks, err := commands.TrackList(m.ctx, m.client, (m.page + 1)) - if err != nil { - return m, tea.Quit - } - m.page++ - items := []list.Item{} - for _, track := range tracks.Tracks { - items = append(items, item{ - Name: track.Name, - Artist: track.Artists[0], - Duration: track.TimeDuration().Round(time.Second).String(), - ID: track.ID, - }) - } - for _, item := range items { - m.list.InsertItem(len(m.list.Items())+1, item) - } + go m.LoadMoreItems() } } switch msg := msg.(type) { diff --git a/internal/tui/main.go b/internal/tui/main.go index 0cbc269..1a1a75d 100644 --- a/internal/tui/main.go +++ b/internal/tui/main.go @@ -12,6 +12,8 @@ import ( "github.com/zmb3/spotify/v2" ) +var main_updates chan *mainModel + type mainItem struct { Name string Desc string @@ -33,26 +35,36 @@ func (m mainModel) Init() tea.Cmd { return nil } +func (m *mainModel) LoadMoreItems() { + playlists, err := commands.Playlists(m.ctx, m.client, (m.page + 1)) + if err != nil { + return + } + m.page++ + items := []list.Item{} + for _, playlist := range playlists.Playlists { + items = append(items, mainItem{ + Name: playlist.Name, + Desc: playlist.Description, + SpotifyItem: playlist, + }) + } + for _, item := range items { + m.list.InsertItem(len(m.list.Items())+1, item) + } + main_updates <- m +} + func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - if m.list.Paginator.OnLastPage() { - // if the last request was not full + 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 { - playlists, err := commands.Playlists(m.ctx, m.client, (m.page + 1)) - if err != nil { - return m, tea.Quit - } - m.page++ - items := []list.Item{} - for _, playlist := range playlists.Playlists { - items = append(items, mainItem{ - Name: playlist.Name, - Desc: playlist.Description, - SpotifyItem: playlist, - }) - } - for _, item := range items { - m.list.InsertItem(len(m.list.Items())+1, item) - } + go m.LoadMoreItems() } } switch msg := msg.(type) { diff --git a/internal/tui/playlisttracks.go b/internal/tui/playlisttracks.go index 9984e86..8ddc5b7 100644 --- a/internal/tui/playlisttracks.go +++ b/internal/tui/playlisttracks.go @@ -14,6 +14,8 @@ import ( "github.com/zmb3/spotify/v2" ) +var list_updates chan *playlistTracksModel + type track struct { Name string Duration string @@ -37,31 +39,20 @@ type playlistTracksModel struct { } func (m playlistTracksModel) Init() tea.Cmd { + list_updates = make(chan *playlistTracksModel) return nil } 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 len(m.list.Items())%50 == 0 { - tracks, err := commands.PlaylistTracks(m.ctx, m.client, m.playlist.ID, (m.page + 1)) - 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) - } - + go m.LoadMoreItems() } } switch msg := msg.(type) { @@ -115,6 +106,27 @@ func (m playlistTracksModel) View() string { 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 { items := []list.Item{} tracks, err := commands.PlaylistTracks(ctx, client, playlist.ID, 1)