aboutsummaryrefslogtreecommitdiff
path: root/src/page_artists.go
blob: 4175f3ca0c7f055d101322af4f311048bd1162e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package src

import (
	"fmt"

	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

type selection struct {
	entryType string
	id        string
}

func (a *app) artistsPage() tview.Primitive {
	grid := tview.NewFlex().SetDirection(tview.FlexColumn)

	// Artist & album list
	root := tview.NewTreeNode("Subsonic server").SetColor(tcell.ColorYellow)
	a.artistsTree = tview.NewTreeView().
		SetRoot(root).
		SetCurrentNode(root).
		SetPrefixes([]string{"", " ", " "}).
		SetSelectedFunc(func(node *tview.TreeNode) {
			if node.GetReference() == nil {
				return
			}

			sel := node.GetReference().(selection)
			if sel.entryType == "artist" {
				node.SetExpanded(!node.IsExpanded())
				return
			}

			a.loadAlbumInPanel(sel.id)
			a.tv.SetFocus(a.songsList)
			a.updateFooter()
		})
	a.artistsTree.SetBorderAttributes(tcell.AttrDim).SetBorder(true)

	// Songs list for the selected album
	a.songsList = tview.NewList()
	a.songsList.ShowSecondaryText(false).SetHighlightFullLine(true)
	a.songsList.SetBorderAttributes(tcell.AttrDim).SetBorder(true)

	// Change the left-right keys to switch between the panels
	a.artistsTree.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyLeft || event.Key() == tcell.KeyRight {
			a.tv.SetFocus(a.songsList)
			a.updateFooter()
			return nil
		}
		return event
	})

	a.songsList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyLeft || event.Key() == tcell.KeyRight {
			a.tv.SetFocus(a.artistsTree)
			a.updateFooter()
			return nil
		}
		return event
	})

	a.setupKeybindings(grid.Box)

	grid.AddItem(a.artistsTree, 0, 1, true)
	grid.AddItem(a.songsList, 0, 1, false)

	return grid
}

func (a *app) refreshArtists() error {
	artistsID3, err := a.sub.GetArtists(nil)
	if err != nil {
		return err
	}

	a.artistsTree.GetRoot().ClearChildren()
	for _, index := range artistsID3.Index {
		for _, artist := range index.Artist {
			node := tview.NewTreeNode(artist.Name)
			node.SetReference(selection{"artist", artist.ID})
			node.SetColor(tcell.ColorRed)
			node.SetSelectable(true)
			node.SetExpanded(false)

			albums, err := a.sub.GetMusicDirectory(artist.ID)
			if err != nil {
				return err
			}

			for _, album := range albums.Child {
				subnode := tview.NewTreeNode(album.Title)
				subnode.SetReference(selection{"album", album.ID})
				subnode.SetColor(tcell.ColorBlue)
				subnode.SetSelectable(true)

				node.AddChild(subnode)
			}

			a.artistsTree.GetRoot().AddChild(node)
		}
	}

	a.artistsTree.GetRoot().SetExpanded(true)

	return nil
}

func (a *app) loadAlbumInPanel(id string) error {
	album, err := a.sub.GetMusicDirectory(id)
	if err != nil {
		return err
	}

	a.songsList.Clear()
	a.currentSongs = album.Child
	for _, song := range album.Child {
		txt := fmt.Sprintf("%-2d - %s", song.Track, song.Title)

		a.songsList.AddItem(txt, "", 0, func() {
			sel := a.songsList.GetCurrentItem()
			a.playQueue.Clear()
			for _, s := range a.currentSongs[sel:] {
				a.playQueue.Append(s)
			}
			err := a.playQueue.Play()
			if err != nil {
				a.alert("Error: %v", err)
			}
		})
	}

	a.songsList.SetCurrentItem(0)

	return nil
}