Skip to content

Commit

Permalink
Remember which station the previous track belongs to when switching s…
Browse files Browse the repository at this point in the history
…tations

This is a hacky fix for #21. The data binding is a mess and needs
addressed in #22.

Fixes #21
  • Loading branch information
nlowe committed Aug 31, 2020
1 parent 7d3797e commit 7e6e721
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
8 changes: 8 additions & 0 deletions mousiki/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mousiki

import "github.com/nlowe/mousiki/pandora"

type MessageTrackChanged struct {
Track *pandora.Track
Station pandora.Station
}
8 changes: 4 additions & 4 deletions mousiki/station_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type StationController struct {
queue []pandora.Track

skip chan struct{}
notifications chan *pandora.Track
notifications chan MessageTrackChanged
stationChanged chan pandora.Station

narrativeCache narrativeCache
Expand All @@ -51,7 +51,7 @@ func NewStationController(c api.Client, p audio.Player) *StationController {
player: p,
station: noStationSelected,

notifications: make(chan *pandora.Track, 1),
notifications: make(chan MessageTrackChanged, 1),
stationChanged: make(chan pandora.Station, 1),

log: logrus.WithFields(logrus.Fields{
Expand Down Expand Up @@ -87,7 +87,7 @@ func (s *StationController) Play(ctx context.Context) {

s.log.WithField("track", s.playing.String()).Info("Playing new track")
select {
case s.notifications <- s.playing:
case s.notifications <- MessageTrackChanged{Track: s.playing, Station: s.station}:
}
s.player.UpdateStream(s.playing.AudioUrl, s.playing.FileGain)
s.stationLock.Unlock()
Expand Down Expand Up @@ -231,6 +231,6 @@ func (s *StationController) CurrentStation() pandora.Station {
return s.station
}

func (s *StationController) NotificationChan() <-chan *pandora.Track {
func (s *StationController) NotificationChan() <-chan MessageTrackChanged {
return s.notifications
}
2 changes: 1 addition & 1 deletion mousiki/station_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestStationController_Play(t *testing.T) {
p.On("UpdateStream", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
url := args.String(0)
played = append(played, url)
assert.Equal(t, url, (<-sut.NotificationChan()).AudioUrl)
assert.Equal(t, url, (<-sut.NotificationChan()).Track.AudioUrl)

if len(played) == 3 {
cancel()
Expand Down
19 changes: 9 additions & 10 deletions mousiki/ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type mainWindow struct {
stationPicker *stationPicker
narrativePopup *narrativePopup

nowPlaying *pandora.Track
nowPlaying mousiki.MessageTrackChanged
nowPlayingSong *cview.TextView
nowPlayingArtist *cview.TextView
nowPlayingAlbum *cview.TextView
Expand Down Expand Up @@ -200,6 +200,7 @@ func (w *mainWindow) HandleKey(app *cview.Application) func(ev *tcell.EventKey)
w.log.WithError(err).Error("Failed to add feedback")
}

// Update NowPlaying with the same message to pick up the feedback
w.updateNowPlaying(app, w.nowPlaying)
} else if ev.Key() == tcell.KeyRune && ev.Rune() == 't' {
if err := w.controller.ProvideFeedback(pandora.TrackRatingTired); err != nil {
Expand Down Expand Up @@ -308,19 +309,17 @@ func (w *mainWindow) updateProgress(app *cview.Application, p audio.PlaybackProg
})
}

func (w *mainWindow) updateNowPlaying(app *cview.Application, t *pandora.Track) {
func (w *mainWindow) updateNowPlaying(app *cview.Application, m mousiki.MessageTrackChanged) {
app.QueueUpdateDraw(func() {
station := w.controller.CurrentStation()

w.nowPlayingSong.SetText(FormatTrackTitle(t))
w.nowPlayingArtist.SetText(FormatTrackArtist(t))
w.nowPlayingAlbum.SetText(FormatTrackAlbum(t))
w.nowPlayingSong.SetText(FormatTrackTitle(m.Track))
w.nowPlayingArtist.SetText(FormatTrackArtist(m.Track))
w.nowPlayingAlbum.SetText(FormatTrackAlbum(m.Track))

if w.nowPlaying != nil && w.nowPlaying != t {
_, _ = w.history.Write([]byte("\n" + FormatTrack(w.nowPlaying, station)))
if w.nowPlaying.Track != nil && w.nowPlaying.Track != m.Track {
_, _ = w.history.Write([]byte("\n" + FormatTrack(w.nowPlaying.Track, w.nowPlaying.Station)))
}

w.nowPlaying = t
w.nowPlaying = m
})
}

Expand Down

0 comments on commit 7e6e721

Please sign in to comment.