-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
91 lines (83 loc) · 2.12 KB
/
update.go
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
package main
import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/codegoalie/bubbletea-test/models"
)
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
return m.updateKeyMsg(msg)
case songMsg:
return m.updateSongMsg(songMsg(msg))
case errMsg:
return m.updateErrMsg(errMsg(msg))
case spinner.TickMsg:
return m.updateSpinner(msg)
case tickMsg:
}
return m, m.spinner.Tick
}
func (m model) updateKeyMsg(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
} else {
m.cursor = len(m.choices) - 1
}
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
} else {
m.cursor = 0
}
case "enter", " ":
return m.updateEnter()
}
return m, m.spinner.Tick
}
func (m model) updateEnter() (tea.Model, tea.Cmd) {
m.selected = m.cursor
newSelection := m.choices[m.selected]
m.mediaURLs <- newSelection.StreamURL()
currentSong := newSelection.CurrentTrack()
m.notifier.Update(
currentSong.Title,
currentSong.Artist,
"",
)
return m, tea.Batch(waitForUpdates(m.updates), m.spinner.Tick)
}
func (m model) updateSongMsg(msg songMsg) (tea.Model, tea.Cmd) {
var newChoices = make(models.Stations, len(m.choices))
for i, choice := range m.choices {
newChoice := choice
if choice.Name() == msg.StationName && choice.CurrentTrack().Title != msg.Song.Title {
newChoice.SetSong(msg.Song)
// Only send notification on selected station
if i == m.selected {
m.notifier.Update(
msg.Song.Title,
msg.Song.Artist,
"",
)
}
}
newChoices[i] = newChoice
}
m.choices = newChoices
return m, tea.Batch(waitForUpdates(m.updates), m.spinner.Tick)
}
func (m model) updateErrMsg(msg errMsg) (tea.Model, tea.Cmd) {
m.errMsg = msg.err.Error()
return m, tea.Batch(waitForUpdates(m.updates), m.spinner.Tick)
}
func (m model) updateSpinner(msg spinner.TickMsg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
m.lastTick = spinner.TickMsg(msg).Time
return m, cmd
}