From 08182123b8165c8c8dc55d1fefc643f5c5c47035 Mon Sep 17 00:00:00 2001 From: Simon Garrelou Date: Sat, 10 Dec 2022 22:22:32 +0100 Subject: Add playlist support --- src/page_playlists.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/page_playlists.go (limited to 'src/page_playlists.go') diff --git a/src/page_playlists.go b/src/page_playlists.go new file mode 100644 index 0000000..303a9a7 --- /dev/null +++ b/src/page_playlists.go @@ -0,0 +1,98 @@ +package src + +import ( + "fmt" + + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +func (a *app) playlistsPage() tview.Primitive { + flex := tview.NewFlex().SetDirection(tview.FlexColumn) + + a.playlistsList = tview.NewList(). + SetMainTextColor(tcell.ColorRed). + SetHighlightFullLine(true). + ShowSecondaryText(false) + a.playlistsList.SetBorder(true).SetBorderAttributes(tcell.AttrDim) + + a.playlistSongs = tview.NewList(). + SetHighlightFullLine(true). + ShowSecondaryText(false) + a.playlistSongs.SetBorder(true).SetBorderAttributes(tcell.AttrDim) + + // Change the left-right keys to switch between the panels + a.playlistsList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyLeft || event.Key() == tcell.KeyRight { + a.tv.SetFocus(a.playlistSongs) + a.updateFooter() + return nil + } + return event + }) + a.playlistSongs.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyLeft || event.Key() == tcell.KeyRight { + a.tv.SetFocus(a.playlistsList) + a.updateFooter() + return nil + } + return event + }) + + // Setup e & n keybinds + a.setupMusicControlKeys(flex.Box) + + flex.AddItem(a.playlistsList, 0, 1, false) + flex.AddItem(a.playlistSongs, 0, 1, false) + + return flex +} + +func (a *app) refreshPlaylists() error { + playlists, err := a.sub.GetPlaylists(nil) + if err != nil { + return err + } + + a.allPlaylists = playlists + + a.playlistsList.Clear() + for _, pl := range playlists { + id := pl.ID + a.playlistsList.AddItem(pl.Name, "", 0, func() { + a.loadPlaylist(id) + a.tv.SetFocus(a.playlistSongs) + a.updateFooter() + }) + } + + a.playlistsList.SetCurrentItem(0) + + return nil +} + +func (a *app) loadPlaylist(id string) error { + a.playlistSongs.Clear() + pl, err := a.sub.GetPlaylist(id) + if err != nil { + return err + } + + a.currentPlaylist = pl + + for _, s := range a.currentPlaylist.Entry { + a.playlistSongs.AddItem(fmt.Sprintf("%s - %s", s.Title, s.Artist), "", 0, func() { + sel := a.playlistSongs.GetCurrentItem() + a.playQueue.Clear() + for _, s := range a.currentPlaylist.Entry[sel:] { + a.playQueue.Append(s) + } + + if err := a.playQueue.Play(); err != nil { + a.alert("Error: %v", err) + } + }) + } + + return nil +} -- cgit v1.2.3