add trackiterator

This commit is contained in:
Drew Weymouth
2023-04-20 19:23:15 -07:00
parent a20f047c12
commit 51efab082e
2 changed files with 46 additions and 0 deletions
+4
View File
@@ -8,6 +8,10 @@ type AlbumIterator interface {
Next() *subsonic.AlbumID3
}
type TrackIterator interface {
Next() *subsonic.Child
}
type LibraryManager struct {
PreCacheCoverFn func(coverID string)
+42
View File
@@ -0,0 +1,42 @@
package backend
import "github.com/dweymouth/go-subsonic/subsonic"
type allTracksIterator struct {
albumIter AlbumIterator
curAlbum *subsonic.AlbumID3
curTrackIdx int
done bool
}
func (l *LibraryManager) AllTracksIterator() TrackIterator {
return &allTracksIterator{
albumIter: l.AlbumsIter(AlbumSortRecentlyAdded),
}
}
func (a *allTracksIterator) Next() *subsonic.Child {
if a.done {
return nil
}
// fetch next album
if a.curAlbum == nil || a.curTrackIdx >= len(a.curAlbum.Song) {
a.curAlbum = a.albumIter.Next()
if a.curAlbum == nil {
a.done = true
return nil
}
a.curTrackIdx = 0
if len(a.curAlbum.Song) == 0 {
// in the unlikely case of an album with zero tracks,
// just call recursively to move to next album
return a.Next()
}
}
tr := a.curAlbum.Song[a.curTrackIdx]
a.curTrackIdx += 1
return tr
}