2024-01-16 15:21:35 +00:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/client"
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/config"
|
|
|
|
"github.com/delucks/go-subsonic"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ View = &albums{}
|
|
|
|
|
|
|
|
type albums struct {
|
|
|
|
view *tview.Table
|
|
|
|
client *client.Client
|
|
|
|
albums []*subsonic.AlbumID3
|
|
|
|
callback func(albumID string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAlbums(client *client.Client) *albums {
|
|
|
|
list := tview.NewTable()
|
|
|
|
|
|
|
|
list.SetBackgroundColor(config.ColorBackground)
|
|
|
|
list.SetTitle("Albums [2]")
|
|
|
|
list.SetBorder(true)
|
|
|
|
|
|
|
|
resp, _ := client.GetAlbums()
|
|
|
|
|
|
|
|
obj := &albums{
|
|
|
|
view: list,
|
|
|
|
client: client,
|
|
|
|
albums: resp,
|
|
|
|
}
|
|
|
|
|
|
|
|
list.SetSelectedFunc(func(row, column int) {
|
|
|
|
obj.callback(obj.albums[row].ID)
|
|
|
|
})
|
|
|
|
|
|
|
|
list.SetFocusFunc(func() {
|
|
|
|
list.SetBorderColor(config.ColorSelectedBoarder)
|
|
|
|
list.SetSelectable(true, false)
|
|
|
|
})
|
|
|
|
list.SetBlurFunc(func() {
|
|
|
|
list.SetBorderColor(config.ColorBluredBoarder)
|
|
|
|
list.SetSelectable(false, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
obj.Update()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *albums) SetAlbums(al []*subsonic.AlbumID3) {
|
|
|
|
a.albums = al
|
|
|
|
a.Update()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *albums) Update() {
|
|
|
|
a.view.Clear()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
for i, pl := range a.albums {
|
|
|
|
title := tview.NewTableCell(pl.Name).SetExpansion(1).SetMaxWidth(15)
|
|
|
|
artist := tview.NewTableCell(pl.Artist).SetExpansion(1).SetAlign(tview.AlignRight)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
a.view.SetCell(i, 0, title)
|
|
|
|
a.view.SetCell(i, 1, artist)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *albums) SetCallback(f func(albumID string)) {
|
|
|
|
a.callback = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *albums) GetView() tview.Primitive {
|
|
|
|
return a.view
|
|
|
|
}
|