aboutsummaryrefslogtreecommitdiff
path: root/src/page_playlists.go
blob: 2ec8165dcf3629426da693f4904ea350e5af12b0 (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
package src

import (
	"fmt"

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

func (a *app) playlistsPage() tview.Primitive {
	flex := tview.NewFlex().SetDirection(tview.FlexColumn)

	a.playlistsList = tview.NewList().
		SetMainTextColor(tcell.ColorRed).
		SetHighlightFullLine(true).
		ShowSecondaryText(false)
	a.playlistsList.SetBorder(true).SetBorderAttributes(tcell.AttrDim)

	a.playlistSongs = tview.NewList().
		SetHighlightFullLine(true).
		ShowSecondaryText(false)
	a.playlistSongs.SetBorder(true).SetBorderAttributes(tcell.AttrDim)

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

	// Setup e & n keybinds
	a.setupKeybindings(flex.Box)

	flex.AddItem(a.playlistsList, 0, 1, false)
	flex.AddItem(a.playlistSongs, 0, 1, false)

	return flex
}

func (a *app) refreshPlaylists() error {
	playlists, err := a.sub.GetPlaylists(nil)
	if err != nil {
		return err
	}

	a.allPlaylists = playlists

	a.playlistsList.Clear()
	for _, pl := range playlists {
		id := pl.ID
		a.playlistsList.AddItem(pl.Name, "", 0, func() {
			a.loadPlaylist(id)
			a.tv.SetFocus(a.playlistSongs)
			a.updateFooter()
		})
	}

	a.playlistsList.SetCurrentItem(0)

	return nil
}

func (a *app) loadPlaylist(id string) error {
	a.playlistSongs.Clear()
	pl, err := a.sub.GetPlaylist(id)
	if err != nil {
		return err
	}

	a.currentPlaylist = pl

	for _, s := range a.currentPlaylist.Entry {
		a.playlistSongs.AddItem(fmt.Sprintf("%s - %s", s.Title, s.Artist), "", 0, func() {
			sel := a.playlistSongs.GetCurrentItem()
			a.playQueue.Clear()
			for _, s := range a.currentPlaylist.Entry[sel:] {
				a.playQueue.Append(s)
			}

			if err := a.playQueue.Play(); err != nil {
				a.alert("Error: %v", err)
			}
		})
	}

	return nil
}