subsonic-tui/internal/playback/queue.go
Sagi Dayan 92e3c4ea7d
All checks were successful
ci / check for spelling errors (pull_request) Successful in 21s
ci / code quality (lint/tests) (pull_request) Successful in 2m29s
ci / Make sure build does not fail (pull_request) Successful in 2m25s
ci / notify-fail (pull_request) Has been skipped
ci / check for spelling errors (push) Successful in 22s
ci / code quality (lint/tests) (push) Successful in 1m53s
ci / Make sure build does not fail (push) Successful in 2m55s
ci / notify-fail (push) Has been skipped
Added ci workflow + fixed linting issues
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-18 18:02:01 +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
}