2024-01-16 15:21:35 +00:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
2024-12-18 15:29:16 +00:00
|
|
|
"strconv"
|
2024-01-16 15:21:35 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/config"
|
|
|
|
"github.com/delucks/go-subsonic"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
|
|
|
type queue struct {
|
|
|
|
view *tview.Table
|
|
|
|
playFunc func(index int)
|
|
|
|
currentSong int
|
|
|
|
songQueue []*subsonic.Child
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewQueue() *queue {
|
|
|
|
table := tview.NewTable()
|
|
|
|
table.SetBackgroundColor(config.ColorBackground)
|
|
|
|
table.SetTitle("Queue [4]")
|
|
|
|
table.SetBorder(true)
|
|
|
|
table.SetFocusFunc(func() {
|
|
|
|
table.SetBorderColor(config.ColorSelectedBoarder)
|
|
|
|
})
|
|
|
|
table.SetBlurFunc(func() {
|
|
|
|
table.SetBorderColor(config.ColorBluredBoarder)
|
|
|
|
})
|
|
|
|
|
|
|
|
return &queue{
|
|
|
|
view: table,
|
|
|
|
currentSong: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queue) SetPlayFunc(f func(index int)) {
|
|
|
|
q.playFunc = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queue) drawQueue() {
|
|
|
|
q.view.Clear()
|
|
|
|
list := q.view
|
|
|
|
list.SetWrapSelection(true, false)
|
|
|
|
list.SetSelectable(true, false)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
for i, song := range q.songQueue {
|
|
|
|
isCurrentSong := q.currentSong == i
|
|
|
|
isPlayed := i < q.currentSong
|
|
|
|
bgColor := config.ColorBackground
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if isCurrentSong {
|
|
|
|
bgColor = config.ColorQueuePlayingBg
|
|
|
|
} else if isPlayed {
|
|
|
|
bgColor = config.ColorQueuePlayedBg
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
|
|
|
num := tview.NewTableCell(strconv.Itoa(i + 1)).SetTextColor(config.ColorTextAccent).SetBackgroundColor(bgColor)
|
2024-01-16 15:21:35 +00:00
|
|
|
title := tview.NewTableCell(song.Title).SetMaxWidth(15).SetExpansion(2).SetBackgroundColor(bgColor)
|
|
|
|
artist := tview.NewTableCell(song.Artist).SetMaxWidth(15).SetExpansion(1).SetBackgroundColor(bgColor)
|
|
|
|
d := time.Second * time.Duration(song.Duration)
|
|
|
|
duration := tview.NewTableCell(d.String()).SetAlign(tview.AlignRight).SetExpansion(1).SetBackgroundColor(bgColor)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
list.SetCell(i, 0, num)
|
|
|
|
list.SetCell(i, 1, title)
|
|
|
|
list.SetCell(i, 2, artist)
|
|
|
|
list.SetCell(i, 3, duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
list.SetSelectedFunc(func(row, column int) {
|
|
|
|
q.currentSong = row
|
|
|
|
q.playFunc(row)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queue) Update(songs []*subsonic.Child, currentSong int) {
|
|
|
|
q.songQueue = songs
|
|
|
|
q.currentSong = currentSong
|
|
|
|
q.drawQueue()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queue) GetView() tview.Primitive {
|
|
|
|
return q.view
|
|
|
|
}
|