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
import "github.com/dweymouth/go-subsonic/subsonic"
import (
"log"
"github.com/dweymouth/go-subsonic/subsonic"
)
type allTracksIterator struct {
l *LibraryManager
albumIter AlbumIterator
curAlbum *subsonic.AlbumID3
curTrackIdx int
@@ -11,7 +16,8 @@ type allTracksIterator struct {
func (l *LibraryManager) AllTracksIterator() TrackIterator {
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
if a.curAlbum == nil || a.curTrackIdx >= len(a.curAlbum.Song) {
a.curAlbum = a.albumIter.Next()
if a.curAlbum == nil {
al := a.albumIter.Next()
if al == nil {
a.done = true
return nil
}
a.curTrackIdx = 0
if len(a.curAlbum.Song) == 0 {
al, err := a.l.s.Server.GetAlbum(al.ID)
if err != nil {
log.Printf("error fetching album: %s", err.Error())
}
if len(al.Song) == 0 {
// in the unlikely case of an album with zero tracks,
// just call recursively to move to next album
return a.Next()
}
a.curAlbum = al
a.curTrackIdx = 0
}
tr := a.curAlbum.Song[a.curTrackIdx]