fix for copy urls, update to playback bar

This commit is contained in:
abs3nt 2023-01-30 09:13:23 -08:00
parent 6e9d50d5ca
commit b8c736ee11
3 changed files with 138 additions and 96 deletions

View File

@ -245,7 +245,7 @@ func PlayLikedSongs(ctx *gctx.Context, client *spotify.Client, position int) err
fmt.Println(err.Error()) fmt.Println(err.Error())
return err return err
} }
playlist, err := GetRadioPlaylist(ctx, client) playlist, err := GetRadioPlaylist(ctx, client, "Saved Songs")
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return err return err
@ -303,9 +303,9 @@ func PlayLikedSongs(ctx *gctx.Context, client *spotify.Client, position int) err
return err return err
} }
func RadioGivenArtist(ctx *gctx.Context, client *spotify.Client, artist_id spotify.ID) error { func RadioGivenArtist(ctx *gctx.Context, client *spotify.Client, artist spotify.SimpleArtist) error {
seed := spotify.Seeds{ seed := spotify.Seeds{
Artists: []spotify.ID{artist_id}, Artists: []spotify.ID{artist.ID},
} }
recomendations, err := client.GetRecommendations(ctx, seed, &spotify.TrackAttributes{}, spotify.Limit(100)) recomendations, err := client.GetRecommendations(ctx, seed, &spotify.TrackAttributes{}, spotify.Limit(100))
if err != nil { if err != nil {
@ -319,7 +319,7 @@ func RadioGivenArtist(ctx *gctx.Context, client *spotify.Client, artist_id spoti
if err != nil { if err != nil {
return err return err
} }
radioPlaylist, err := GetRadioPlaylist(ctx, client) radioPlaylist, err := GetRadioPlaylist(ctx, client, artist.Name)
if err != nil { if err != nil {
return err return err
} }
@ -364,10 +364,10 @@ func RadioGivenArtist(ctx *gctx.Context, client *spotify.Client, artist_id spoti
return nil return nil
} }
func RadioGivenSong(ctx *gctx.Context, client *spotify.Client, song_id spotify.ID, pos int) error { func RadioGivenSong(ctx *gctx.Context, client *spotify.Client, song spotify.SimpleTrack, pos int) error {
start := time.Now().UnixMilli() start := time.Now().UnixMilli()
seed := spotify.Seeds{ seed := spotify.Seeds{
Tracks: []spotify.ID{song_id}, Tracks: []spotify.ID{song.ID},
} }
recomendations, err := client.GetRecommendations(ctx, seed, &spotify.TrackAttributes{}, spotify.Limit(99)) recomendations, err := client.GetRecommendations(ctx, seed, &spotify.TrackAttributes{}, spotify.Limit(99))
if err != nil { if err != nil {
@ -381,13 +381,13 @@ func RadioGivenSong(ctx *gctx.Context, client *spotify.Client, song_id spotify.I
if err != nil { if err != nil {
return err return err
} }
radioPlaylist, err := GetRadioPlaylist(ctx, client) radioPlaylist, err := GetRadioPlaylist(ctx, client, song.Name)
if err != nil { if err != nil {
return err return err
} }
queue := []spotify.ID{song_id} queue := []spotify.ID{song.ID}
all_recs := map[spotify.ID]bool{} all_recs := map[spotify.ID]bool{}
all_recs[song_id] = true all_recs[song.ID] = true
for _, rec := range recomendationIds { for _, rec := range recomendationIds {
all_recs[rec] = true all_recs[rec] = true
queue = append(queue, rec) queue = append(queue, rec)
@ -482,7 +482,7 @@ func Radio(ctx *gctx.Context, client *spotify.Client) error {
seed_song = tracks.Tracks[rand.Intn(len(tracks.Tracks))].SimpleTrack seed_song = tracks.Tracks[rand.Intn(len(tracks.Tracks))].SimpleTrack
} }
} }
return RadioGivenSong(ctx, client, seed_song.ID, current_song.Progress) return RadioGivenSong(ctx, client, seed_song, current_song.Progress)
} }
func RefillRadio(ctx *gctx.Context, client *spotify.Client) error { func RefillRadio(ctx *gctx.Context, client *spotify.Client) error {
@ -494,7 +494,7 @@ func RefillRadio(ctx *gctx.Context, client *spotify.Client) error {
return nil return nil
} }
to_remove := []spotify.ID{} to_remove := []spotify.ID{}
radioPlaylist, err := GetRadioPlaylist(ctx, client) radioPlaylist, err := GetRadioPlaylist(ctx, client, "")
if status.PlaybackContext.URI != radioPlaylist.URI { if status.PlaybackContext.URI != radioPlaylist.URI {
return nil return nil
} }
@ -621,7 +621,7 @@ func RefillRadio(ctx *gctx.Context, client *spotify.Client) error {
} }
func ClearRadio(ctx *gctx.Context, client *spotify.Client) error { func ClearRadio(ctx *gctx.Context, client *spotify.Client) error {
radioPlaylist, err := GetRadioPlaylist(ctx, client) radioPlaylist, err := GetRadioPlaylist(ctx, client, "")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return err return err
@ -938,12 +938,12 @@ func RadioFromPlaylist(ctx *gctx.Context, client *spotify.Client, playlist spoti
} }
seedIds = append(seedIds, song.Track.Track.ID) seedIds = append(seedIds, song.Track.Track.ID)
} }
return RadioGivenList(ctx, client, seedIds[:seedCount]) return RadioGivenList(ctx, client, seedIds[:seedCount], playlist.Name)
} }
func RadioFromAlbum(ctx *gctx.Context, client *spotify.Client, album spotify.ID) error { func RadioFromAlbum(ctx *gctx.Context, client *spotify.Client, album spotify.SimpleAlbum) error {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
tracks, err := AlbumTracks(ctx, client, album, 1) tracks, err := AlbumTracks(ctx, client, album.ID, 1)
if err != nil { if err != nil {
return err return err
} }
@ -956,7 +956,7 @@ func RadioFromAlbum(ctx *gctx.Context, client *spotify.Client, album spotify.ID)
if pages > 1 { if pages > 1 {
randomPage = rand.Intn(int(pages-1)) + 1 randomPage = rand.Intn(int(pages-1)) + 1
} }
albumTrackPage, err := AlbumTracks(ctx, client, album, randomPage) albumTrackPage, err := AlbumTracks(ctx, client, album.ID, randomPage)
if err != nil { if err != nil {
return err return err
} }
@ -973,7 +973,7 @@ func RadioFromAlbum(ctx *gctx.Context, client *spotify.Client, album spotify.ID)
} }
seedIds = append(seedIds, song.ID) seedIds = append(seedIds, song.ID)
} }
return RadioGivenList(ctx, client, seedIds[:seedCount]) return RadioGivenList(ctx, client, seedIds[:seedCount], album.Name)
} }
func RadioFromSavedTracks(ctx *gctx.Context, client *spotify.Client) error { func RadioFromSavedTracks(ctx *gctx.Context, client *spotify.Client) error {
@ -1005,10 +1005,10 @@ func RadioFromSavedTracks(ctx *gctx.Context, client *spotify.Client) error {
seedIds = append(seedIds, song.ID) seedIds = append(seedIds, song.ID)
} }
seedIds = append(seedIds, savedSongs.Tracks[0].ID) seedIds = append(seedIds, savedSongs.Tracks[0].ID)
return RadioGivenList(ctx, client, seedIds) return RadioGivenList(ctx, client, seedIds, "Saved Tracks")
} }
func RadioGivenList(ctx *gctx.Context, client *spotify.Client, song_ids []spotify.ID) error { func RadioGivenList(ctx *gctx.Context, client *spotify.Client, song_ids []spotify.ID, name string) error {
seed := spotify.Seeds{ seed := spotify.Seeds{
Tracks: song_ids, Tracks: song_ids,
} }
@ -1024,7 +1024,7 @@ func RadioGivenList(ctx *gctx.Context, client *spotify.Client, song_ids []spotif
if err != nil { if err != nil {
return err return err
} }
radioPlaylist, err := GetRadioPlaylist(ctx, client) radioPlaylist, err := GetRadioPlaylist(ctx, client, name)
if err != nil { if err != nil {
return err return err
} }
@ -1138,11 +1138,11 @@ func getDefaultDevice(ctx *gctx.Context, client *spotify.Client) (spotify.ID, er
} }
} }
func GetRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullPlaylist, error) { func GetRadioPlaylist(ctx *gctx.Context, client *spotify.Client, name string) (*spotify.FullPlaylist, error) {
configDir, _ := os.UserConfigDir() configDir, _ := os.UserConfigDir()
playlistFile, err := os.ReadFile(filepath.Join(configDir, "gospt/radio.json")) playlistFile, err := os.ReadFile(filepath.Join(configDir, "gospt/radio.json"))
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
return CreateRadioPlaylist(ctx, client) return CreateRadioPlaylist(ctx, client, name)
} }
if err != nil { if err != nil {
return nil, err return nil, err
@ -1155,10 +1155,10 @@ func GetRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullP
return playlist, nil return playlist, nil
} }
func CreateRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullPlaylist, error) { func CreateRadioPlaylist(ctx *gctx.Context, client *spotify.Client, name string) (*spotify.FullPlaylist, error) {
// private flag doesnt work // private flag doesnt work
configDir, _ := os.UserConfigDir() configDir, _ := os.UserConfigDir()
playlist, err := client.CreatePlaylistForUser(ctx, ctx.UserId, "autoradio", "Automanaged radio playlist", false, false) playlist, err := client.CreatePlaylistForUser(ctx, ctx.UserId, name+" - autoradio", "Automanaged radio playlist", false, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -17,15 +17,15 @@ func HandlePlayWithContext(ctx *gctx.Context, client *spotify.Client, uri *spoti
} }
} }
func HandleRadio(ctx *gctx.Context, client *spotify.Client, id spotify.ID) { func HandleRadio(ctx *gctx.Context, client *spotify.Client, song spotify.SimpleTrack) {
err := commands.RadioGivenSong(ctx, client, id, 0) err := commands.RadioGivenSong(ctx, client, song, 0)
if err != nil { if err != nil {
return return
} }
} }
func HandleAlbumRadio(ctx *gctx.Context, client *spotify.Client, id spotify.ID) { func HandleAlbumRadio(ctx *gctx.Context, client *spotify.Client, album spotify.SimpleAlbum) {
err := commands.RadioFromAlbum(ctx, client, id) err := commands.RadioFromAlbum(ctx, client, album)
if err != nil { if err != nil {
return return
} }
@ -49,15 +49,15 @@ func HandleVolume(ctx *gctx.Context, client *spotify.Client, up bool) {
} }
} }
func HandleArtistRadio(ctx *gctx.Context, client *spotify.Client, id spotify.ID) { func HandleArtistRadio(ctx *gctx.Context, client *spotify.Client, artist spotify.SimpleArtist) {
err := commands.RadioGivenArtist(ctx, client, id) err := commands.RadioGivenArtist(ctx, client, artist)
if err != nil { if err != nil {
return return
} }
} }
func HandleAlbumArtist(ctx *gctx.Context, client *spotify.Client, id spotify.ID) { func HandleAlbumArtist(ctx *gctx.Context, client *spotify.Client, artist spotify.SimpleArtist) {
err := commands.RadioGivenArtist(ctx, client, id) err := commands.RadioGivenArtist(ctx, client, artist)
if err != nil { if err != nil {
return return
} }

View File

@ -2,6 +2,7 @@ package tui
import ( import (
"fmt" "fmt"
"strings"
"time" "time"
"gospt/src/gctx" "gospt/src/gctx"
@ -19,7 +20,8 @@ import (
var ( var (
P *tea.Program P *tea.Program
DocStyle = lipgloss.NewStyle().Margin(0, 2) DocStyle = lipgloss.NewStyle().Margin(0, 2)
currentlyPlaying string currentlyPlaying *spotify.CurrentlyPlaying
playbackContext string
main_updates chan *mainModel main_updates chan *mainModel
page = 1 page = 1
) )
@ -80,12 +82,12 @@ type mainModel struct {
searchResults *SearchResults searchResults *SearchResults
progress progress.Model progress progress.Model
playing *spotify.CurrentlyPlaying playing *spotify.CurrentlyPlaying
playbackContext string
search string search string
} }
func (m *mainModel) PlayRadio() { func (m *mainModel) PlayRadio() {
currentlyPlaying = m.list.SelectedItem().(mainItem).Title() m.list.NewStatusMessage("Starting radio for " + m.list.SelectedItem().(mainItem).Title())
m.list.NewStatusMessage("Starting radio for " + currentlyPlaying)
selectedItem := m.list.SelectedItem().(mainItem).SpotifyItem selectedItem := m.list.SelectedItem().(mainItem).SpotifyItem
switch selectedItem.(type) { switch selectedItem.(type) {
case spotify.SimplePlaylist: case spotify.SimplePlaylist:
@ -95,19 +97,28 @@ func (m *mainModel) PlayRadio() {
go HandleLibraryRadio(m.ctx, m.client) go HandleLibraryRadio(m.ctx, m.client)
return return
case spotify.SimpleAlbum: case spotify.SimpleAlbum:
go HandleAlbumRadio(m.ctx, m.client, selectedItem.(spotify.SimpleAlbum).ID) go HandleAlbumRadio(m.ctx, m.client, selectedItem.(spotify.SimpleAlbum))
return return
case spotify.FullAlbum: case spotify.FullAlbum:
go HandleAlbumRadio(m.ctx, m.client, selectedItem.(spotify.FullAlbum).ID) go HandleAlbumRadio(m.ctx, m.client, selectedItem.(spotify.FullAlbum).SimpleAlbum)
return return
case spotify.SimpleArtist: case spotify.SimpleArtist:
go HandleArtistRadio(m.ctx, m.client, selectedItem.(spotify.SimpleArtist).ID) go HandleArtistRadio(m.ctx, m.client, selectedItem.(spotify.SimpleArtist))
return return
case spotify.FullArtist: case spotify.FullArtist:
go HandleArtistRadio(m.ctx, m.client, selectedItem.(spotify.FullArtist).ID) go HandleArtistRadio(m.ctx, m.client, selectedItem.(spotify.FullArtist).SimpleArtist)
return return
default: case spotify.SimpleTrack:
go HandleRadio(m.ctx, m.client, m.list.SelectedItem().(mainItem).ID) go HandleRadio(m.ctx, m.client, selectedItem.(spotify.SimpleTrack))
return
case spotify.FullTrack:
go HandleRadio(m.ctx, m.client, selectedItem.(spotify.FullTrack).SimpleTrack)
return
case spotify.PlaylistTrack:
go HandleRadio(m.ctx, m.client, selectedItem.(spotify.PlaylistTrack).Track.SimpleTrack)
return
case spotify.SavedTrack:
go HandleRadio(m.ctx, m.client, selectedItem.(spotify.SavedTrack).SimpleTrack)
return return
} }
} }
@ -118,7 +129,6 @@ func (m *mainModel) GoBack() (tea.Cmd, error) {
return tea.Quit, nil return tea.Quit, nil
case Albums, Artists, Tracks, Playlist, Devices, Search: case Albums, Artists, Tracks, Playlist, Devices, Search:
m.mode = Main m.mode = Main
m.list.NewStatusMessage("Setting view to main")
new_items, err := MainView(m.ctx, m.client) new_items, err := MainView(m.ctx, m.client)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
@ -126,7 +136,6 @@ func (m *mainModel) GoBack() (tea.Cmd, error) {
m.list.SetItems(new_items) m.list.SetItems(new_items)
case Album: case Album:
m.mode = Albums m.mode = Albums
m.list.NewStatusMessage("Setting view to albums")
new_items, err := AlbumsView(m.ctx, m.client) new_items, err := AlbumsView(m.ctx, m.client)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
@ -135,7 +144,6 @@ func (m *mainModel) GoBack() (tea.Cmd, error) {
m.list.SetItems(new_items) m.list.SetItems(new_items)
case Artist: case Artist:
m.mode = Artists m.mode = Artists
m.list.NewStatusMessage("Setting view to artists")
new_items, err := ArtistsView(m.ctx, m.client) new_items, err := ArtistsView(m.ctx, m.client)
if err != nil { if err != nil {
return nil, err return nil, err
@ -143,7 +151,6 @@ func (m *mainModel) GoBack() (tea.Cmd, error) {
m.list.SetItems(new_items) m.list.SetItems(new_items)
case ArtistAlbum: case ArtistAlbum:
m.mode = Artist m.mode = Artist
m.list.NewStatusMessage("Opening " + m.artist.Name)
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client) new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client)
if err != nil { if err != nil {
return nil, err return nil, err
@ -151,7 +158,6 @@ func (m *mainModel) GoBack() (tea.Cmd, error) {
m.list.SetItems(new_items) m.list.SetItems(new_items)
case SearchArtists, SearchTracks, SearchAlbums, SearchPlaylists: case SearchArtists, SearchTracks, SearchAlbums, SearchPlaylists:
m.mode = Search m.mode = Search
m.list.NewStatusMessage("Setting view to search for " + m.input.Value())
items, result, err := SearchView(m.ctx, m.client, m.search) items, result, err := SearchView(m.ctx, m.client, m.search)
if err != nil { if err != nil {
return nil, err return nil, err
@ -160,7 +166,6 @@ func (m *mainModel) GoBack() (tea.Cmd, error) {
m.list.SetItems(items) m.list.SetItems(items)
case SearchArtist: case SearchArtist:
m.mode = SearchArtists m.mode = SearchArtists
m.list.NewStatusMessage("Setting view to artists")
new_items, err := SearchArtistsView(m.ctx, m.client, m.searchResults.Artists) new_items, err := SearchArtistsView(m.ctx, m.client, m.searchResults.Artists)
if err != nil { if err != nil {
return nil, err return nil, err
@ -168,7 +173,6 @@ func (m *mainModel) GoBack() (tea.Cmd, error) {
m.list.SetItems(new_items) m.list.SetItems(new_items)
case SearchArtistAlbum: case SearchArtistAlbum:
m.mode = SearchArtist m.mode = SearchArtist
m.list.NewStatusMessage("Opening " + m.artist.Name)
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client) new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client)
if err != nil { if err != nil {
return nil, err return nil, err
@ -176,7 +180,6 @@ func (m *mainModel) GoBack() (tea.Cmd, error) {
m.list.SetItems(new_items) m.list.SetItems(new_items)
case SearchAlbum: case SearchAlbum:
m.mode = SearchAlbums m.mode = SearchAlbums
m.list.NewStatusMessage("Setting view to albums")
new_items, err := SearchAlbumsView(m.ctx, m.client, m.searchResults.Albums) new_items, err := SearchAlbumsView(m.ctx, m.client, m.searchResults.Albums)
if err != nil { if err != nil {
return nil, err return nil, err
@ -184,7 +187,6 @@ func (m *mainModel) GoBack() (tea.Cmd, error) {
m.list.SetItems(new_items) m.list.SetItems(new_items)
case SearchPlaylist: case SearchPlaylist:
m.mode = SearchPlaylists m.mode = SearchPlaylists
m.list.NewStatusMessage("Setting view to playlists")
new_items, err := SearchPlaylistsView(m.ctx, m.client, m.searchResults.Playlists) new_items, err := SearchPlaylistsView(m.ctx, m.client, m.searchResults.Playlists)
if err != nil { if err != nil {
return nil, err return nil, err
@ -202,28 +204,37 @@ type SpotifyUrl struct {
func (m *mainModel) CopyToClipboard() error { func (m *mainModel) CopyToClipboard() error {
item := m.list.SelectedItem().(mainItem).SpotifyItem item := m.list.SelectedItem().(mainItem).SpotifyItem
m.list.NewStatusMessage("Copying link to " + item.(mainItem).Title())
switch item.(type) { switch item.(type) {
case spotify.SimplePlaylist: case spotify.SimplePlaylist:
clipboard.WriteAll(item.(spotify.SimplePlaylist).ExternalURLs["spotify"]) clipboard.WriteAll(item.(spotify.SimplePlaylist).ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
case *spotify.FullPlaylist: case *spotify.FullPlaylist:
clipboard.WriteAll(item.(*spotify.FullPlaylist).ExternalURLs["spotify"]) clipboard.WriteAll(item.(*spotify.FullPlaylist).ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
case spotify.SimpleAlbum: case spotify.SimpleAlbum:
clipboard.WriteAll(item.(spotify.SimpleAlbum).ExternalURLs["spotify"]) clipboard.WriteAll(item.(spotify.SimpleAlbum).ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
case *spotify.FullAlbum: case *spotify.FullAlbum:
clipboard.WriteAll(item.(*spotify.FullAlbum).ExternalURLs["spotify"]) clipboard.WriteAll(item.(*spotify.FullAlbum).ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
case spotify.SimpleArtist: case spotify.SimpleArtist:
clipboard.WriteAll(item.(spotify.SimpleArtist).ExternalURLs["spotify"]) clipboard.WriteAll(item.(spotify.SimpleArtist).ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
case *spotify.FullArtist: case *spotify.FullArtist:
clipboard.WriteAll(item.(*spotify.FullArtist).ExternalURLs["spotify"]) clipboard.WriteAll(item.(*spotify.FullArtist).ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
case spotify.SimpleTrack: case spotify.SimpleTrack:
clipboard.WriteAll(item.(spotify.SimpleTrack).ExternalURLs["spotify"]) clipboard.WriteAll(item.(spotify.SimpleTrack).ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
case spotify.PlaylistTrack: case spotify.PlaylistTrack:
clipboard.WriteAll(item.(spotify.PlaylistTrack).Track.ExternalURLs["spotify"]) clipboard.WriteAll(item.(spotify.PlaylistTrack).Track.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
case spotify.SavedTrack: case spotify.SavedTrack:
clipboard.WriteAll(item.(spotify.SavedTrack).ExternalURLs["spotify"]) clipboard.WriteAll(item.(spotify.SavedTrack).ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
case spotify.FullTrack: case spotify.FullTrack:
clipboard.WriteAll(item.(spotify.FullTrack).ExternalURLs["spotify"]) clipboard.WriteAll(item.(spotify.FullTrack).ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
} }
return nil return nil
} }
@ -234,7 +245,6 @@ func (m *mainModel) SelectItem() error {
switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) { switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) {
case *spotify.FullArtistPage: case *spotify.FullArtistPage:
m.mode = SearchArtists m.mode = SearchArtists
m.list.NewStatusMessage("Setting view to artists")
new_items, err := SearchArtistsView(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.FullArtistPage)) new_items, err := SearchArtistsView(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.FullArtistPage))
if err != nil { if err != nil {
return err return err
@ -243,7 +253,6 @@ func (m *mainModel) SelectItem() error {
m.list.ResetSelected() m.list.ResetSelected()
case *spotify.SimpleAlbumPage: case *spotify.SimpleAlbumPage:
m.mode = SearchAlbums m.mode = SearchAlbums
m.list.NewStatusMessage("Setting view to albums")
new_items, err := SearchAlbumsView(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.SimpleAlbumPage)) new_items, err := SearchAlbumsView(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.SimpleAlbumPage))
if err != nil { if err != nil {
return err return err
@ -253,7 +262,6 @@ func (m *mainModel) SelectItem() error {
case *spotify.SimplePlaylistPage: case *spotify.SimplePlaylistPage:
m.mode = SearchPlaylists m.mode = SearchPlaylists
playlists := m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.SimplePlaylistPage) playlists := m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.SimplePlaylistPage)
m.list.NewStatusMessage("Setting view to playlist")
new_items, err := SearchPlaylistsView(m.ctx, m.client, playlists) new_items, err := SearchPlaylistsView(m.ctx, m.client, playlists)
if err != nil { if err != nil {
return err return err
@ -262,19 +270,16 @@ func (m *mainModel) SelectItem() error {
m.list.ResetSelected() m.list.ResetSelected()
case *spotify.FullTrackPage: case *spotify.FullTrackPage:
m.mode = SearchTracks m.mode = SearchTracks
m.list.NewStatusMessage("Setting view to tracks")
new_items, err := SearchTracksView(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.FullTrackPage)) new_items, err := SearchTracksView(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.FullTrackPage))
if err != nil { if err != nil {
return err return err
} }
m.list.SetItems(new_items) m.list.SetItems(new_items)
m.list.ResetSelected() m.list.ResetSelected()
m.list.NewStatusMessage("Setting view to tracks")
} }
case SearchArtists: case SearchArtists:
m.mode = SearchArtist m.mode = SearchArtist
m.artist = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleArtist) m.artist = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleArtist)
m.list.NewStatusMessage("Opening " + m.artist.Name)
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client) new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client)
if err != nil { if err != nil {
return err return err
@ -284,7 +289,6 @@ func (m *mainModel) SelectItem() error {
case SearchArtist: case SearchArtist:
m.mode = SearchArtistAlbum m.mode = SearchArtistAlbum
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum) m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
m.list.NewStatusMessage("Opening " + m.album.Name)
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client) new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client)
if err != nil { if err != nil {
return err return err
@ -294,7 +298,6 @@ func (m *mainModel) SelectItem() error {
case SearchAlbums: case SearchAlbums:
m.mode = SearchAlbum m.mode = SearchAlbum
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum) m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
m.list.NewStatusMessage("Opening " + m.album.Name)
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client) new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client)
if err != nil { if err != nil {
return err return err
@ -305,7 +308,6 @@ func (m *mainModel) SelectItem() error {
m.mode = SearchPlaylist m.mode = SearchPlaylist
playlist := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist) playlist := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist)
m.playlist = playlist m.playlist = playlist
m.list.NewStatusMessage("Setting view to playlist " + playlist.Name)
new_items, err := PlaylistView(m.ctx, m.client, playlist) new_items, err := PlaylistView(m.ctx, m.client, playlist)
if err != nil { if err != nil {
return err return err
@ -316,7 +318,6 @@ func (m *mainModel) SelectItem() error {
switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) { switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) {
case *spotify.FullArtistCursorPage: case *spotify.FullArtistCursorPage:
m.mode = Artists m.mode = Artists
m.list.NewStatusMessage("Setting view to artists")
new_items, err := ArtistsView(m.ctx, m.client) new_items, err := ArtistsView(m.ctx, m.client)
if err != nil { if err != nil {
return err return err
@ -325,7 +326,6 @@ func (m *mainModel) SelectItem() error {
m.list.ResetSelected() m.list.ResetSelected()
case *spotify.SavedAlbumPage: case *spotify.SavedAlbumPage:
m.mode = Albums m.mode = Albums
m.list.NewStatusMessage("Setting view to albums")
new_items, err := AlbumsView(m.ctx, m.client) new_items, err := AlbumsView(m.ctx, m.client)
if err != nil { if err != nil {
return err return err
@ -336,7 +336,6 @@ func (m *mainModel) SelectItem() error {
m.mode = Playlist m.mode = Playlist
playlist := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist) playlist := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist)
m.playlist = playlist m.playlist = playlist
m.list.NewStatusMessage("Setting view to playlist " + playlist.Name)
new_items, err := PlaylistView(m.ctx, m.client, playlist) new_items, err := PlaylistView(m.ctx, m.client, playlist)
if err != nil { if err != nil {
return err return err
@ -345,19 +344,16 @@ func (m *mainModel) SelectItem() error {
m.list.ResetSelected() m.list.ResetSelected()
case *spotify.SavedTrackPage: case *spotify.SavedTrackPage:
m.mode = Tracks m.mode = Tracks
m.list.NewStatusMessage("Setting view to saved tracks")
new_items, err := SavedTracksView(m.ctx, m.client) new_items, err := SavedTracksView(m.ctx, m.client)
if err != nil { if err != nil {
return err return err
} }
m.list.SetItems(new_items) m.list.SetItems(new_items)
m.list.ResetSelected() m.list.ResetSelected()
m.list.NewStatusMessage("Setting view to tracks")
} }
case Albums: case Albums:
m.mode = Album m.mode = Album
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum) m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
m.list.NewStatusMessage("Opening " + m.album.Name)
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client) new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client)
if err != nil { if err != nil {
return err return err
@ -367,7 +363,6 @@ func (m *mainModel) SelectItem() error {
case Artist: case Artist:
m.mode = ArtistAlbum m.mode = ArtistAlbum
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum) m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
m.list.NewStatusMessage("Opening " + m.album.Name)
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client) new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client)
if err != nil { if err != nil {
return err return err
@ -377,7 +372,6 @@ func (m *mainModel) SelectItem() error {
case Artists: case Artists:
m.mode = Artist m.mode = Artist
m.artist = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleArtist) m.artist = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleArtist)
m.list.NewStatusMessage("Opening " + m.artist.Name)
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client) new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client)
if err != nil { if err != nil {
return err return err
@ -385,20 +379,12 @@ func (m *mainModel) SelectItem() error {
m.list.SetItems(new_items) m.list.SetItems(new_items)
m.list.ResetSelected() m.list.ResetSelected()
case Album, ArtistAlbum, SearchArtistAlbum, SearchAlbum: case Album, ArtistAlbum, SearchArtistAlbum, SearchAlbum:
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Playing " + currentlyPlaying)
go HandlePlayWithContext(m.ctx, m.client, &m.album.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.TotalPages)) go HandlePlayWithContext(m.ctx, m.client, &m.album.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.TotalPages))
case Playlist, SearchPlaylist: case Playlist, SearchPlaylist:
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Playing " + currentlyPlaying)
go HandlePlayWithContext(m.ctx, m.client, &m.playlist.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage)) go HandlePlayWithContext(m.ctx, m.client, &m.playlist.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
case Tracks: case Tracks:
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Playing " + currentlyPlaying)
go HandlePlayLikedSong(m.ctx, m.client, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage)) go HandlePlayLikedSong(m.ctx, m.client, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
case SearchTracks: case SearchTracks:
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Playing " + currentlyPlaying)
go HandlePlayTrack(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.FullTrack).ID) go HandlePlayTrack(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.FullTrack).ID)
case Devices: case Devices:
go HandleSetDevice(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.PlayerDevice)) go HandleSetDevice(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.PlayerDevice))
@ -427,6 +413,31 @@ func Tick() tea.Cmd {
}) })
} }
func (m *mainModel) TickPlayback() {
playing, _ := m.client.PlayerCurrentlyPlaying(m.ctx)
if playing != nil && playing.Playing && playing.Item != nil {
currentlyPlaying = playing
playbackContext, _ = m.getContext(playing)
}
ticker := time.NewTicker(1 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
playing, _ := m.client.PlayerCurrentlyPlaying(m.ctx)
if playing != nil && playing.Playing && playing.Item != nil {
currentlyPlaying = playing
playbackContext, _ = m.getContext(playing)
}
case <-quit:
ticker.Stop()
return
}
}
}()
}
func (m mainModel) View() string { func (m mainModel) View() string {
if m.input.Focused() { if m.input.Focused() {
return DocStyle.Render(m.list.View() + "\n" + m.input.View()) return DocStyle.Render(m.list.View() + "\n" + m.input.View())
@ -436,7 +447,6 @@ func (m mainModel) View() string {
func (m *mainModel) Typing(msg tea.KeyMsg) (bool, tea.Cmd) { func (m *mainModel) Typing(msg tea.KeyMsg) (bool, tea.Cmd) {
if msg.String() == "enter" { if msg.String() == "enter" {
m.list.NewStatusMessage("Setting view to search for " + m.input.Value())
items, result, err := SearchView(m.ctx, m.client, m.input.Value()) items, result, err := SearchView(m.ctx, m.client, m.input.Value())
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
@ -459,9 +469,33 @@ func (m *mainModel) Typing(msg tea.KeyMsg) (bool, tea.Cmd) {
return false, nil return false, nil
} }
func (m *mainModel) getContext(playing *spotify.CurrentlyPlaying) (string, error) {
context := playing.PlaybackContext
id := strings.Split(string(context.URI), ":")[2]
switch context.Type {
case "album":
album, err := m.client.GetAlbum(m.ctx, spotify.ID(id))
if err != nil {
return "", err
}
return album.Name, nil
case "playlist":
playlist, err := m.client.GetPlaylist(m.ctx, spotify.ID(id))
if err != nil {
return "", err
}
return playlist.Name, nil
case "artist":
artist, err := m.client.GetArtist(m.ctx, spotify.ID(id))
if err != nil {
return "", err
}
return artist.Name, nil
}
return "", nil
}
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Update Now Playing
m.list.NewStatusMessage(currentlyPlaying)
// Update list items from LoadMore // Update list items from LoadMore
select { select {
case update := <-main_updates: case update := <-main_updates:
@ -478,20 +512,27 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Handle user input // Handle user input
switch msg := msg.(type) { switch msg := msg.(type) {
case tickMsg: case tickMsg:
playing, err := m.client.PlayerCurrentlyPlaying(m.ctx) playing := currentlyPlaying
if err != nil {
return nil, nil
}
if playing != nil && playing.Playing && playing.Item != nil { if playing != nil && playing.Playing && playing.Item != nil {
cmd := m.progress.SetPercent(float64(playing.Progress) / float64(playing.Item.Duration)) cmd := m.progress.SetPercent(float64(playing.Progress) / float64(playing.Item.Duration))
m.playing = playing m.playing = playing
m.playbackContext = playbackContext
return m, tea.Batch(Tick(), cmd) return m, tea.Batch(Tick(), cmd)
} }
return m, Tick() return m, Tick()
case progress.FrameMsg: case progress.FrameMsg:
progressModel, cmd := m.progress.Update(msg) progressModel, cmd := m.progress.Update(msg)
m.progress = progressModel.(progress.Model) m.progress = progressModel.(progress.Model)
currentlyPlaying = fmt.Sprintf("Now playing %s by %s - %s %s/%s ", m.playing.Item.Name, m.playing.Item.Artists[0].Name, m.progress.View(), (time.Duration(m.playing.Progress) * time.Millisecond).Round(time.Second), (time.Duration(m.playing.Item.Duration) * time.Millisecond).Round(time.Second)) m.list.NewStatusMessage(
fmt.Sprintf("Now playing %s by %s - %s %s/%s : %s",
m.playing.Item.Name,
m.playing.Item.Artists[0].Name,
m.progress.View(),
(time.Duration(m.playing.Progress) * time.Millisecond).Round(time.Second),
(time.Duration(m.playing.Item.Duration) * time.Millisecond).Round(time.Second),
m.playbackContext),
)
return m, cmd return m, cmd
case tea.KeyMsg: case tea.KeyMsg:
// quit // quit
@ -613,6 +654,7 @@ func InitMain(ctx *gctx.Context, client *spotify.Client, mode Mode) (tea.Model,
progress: prog, progress: prog,
} }
m.list.Title = "GOSPT" m.list.Title = "GOSPT"
go m.TickPlayback()
Tick() Tick()
m.list.DisableQuitKeybindings() m.list.DisableQuitKeybindings()
m.list.AdditionalShortHelpKeys = func() []key.Binding { m.list.AdditionalShortHelpKeys = func() []key.Binding {