Merge pull request #27 from dweymouth/develop
Add support for browsing and playing existing playlists
This commit is contained in:
@@ -18,8 +18,8 @@ Slightly outdated screenshots of Supersonic running against the Navidrome <a hre
|
||||
* [x] Browse by genre
|
||||
* [x] Browse by artist
|
||||
* [x] Set/unset favorite and browse by favorites (albums only; artists+songs coming soon)
|
||||
* [x] Browse and play playlists (create and edit support coming soon)
|
||||
* [ ] View and edit play queue (coming soon)
|
||||
* [ ] Browse, create, and edit playlists (coming soon)
|
||||
* [ ] Artist view with biography, image, similar artists (coming soon)
|
||||
* [ ] Shuffle and repeat playback modes (planned)
|
||||
* [ ] Set and view five-star rating (planned)
|
||||
|
||||
@@ -108,12 +108,25 @@ func (p *PlaybackManager) LoadAlbum(albumID string, appendToQueue bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return p.LoadTracks(album.Song, appendToQueue)
|
||||
}
|
||||
|
||||
// Loads the specified playlist into the play queue.
|
||||
func (p *PlaybackManager) LoadPlaylist(playlistID string, appendToQueue bool) error {
|
||||
playlist, err := p.sm.Server.GetPlaylist(playlistID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return p.LoadTracks(playlist.Entry, appendToQueue)
|
||||
}
|
||||
|
||||
func (p *PlaybackManager) LoadTracks(tracks []*subsonic.Child, appendToQueue bool) error {
|
||||
if !appendToQueue {
|
||||
p.player.Stop()
|
||||
p.nowPlayingIdx = 0
|
||||
p.playQueue = nil
|
||||
}
|
||||
for _, song := range album.Song {
|
||||
for _, song := range tracks {
|
||||
url, err := p.sm.Server.GetStreamURL(song.ID, map[string]string{})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -134,6 +147,16 @@ func (p *PlaybackManager) PlayAlbum(albumID string, firstTrack int) error {
|
||||
return p.player.PlayTrackAt(firstTrack)
|
||||
}
|
||||
|
||||
func (p *PlaybackManager) PlayPlaylist(playlistID string, firstTrack int) error {
|
||||
if err := p.LoadPlaylist(playlistID, false); err != nil {
|
||||
return err
|
||||
}
|
||||
if firstTrack <= 0 {
|
||||
return p.player.PlayFromBeginning()
|
||||
}
|
||||
return p.player.PlayTrackAt(firstTrack)
|
||||
}
|
||||
|
||||
func (p *PlaybackManager) checkScrobble(playDur time.Duration) {
|
||||
if playDur.Seconds() < 0.1 || p.curTrackTime < 0.1 {
|
||||
return
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
package browsing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"supersonic/backend"
|
||||
"supersonic/ui/layouts"
|
||||
"supersonic/ui/util"
|
||||
"supersonic/ui/widgets"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"github.com/dweymouth/go-subsonic"
|
||||
)
|
||||
|
||||
type PlaylistPage struct {
|
||||
widget.BaseWidget
|
||||
|
||||
playlistID string
|
||||
sm *backend.ServerManager
|
||||
pm *backend.PlaybackManager
|
||||
nav func(Route)
|
||||
header *PlaylistPageHeader
|
||||
tracklist *widgets.Tracklist
|
||||
nowPlayingID string
|
||||
container *fyne.Container
|
||||
popUpProvider PopUpProvider
|
||||
}
|
||||
|
||||
func NewPlaylistPage(
|
||||
playlistID string,
|
||||
sm *backend.ServerManager,
|
||||
pm *backend.PlaybackManager,
|
||||
nav func(Route),
|
||||
) *PlaylistPage {
|
||||
a := &PlaylistPage{playlistID: playlistID, sm: sm, pm: pm}
|
||||
a.ExtendBaseWidget(a)
|
||||
a.header = NewPlaylistPageHeader(a)
|
||||
a.tracklist = widgets.NewTracklist(nil)
|
||||
a.tracklist.AutoNumber = true
|
||||
a.tracklist.OnPlayTrackAt = a.onPlayTrackAt
|
||||
a.container = container.NewBorder(
|
||||
container.New(&layouts.MaxPadLayout{PadLeft: 15, PadRight: 15, PadTop: 15, PadBottom: 10}, a.header),
|
||||
nil, nil, nil, container.New(&layouts.MaxPadLayout{PadLeft: 15, PadRight: 15, PadBottom: 15}, a.tracklist))
|
||||
a.loadAsync()
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *PlaylistPage) CreateRenderer() fyne.WidgetRenderer {
|
||||
return widget.NewSimpleRenderer(a.container)
|
||||
}
|
||||
|
||||
func (a *PlaylistPage) Save() SavedPage {
|
||||
return &savedPlaylistPage{
|
||||
playlistID: a.playlistID,
|
||||
sm: a.sm,
|
||||
pm: a.pm,
|
||||
nav: a.nav,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *PlaylistPage) Route() Route {
|
||||
return AlbumRoute(a.playlistID)
|
||||
}
|
||||
|
||||
func (a *PlaylistPage) OnSongChange(song *subsonic.Child) {
|
||||
if song == nil {
|
||||
a.nowPlayingID = ""
|
||||
} else {
|
||||
a.nowPlayingID = song.ID
|
||||
}
|
||||
a.tracklist.SetNowPlaying(a.nowPlayingID)
|
||||
}
|
||||
|
||||
func (a *PlaylistPage) Reload() {
|
||||
a.loadAsync()
|
||||
}
|
||||
|
||||
func (a *PlaylistPage) onPlayTrackAt(tracknum int) {
|
||||
a.pm.PlayPlaylist(a.playlistID, tracknum)
|
||||
}
|
||||
|
||||
func (a *PlaylistPage) loadAsync() {
|
||||
go func() {
|
||||
playlist, err := a.sm.Server.GetPlaylist(a.playlistID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get playlist: %s", err.Error())
|
||||
return
|
||||
}
|
||||
a.header.Update(playlist)
|
||||
a.tracklist.Tracks = playlist.Entry
|
||||
a.tracklist.SetNowPlaying(a.nowPlayingID)
|
||||
}()
|
||||
}
|
||||
|
||||
type PlaylistPageHeader struct {
|
||||
widget.BaseWidget
|
||||
|
||||
page *PlaylistPage
|
||||
|
||||
titleLabel *widget.RichText
|
||||
descriptionLabel *widget.Label
|
||||
createdAtLabel *widget.Label
|
||||
ownerLabel *widget.Label
|
||||
trackTimeLabel *widget.Label
|
||||
|
||||
playButton *widget.Button
|
||||
|
||||
container *fyne.Container
|
||||
}
|
||||
|
||||
func NewPlaylistPageHeader(page *PlaylistPage) *PlaylistPageHeader {
|
||||
a := &PlaylistPageHeader{page: page}
|
||||
a.ExtendBaseWidget(a)
|
||||
|
||||
a.titleLabel = widget.NewRichTextWithText("")
|
||||
a.titleLabel.Wrapping = fyne.TextTruncate
|
||||
a.titleLabel.Segments[0].(*widget.TextSegment).Style = widget.RichTextStyle{
|
||||
SizeName: theme.SizeNameHeadingText,
|
||||
}
|
||||
a.descriptionLabel = widget.NewLabel("")
|
||||
a.ownerLabel = widget.NewLabel("")
|
||||
a.createdAtLabel = widget.NewLabel("")
|
||||
a.trackTimeLabel = widget.NewLabel("")
|
||||
a.playButton = widget.NewButtonWithIcon("Play", theme.MediaPlayIcon(), func() {
|
||||
page.onPlayTrackAt(0)
|
||||
})
|
||||
|
||||
a.container = container.NewVBox(a.titleLabel, container.New(&layouts.VboxCustomPadding{ExtraPad: -10},
|
||||
a.descriptionLabel,
|
||||
a.ownerLabel,
|
||||
a.trackTimeLabel),
|
||||
container.NewHBox(a.playButton),
|
||||
)
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *PlaylistPageHeader) CreateRenderer() fyne.WidgetRenderer {
|
||||
return widget.NewSimpleRenderer(a.container)
|
||||
}
|
||||
|
||||
func (a *PlaylistPageHeader) Update(playlist *subsonic.Playlist) {
|
||||
a.titleLabel.Segments[0].(*widget.TextSegment).Text = playlist.Name
|
||||
a.descriptionLabel.SetText(playlist.Comment)
|
||||
a.ownerLabel.SetText(a.formatPlaylistOwnerStr(playlist))
|
||||
a.trackTimeLabel.SetText(a.formatPlaylistTrackTimeStr(playlist))
|
||||
a.createdAtLabel.SetText("created at TODO")
|
||||
a.Refresh()
|
||||
}
|
||||
|
||||
func (a *PlaylistPageHeader) formatPlaylistOwnerStr(p *subsonic.Playlist) string {
|
||||
pubPriv := "Public"
|
||||
if !p.Public {
|
||||
pubPriv = "Private"
|
||||
}
|
||||
return fmt.Sprintf("%s playlist by %s", pubPriv, p.Owner)
|
||||
}
|
||||
|
||||
func (a *PlaylistPageHeader) formatPlaylistTrackTimeStr(p *subsonic.Playlist) string {
|
||||
return fmt.Sprintf("%d tracks, %s", p.SongCount, util.SecondsToTimeString(float64(p.Duration)))
|
||||
}
|
||||
|
||||
type savedPlaylistPage struct {
|
||||
playlistID string
|
||||
sm *backend.ServerManager
|
||||
pm *backend.PlaybackManager
|
||||
nav func(Route)
|
||||
}
|
||||
|
||||
func (s *savedPlaylistPage) Restore() Page {
|
||||
return NewPlaylistPage(s.playlistID, s.sm, s.pm, s.nav)
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package browsing
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"supersonic/backend"
|
||||
"supersonic/ui/layouts"
|
||||
"supersonic/ui/widgets"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"github.com/dweymouth/go-subsonic"
|
||||
)
|
||||
|
||||
type PlaylistsPage struct {
|
||||
widget.BaseWidget
|
||||
|
||||
sm *backend.ServerManager
|
||||
nav func(Route)
|
||||
titleDisp *widget.RichText
|
||||
container *fyne.Container
|
||||
list *PlaylistList
|
||||
}
|
||||
|
||||
func NewPlaylistsPage(sm *backend.ServerManager, nav func(Route)) *PlaylistsPage {
|
||||
a := &PlaylistsPage{
|
||||
sm: sm,
|
||||
nav: nav,
|
||||
titleDisp: widget.NewRichTextWithText("Playlists"),
|
||||
}
|
||||
a.ExtendBaseWidget(a)
|
||||
a.titleDisp.Segments[0].(*widget.TextSegment).Style.SizeName = theme.SizeNameHeadingText
|
||||
a.list = NewPlaylistList()
|
||||
a.list.OnNavTo = func(id string) {
|
||||
nav(PlaylistRoute(id))
|
||||
}
|
||||
a.buildContainer()
|
||||
go a.loadAsync()
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *PlaylistsPage) loadAsync() {
|
||||
playlists, err := a.sm.Server.GetPlaylists(nil)
|
||||
if err != nil {
|
||||
log.Printf("error loading playlists: %v", err.Error())
|
||||
}
|
||||
a.list.Playlists = playlists
|
||||
a.list.Refresh()
|
||||
}
|
||||
|
||||
func (a *PlaylistsPage) Route() Route {
|
||||
return PlaylistsRoute()
|
||||
}
|
||||
|
||||
func (a *PlaylistsPage) Reload() {
|
||||
go a.loadAsync()
|
||||
}
|
||||
|
||||
func (a *PlaylistsPage) Save() SavedPage {
|
||||
return &savedPlaylistsPage{
|
||||
sm: a.sm,
|
||||
nav: a.nav,
|
||||
}
|
||||
}
|
||||
|
||||
type savedPlaylistsPage struct {
|
||||
sm *backend.ServerManager
|
||||
nav func(Route)
|
||||
}
|
||||
|
||||
func (s *savedPlaylistsPage) Restore() Page {
|
||||
return NewPlaylistsPage(s.sm, s.nav)
|
||||
}
|
||||
|
||||
func (a *PlaylistsPage) buildContainer() {
|
||||
a.container = container.New(&layouts.MaxPadLayout{PadLeft: 15, PadRight: 15, PadTop: 5, PadBottom: 15},
|
||||
container.NewBorder(a.titleDisp, nil, nil, nil, a.list))
|
||||
}
|
||||
|
||||
func (a *PlaylistsPage) CreateRenderer() fyne.WidgetRenderer {
|
||||
return widget.NewSimpleRenderer(a.container)
|
||||
}
|
||||
|
||||
type PlaylistList struct {
|
||||
widget.BaseWidget
|
||||
|
||||
Playlists []*subsonic.Playlist
|
||||
OnNavTo func(string)
|
||||
|
||||
columnsLayout *layouts.ColumnsLayout
|
||||
header *widgets.ListHeader
|
||||
list *widget.List
|
||||
container *fyne.Container
|
||||
}
|
||||
|
||||
func NewPlaylistList() *PlaylistList {
|
||||
a := &PlaylistList{
|
||||
columnsLayout: layouts.NewColumnsLayout([]float32{-1, -1, 200, 125}),
|
||||
}
|
||||
a.header = widgets.NewListHeader([]string{"Name", "Description", "Owner", "Track Count"}, a.columnsLayout)
|
||||
a.list = widget.NewList(
|
||||
func() int {
|
||||
return len(a.Playlists)
|
||||
},
|
||||
func() fyne.CanvasObject {
|
||||
r := NewPlaylistListRow(a.columnsLayout)
|
||||
r.OnTapped = func() { a.onRowTapped(r.ID) }
|
||||
return r
|
||||
},
|
||||
func(id widget.ListItemID, item fyne.CanvasObject) {
|
||||
row := item.(*PlaylistListRow)
|
||||
row.ID = a.Playlists[id].ID
|
||||
row.nameLabel.Text = a.Playlists[id].Name
|
||||
row.descrptionLabel.Text = a.Playlists[id].Comment
|
||||
row.ownerLabel.Text = a.Playlists[id].Owner
|
||||
row.trackCountLabel.Text = strconv.Itoa(a.Playlists[id].SongCount)
|
||||
row.Refresh()
|
||||
},
|
||||
)
|
||||
a.container = container.NewBorder(a.header, nil, nil, nil, a.list)
|
||||
a.ExtendBaseWidget(a)
|
||||
return a
|
||||
}
|
||||
|
||||
func (p *PlaylistList) CreateRenderer() fyne.WidgetRenderer {
|
||||
return widget.NewSimpleRenderer(p.container)
|
||||
}
|
||||
|
||||
func (p *PlaylistList) onRowTapped(id string) {
|
||||
if p.OnNavTo != nil {
|
||||
p.OnNavTo(id)
|
||||
}
|
||||
}
|
||||
|
||||
type PlaylistListRow struct {
|
||||
widget.BaseWidget
|
||||
|
||||
ID string
|
||||
OnTapped func()
|
||||
|
||||
nameLabel *widget.Label
|
||||
descrptionLabel *widget.Label
|
||||
ownerLabel *widget.Label
|
||||
trackCountLabel *widget.Label
|
||||
|
||||
container *fyne.Container
|
||||
}
|
||||
|
||||
func NewPlaylistListRow(layout *layouts.ColumnsLayout) *PlaylistListRow {
|
||||
a := &PlaylistListRow{
|
||||
nameLabel: widget.NewLabel(""),
|
||||
descrptionLabel: widget.NewLabel(""),
|
||||
ownerLabel: widget.NewLabel(""),
|
||||
trackCountLabel: widget.NewLabel(""),
|
||||
}
|
||||
a.ownerLabel.Wrapping = fyne.TextTruncate
|
||||
a.container = container.New(layout, a.nameLabel, a.descrptionLabel, a.ownerLabel, a.trackCountLabel)
|
||||
a.ExtendBaseWidget(a)
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *PlaylistListRow) Tapped(*fyne.PointEvent) {
|
||||
if a.OnTapped != nil {
|
||||
a.OnTapped()
|
||||
}
|
||||
}
|
||||
|
||||
func (a *PlaylistListRow) CreateRenderer() fyne.WidgetRenderer {
|
||||
return widget.NewSimpleRenderer(a.container)
|
||||
}
|
||||
@@ -51,6 +51,13 @@ func GenresRoute() Route {
|
||||
return Route{Page: Genres}
|
||||
}
|
||||
|
||||
func PlaylistRoute(id string) Route {
|
||||
return Route{Page: Playlist, Arg: id}
|
||||
}
|
||||
func PlaylistsRoute() Route {
|
||||
return Route{Page: Playlists}
|
||||
}
|
||||
|
||||
func ArtistsRoute() Route {
|
||||
return Route{Page: Artists}
|
||||
}
|
||||
@@ -105,6 +112,10 @@ func (r Router) CreatePage(rte Route) Page {
|
||||
return NewGenrePage(rte.Arg, r.App.LibraryManager, r.App.ImageManager, r.OpenRoute)
|
||||
case Genres:
|
||||
return NewArtistsGenresPage(true, r.App.ServerManager, r.OpenRoute)
|
||||
case Playlist:
|
||||
return NewPlaylistPage(rte.Arg, r.App.ServerManager, r.App.PlaybackManager, r.OpenRoute)
|
||||
case Playlists:
|
||||
return NewPlaylistsPage(r.App.ServerManager, r.OpenRoute)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -87,6 +87,9 @@ func (m *MainWindow) addNavigationButtons() {
|
||||
m.BrowsingPane.AddNavigationButton(res.ResTheatermasksInvertPng, func() {
|
||||
m.Router.OpenRoute(browsing.GenresRoute())
|
||||
})
|
||||
m.BrowsingPane.AddNavigationButton(res.ResPlaylistInvertPng, func() {
|
||||
m.Router.OpenRoute(browsing.PlaylistsRoute())
|
||||
})
|
||||
}
|
||||
|
||||
func (m *MainWindow) addShortcuts() {
|
||||
|
||||
+12
-3
@@ -45,14 +45,18 @@ func NewTrackRow(layout *layouts.ColumnsLayout) *TrackRow {
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *TrackRow) Update(tr *subsonic.Child, isPlaying bool) {
|
||||
func (t *TrackRow) Update(tr *subsonic.Child, isPlaying bool, rowNum int) {
|
||||
if tr.ID == t.prevTrackID && isPlaying == t.prevIsPlaying {
|
||||
return
|
||||
}
|
||||
t.prevTrackID = t.trackID
|
||||
t.prevIsPlaying = isPlaying
|
||||
t.trackID = tr.ID
|
||||
t.num.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(tr.Track)
|
||||
|
||||
if rowNum < 0 {
|
||||
rowNum = tr.Track
|
||||
}
|
||||
t.num.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(rowNum)
|
||||
t.name.Segments[0].(*widget.TextSegment).Text = tr.Title
|
||||
t.artist.Segments[0].(*widget.TextSegment).Text = tr.Artist
|
||||
t.dur.Segments[0].(*widget.TextSegment).Text = util.SecondsToTimeString(float64(tr.Duration))
|
||||
@@ -79,6 +83,7 @@ type Tracklist struct {
|
||||
widget.BaseWidget
|
||||
|
||||
Tracks []*subsonic.Child
|
||||
AutoNumber bool
|
||||
OnPlayTrackAt func(int)
|
||||
|
||||
nowPlayingIdx int
|
||||
@@ -99,7 +104,11 @@ func NewTracklist(tracks []*subsonic.Child) *Tracklist {
|
||||
func(itemID widget.ListItemID, item fyne.CanvasObject) {
|
||||
tr := item.(*TrackRow)
|
||||
tr.OnDoubleTapped = func() { t.onPlayTrackAt(itemID) }
|
||||
tr.Update(t.Tracks[itemID], itemID == t.nowPlayingIdx)
|
||||
i := itemID + 1
|
||||
if !t.AutoNumber {
|
||||
i = -1 // signal that we want to use the track num.
|
||||
}
|
||||
tr.Update(t.Tracks[itemID], itemID == t.nowPlayingIdx, i)
|
||||
})
|
||||
t.container = container.NewBorder(t.hdr, nil, nil, nil, t.list)
|
||||
return t
|
||||
|
||||
Reference in New Issue
Block a user