Implement TopTracksView

This commit is contained in:
aditya-K2
2023-04-14 01:05:31 +05:30
parent b99f9180a7
commit 1a4c61ea5d
4 changed files with 97 additions and 9 deletions

View File

@@ -217,3 +217,13 @@ func RecentlyPlayed() ([]spotify.RecentlyPlayedItem, error) {
func GetPlayerState() (*spotify.PlayerState, error) { func GetPlayerState() (*spotify.PlayerState, error) {
return Client.PlayerState(ctx()) return Client.PlayerState(ctx())
} }
func GetTopTracks() ([]spotify.FullTrack, error) {
c, err := Client.CurrentUsersTopTracks(ctx(), spotify.Limit(10))
return c.Tracks, err
}
func GetTopArtists() ([]spotify.FullArtist, error) {
c, err := Client.CurrentUsersTopArtists(ctx(), spotify.Limit(10))
return c.Artists, err
}

View File

@@ -18,13 +18,14 @@ var (
) )
var ( var (
TrackStyle = tcell.StyleDefault.Foreground(tcell.ColorBlue).Background(tcell.ColorBlack) TrackStyle = tcell.StyleDefault.Foreground(tcell.ColorBlue).Background(tcell.ColorBlack)
AlbumStyle = tcell.StyleDefault.Foreground(tcell.ColorGreen).Background(tcell.ColorBlack) AlbumStyle = tcell.StyleDefault.Foreground(tcell.ColorGreen).Background(tcell.ColorBlack)
ArtistStyle = tcell.StyleDefault.Foreground(tcell.ColorPink).Background(tcell.ColorBlack) ArtistStyle = tcell.StyleDefault.Foreground(tcell.ColorPink).Background(tcell.ColorBlack)
TimeStyle = tcell.StyleDefault.Foreground(tcell.ColorOrange).Background(tcell.ColorBlack) TimeStyle = tcell.StyleDefault.Foreground(tcell.ColorOrange).Background(tcell.ColorBlack)
PlaylistNavStyle = tcell.StyleDefault.Foreground(tcell.ColorCoral).Background(tcell.ColorBlack) PlaylistNavStyle = tcell.StyleDefault.Foreground(tcell.ColorCoral).Background(tcell.ColorBlack)
NavStyle = tcell.StyleDefault.Foreground(tcell.ColorPapayaWhip).Background(tcell.ColorBlack) NavStyle = tcell.StyleDefault.Foreground(tcell.ColorPapayaWhip).Background(tcell.ColorBlack)
ContextMenuStyle = tcell.StyleDefault.Foreground(tcell.ColorPink).Background(tcell.ColorDefault).Bold(true) ContextMenuStyle = tcell.StyleDefault.Foreground(tcell.ColorPink).Background(tcell.ColorDefault).Bold(true)
NotSelectableStyle = tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorDefault).Bold(true).Italic(true)
) )
type Application struct { type Application struct {
@@ -46,7 +47,8 @@ func NewApplication() *Application {
pBar := NewProgressBar().SetProgressFunc(progressFunc) pBar := NewProgressBar().SetProgressFunc(progressFunc)
coverArt := newCoverArt() coverArt := newCoverArt()
searchbar := tview.NewBox().SetBorder(true).SetTitle("SEARCH").SetBackgroundColor(tcell.ColorDefault) searchbar := tview.NewBox().SetBorder(true).SetTitle("SEARCH").SetBackgroundColor(tcell.ColorDefault)
SetCurrentView(playlistView) SetCurrentView(topTracksView)
topTracksView.RefreshState()
Main := NewInteractiveView() Main := NewInteractiveView()
Main.Table.SetBorder(true) Main.Table.SetBorder(true)
@@ -123,7 +125,7 @@ func NewApplication() *Application {
AddItem(pBar, 5, 1, false) AddItem(pBar, 5, 1, false)
Root.Primitive("Main", MainFlex) Root.Primitive("Main", MainFlex)
App.SetRoot(Root.Root, true).SetFocus(playlistNav.Table) App.SetRoot(Root.Root, true).SetFocus(Main.Table)
InitNotifier() InitNotifier()
updateRoutine() updateRoutine()

View File

@@ -9,6 +9,7 @@ var (
albumsView = &AlbumsView{} albumsView = &AlbumsView{}
likedSongsView = &LikedSongsView{} likedSongsView = &LikedSongsView{}
recentlyPlayedView = &RecentlyPlayedView{} recentlyPlayedView = &RecentlyPlayedView{}
topTracksView = &TopTracksView{}
) )
type View interface { type View interface {

75
ui/view_top.go Normal file
View File

@@ -0,0 +1,75 @@
package ui
import (
"github.com/aditya-K2/gspt/spt"
"github.com/gdamore/tcell/v2"
"github.com/zmb3/spotify/v2"
)
type TopTracksView struct {
topTracks []spotify.FullTrack
topArtists []spotify.FullArtist
}
func (a *TopTracksView) RefreshState() {
topTracks, err := spt.GetTopTracks()
if err != nil {
SendNotification("Error retrieving Top Tracks: " + err.Error())
return
}
a.topTracks = topTracks
artists, err := spt.GetTopArtists()
if err != nil {
SendNotification("Error retrieving Top Artists: " + err.Error())
return
}
a.topArtists = artists
}
func (a *TopTracksView) Content() func() [][]Content {
return func() [][]Content {
c := make([][]Content, 0)
c = append(c, []Content{{"Top Artists:", NotSelectableStyle}})
for _, v := range a.topArtists {
c = append(c, []Content{
{Content: v.Name, Style: ArtistStyle},
{Content: v.Genres[0], Style: AlbumStyle},
})
}
c = append(c, []Content{{"Top Tracks:", NotSelectableStyle}})
for _, v := range a.topTracks {
c = append(c, []Content{
{Content: v.Name, Style: TrackStyle},
{Content: v.Artists[0].Name, Style: ArtistStyle},
{Content: v.Album.Name, Style: AlbumStyle},
})
}
return c
}
}
func (a *TopTracksView) ContextOpener() func(m *Root, s func(s int)) { return nil }
func (a *TopTracksView) ContextHandler() func(int, int, int) { return nil }
func (a *TopTracksView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
return func(e *tcell.EventKey) *tcell.EventKey {
if e.Key() == tcell.KeyEnter {
r, _ := Ui.Main.Table.GetSelection()
if r > 0 {
if r < (len(a.topArtists) + 1) {
if err := spt.PlayContext(&a.topArtists[r-1].URI); err != nil {
SendNotification(err.Error())
}
} else if r != len(a.topArtists)+1 {
if err := spt.PlaySong(a.topTracks[r-2-len(a.topArtists)].URI); err != nil {
SendNotification(err.Error())
}
}
}
}
return e
}
}
func (a *TopTracksView) ContextKey() rune { return 'a' }
func (a *TopTracksView) DisableVisualMode() bool { return true }
func (a *TopTracksView) Name() string { return "AlbumsView" }