trackiterator properly loads album tracklist

This commit is contained in:
Drew Weymouth
2023-04-20 19:24:44 -07:00
parent ddc4d5272b
commit 88d4322ecd
+17 -7
View File
@@ -1,8 +1,13 @@
package backend package backend
import "github.com/dweymouth/go-subsonic/subsonic" import (
"log"
"github.com/dweymouth/go-subsonic/subsonic"
)
type allTracksIterator struct { type allTracksIterator struct {
l *LibraryManager
albumIter AlbumIterator albumIter AlbumIterator
curAlbum *subsonic.AlbumID3 curAlbum *subsonic.AlbumID3
curTrackIdx int curTrackIdx int
@@ -11,7 +16,8 @@ type allTracksIterator struct {
func (l *LibraryManager) AllTracksIterator() TrackIterator { func (l *LibraryManager) AllTracksIterator() TrackIterator {
return &allTracksIterator{ return &allTracksIterator{
albumIter: l.AlbumsIter(AlbumSortRecentlyAdded), l: l,
albumIter: l.AlbumsIter(AlbumSortArtistAZ),
} }
} }
@@ -22,18 +28,22 @@ func (a *allTracksIterator) Next() *subsonic.Child {
// fetch next album // fetch next album
if a.curAlbum == nil || a.curTrackIdx >= len(a.curAlbum.Song) { if a.curAlbum == nil || a.curTrackIdx >= len(a.curAlbum.Song) {
a.curAlbum = a.albumIter.Next() al := a.albumIter.Next()
if a.curAlbum == nil { if al == nil {
a.done = true a.done = true
return nil return nil
} }
a.curTrackIdx = 0 al, err := a.l.s.Server.GetAlbum(al.ID)
if err != nil {
if len(a.curAlbum.Song) == 0 { log.Printf("error fetching album: %s", err.Error())
}
if len(al.Song) == 0 {
// in the unlikely case of an album with zero tracks, // in the unlikely case of an album with zero tracks,
// just call recursively to move to next album // just call recursively to move to next album
return a.Next() return a.Next()
} }
a.curAlbum = al
a.curTrackIdx = 0
} }
tr := a.curAlbum.Song[a.curTrackIdx] tr := a.curAlbum.Song[a.curTrackIdx]