subsonic-tui/internal/playback/queue.go
Sagi Dayan 99e8e299e4
Some checks failed
ci / check for spelling errors (pull_request) Failing after 22s
ci / Make sure build does not fail (pull_request) Successful in 2m52s
ci / code quality (lint/tests) (pull_request) Has been skipped
Added ci workflow + fixed linting issues
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-18 17:29:16 +02:00

94 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++
return q.GetCurrentSong()
}
return nil
}
func (q *queue) Prev() *subsonic.Child {
if q.currentSong > 0 {
q.currentSong--
return q.GetCurrentSong()
}
return nil
}
func (q *queue) Get() []*subsonic.Child {
return q.songQueue
}