subsonic-tui/internal/playback/queue.go
Sagi Dayan a3923cf42c initial commit
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-03-29 17:56:39 +03:00

85 lines
1.6 KiB
Go

package playback
import "github.com/delucks/go-subsonic"
type queue struct {
currentSong int
songQueue []*subsonic.Child
}
func newQueue() *queue {
return &queue{
currentSong: 0,
songQueue: []*subsonic.Child{},
}
}
func (q *queue) Set(songs []*subsonic.Child) {
q.currentSong = 0
q.songQueue = songs
}
func (q *queue) Clear() {
q.currentSong = 0
q.songQueue = []*subsonic.Child{}
}
// returns true if queue was empty before addition
func (q *queue) Add(songs ...*subsonic.Child) bool {
shouldStartPlaying := len(q.songQueue) == 0
q.songQueue = append(q.songQueue, songs...)
if shouldStartPlaying {
q.currentSong = 0
return true
}
return false
}
// returns a song if position has changed
func (q *queue) SetPosition(position int) *subsonic.Child {
if position == q.currentSong || position < 0 || len(q.songQueue) < position {
return nil
}
q.currentSong = position
return q.GetCurrentSong()
}
func (q *queue) GetPosition() int {
return q.currentSong
}
func (q *queue) GetCurrentSong() *subsonic.Child {
if len(q.songQueue) == 0 {
return nil
}
return q.songQueue[q.currentSong]
}
func (q *queue) HasPrev() bool {
return 0 < q.currentSong
}
func (q *queue) HasNext() bool {
return q.currentSong < len(q.songQueue)-1
}
func (q *queue) Next() *subsonic.Child {
if len(q.songQueue) > q.currentSong+1 {
q.currentSong = q.currentSong + 1
return q.GetCurrentSong()
}
return nil
}
func (q *queue) Prev() *subsonic.Child {
if q.currentSong > 0 {
q.currentSong = q.currentSong - 1
return q.GetCurrentSong()
}
return nil
}
func (q *queue) Get() []*subsonic.Child {
return q.songQueue
}