From f7f6bcd546f6115268599d0be65f736e3c91b3fb Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Mon, 13 Feb 2023 17:04:47 -0800 Subject: [PATCH] ensure play count in tracklist updates after scrobble --- backend/playbackmanager.go | 32 +++++++++++++++++++------------- ui/bottompanel.go | 2 +- ui/browsing/albumpage.go | 3 ++- ui/browsing/browsingpane.go | 9 +++++---- ui/browsing/nowplayingpage.go | 3 ++- ui/browsing/playlistpage.go | 3 ++- ui/mainwindow.go | 2 +- ui/widgets/tracklist.go | 16 +++++++++++++++- 8 files changed, 47 insertions(+), 23 deletions(-) diff --git a/backend/playbackmanager.go b/backend/playbackmanager.go index fd22518..0f4d43a 100644 --- a/backend/playbackmanager.go +++ b/backend/playbackmanager.go @@ -25,9 +25,13 @@ type PlaybackManager struct { playTimeStopwatch util.Stopwatch curTrackTime float64 - playQueue []*subsonic.Child - nowPlayingIdx int64 - onSongChange []func(*subsonic.Child) + playQueue []*subsonic.Child + nowPlayingIdx int64 + + // to pass to onSongChange listeners; clear once listeners have been called + lastScrobbled *subsonic.Child + + onSongChange []func(nowPlaying *subsonic.Child, justScrobbledIfAny *subsonic.Child) onPlayTimeUpdate []func(float64, float64) } @@ -51,9 +55,7 @@ func NewPlaybackManager(ctx context.Context, s *ServerManager, p *player.Player) } pm.nowPlayingIdx = tracknum pm.curTrackTime = float64(pm.playQueue[pm.nowPlayingIdx].Duration) - for _, cb := range pm.onSongChange { - cb(pm.NowPlaying()) - } + pm.invokeOnSongChangeCallbacks() pm.doUpdateTimePos() }) p.OnSeek(func() { @@ -65,9 +67,7 @@ func NewPlaybackManager(ctx context.Context, s *ServerManager, p *player.Player) pm.playTimeStopwatch.Reset() pm.stopPollTimePos() pm.doUpdateTimePos() - for _, cb := range pm.onSongChange { - cb(nil) - } + pm.invokeOnSongChangeCallbacks() }) p.OnPaused(func() { pm.playTimeStopwatch.Stop() @@ -98,7 +98,7 @@ func (p *PlaybackManager) NowPlaying() *subsonic.Child { } // Sets a callback that is notified whenever a new song begins playing. -func (p *PlaybackManager) OnSongChange(cb func(*subsonic.Child)) { +func (p *PlaybackManager) OnSongChange(cb func(nowPlaying *subsonic.Child, justScrobbledIfAny *subsonic.Child)) { p.onSongChange = append(p.onSongChange, cb) } @@ -203,9 +203,7 @@ func (p *PlaybackManager) RemoveTracksFromQueue(trackIdxs []int) { p.nowPlayingIdx = p.player.GetStatus().PlaylistPos // fire on song change callbacks in case the playing track was removed // TODO: only call this if the playing track actually was removed - for _, cb := range p.onSongChange { - cb(p.NowPlaying()) - } + p.invokeOnSongChangeCallbacks() } // Stop playback and clear the play queue. @@ -226,10 +224,18 @@ func (p *PlaybackManager) checkScrobble(playDur time.Duration) { song := p.playQueue[p.nowPlayingIdx] if playDur.Seconds()/p.curTrackTime > ScrobbleThreshold { log.Printf("Scrobbling %q", song.Title) + p.lastScrobbled = song p.sm.Server.Scrobble(song.ID, map[string]string{"time": strconv.FormatInt(time.Now().Unix()*1000, 10)}) } } +func (p *PlaybackManager) invokeOnSongChangeCallbacks() { + for _, cb := range p.onSongChange { + cb(p.NowPlaying(), p.lastScrobbled) + } + p.lastScrobbled = nil +} + func (p *PlaybackManager) startPollTimePos() { ctx, cancel := context.WithCancel(p.ctx) p.cancelPollPos = cancel diff --git a/ui/bottompanel.go b/ui/bottompanel.go index bf00fb5..6e60aaa 100644 --- a/ui/bottompanel.go +++ b/ui/bottompanel.go @@ -84,7 +84,7 @@ func (bp *BottomPanel) SetPlaybackManager(pm *backend.PlaybackManager) { }) } -func (bp *BottomPanel) onSongChange(song *subsonic.Child) { +func (bp *BottomPanel) onSongChange(song *subsonic.Child, _ *subsonic.Child) { if song == nil { bp.NowPlaying.Update("", "", "", nil) } else { diff --git a/ui/browsing/albumpage.go b/ui/browsing/albumpage.go index 5abc2e7..32dac31 100644 --- a/ui/browsing/albumpage.go +++ b/ui/browsing/albumpage.go @@ -93,13 +93,14 @@ func (a *AlbumPage) Route() Route { return AlbumRoute(a.albumID) } -func (a *AlbumPage) OnSongChange(song *subsonic.Child) { +func (a *AlbumPage) OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) { if song == nil { a.nowPlayingID = "" } else { a.nowPlayingID = song.ID } a.tracklist.SetNowPlaying(a.nowPlayingID) + a.tracklist.IncrementPlayCount(lastScrobbledIfAny) } func (a *AlbumPage) Reload() { diff --git a/ui/browsing/browsingpane.go b/ui/browsing/browsingpane.go index eb31798..a13754c 100644 --- a/ui/browsing/browsingpane.go +++ b/ui/browsing/browsingpane.go @@ -36,7 +36,7 @@ type CanSelectAll interface { } type CanShowNowPlaying interface { - OnSongChange(song *subsonic.Child) + OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) } type BrowsingPane struct { @@ -142,7 +142,8 @@ func (b *BrowsingPane) doSetPage(p Page) bool { } b.curPage = p if np, ok := p.(CanShowNowPlaying); ok { - np.OnSongChange(b.app.PlaybackManager.NowPlaying()) + // inform page of currently playing track + np.OnSongChange(b.app.PlaybackManager.NowPlaying(), nil) } b.pageContainer.Remove(b.curPage) b.pageContainer.Objects[1] = p @@ -150,12 +151,12 @@ func (b *BrowsingPane) doSetPage(p Page) bool { return true } -func (b *BrowsingPane) onSongChange(song *subsonic.Child) { +func (b *BrowsingPane) onSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) { if b.curPage == nil { return } if p, ok := b.curPage.(CanShowNowPlaying); ok { - p.OnSongChange(song) + p.OnSongChange(song, lastScrobbledIfAny) } } diff --git a/ui/browsing/nowplayingpage.go b/ui/browsing/nowplayingpage.go index 5b4c431..377cff4 100644 --- a/ui/browsing/nowplayingpage.go +++ b/ui/browsing/nowplayingpage.go @@ -77,13 +77,14 @@ func (a *NowPlayingPage) SelectAll() { a.tracklist.SelectAll() } -func (a *NowPlayingPage) OnSongChange(song *subsonic.Child) { +func (a *NowPlayingPage) OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) { if song == nil { a.nowPlayingID = "" } else { a.nowPlayingID = song.ID } a.tracklist.SetNowPlaying(a.nowPlayingID) + a.tracklist.IncrementPlayCount(lastScrobbledIfAny) } func (a *NowPlayingPage) Reload() { diff --git a/ui/browsing/playlistpage.go b/ui/browsing/playlistpage.go index da02e03..287fa69 100644 --- a/ui/browsing/playlistpage.go +++ b/ui/browsing/playlistpage.go @@ -84,13 +84,14 @@ func (a *PlaylistPage) Route() Route { return PlaylistRoute(a.playlistID) } -func (a *PlaylistPage) OnSongChange(song *subsonic.Child) { +func (a *PlaylistPage) OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) { if song == nil { a.nowPlayingID = "" } else { a.nowPlayingID = song.ID } a.tracklist.SetNowPlaying(a.nowPlayingID) + a.tracklist.IncrementPlayCount(lastScrobbledIfAny) } func (a *PlaylistPage) Reload() { diff --git a/ui/mainwindow.go b/ui/mainwindow.go index f394ea0..123ffa0 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -54,7 +54,7 @@ func NewMainWindow(fyneApp fyne.App, appName string, app *backend.App, size fyne m.container = container.NewBorder(nil, m.BottomPanel, nil, nil, m.BrowsingPane) m.Window.SetContent(m.container) m.Window.Resize(size) - app.PlaybackManager.OnSongChange(func(song *subsonic.Child) { + app.PlaybackManager.OnSongChange(func(song *subsonic.Child, _ *subsonic.Child) { if song == nil { m.Window.SetTitle(appName) return diff --git a/ui/widgets/tracklist.go b/ui/widgets/tracklist.go index 9f7c29d..b2d1f11 100644 --- a/ui/widgets/tracklist.go +++ b/ui/widgets/tracklist.go @@ -105,6 +105,19 @@ func (t *Tracklist) SetNowPlaying(trackID string) { t.list.Refresh() } +func (t *Tracklist) IncrementPlayCount(track *subsonic.Child) { + if track == nil { + return + } + for _, tr := range t.Tracks { + if tr.ID == track.ID { + tr.PlayCount += 1 + t.Refresh() + return + } + } +} + func (t *Tracklist) SelectAll() { t.selectionMgr.SelectAll() t.Refresh() @@ -229,6 +242,7 @@ type TrackRow struct { trackIdx int trackID string isPlaying bool + playCount int64 tappedAt int64 // unixMillis num *widget.RichText @@ -275,7 +289,7 @@ func NewTrackRow(tracklist *Tracklist, playingIcon fyne.CanvasObject) *TrackRow } func (t *TrackRow) Update(tr *subsonic.Child, isPlaying bool, rowNum int) { - if tr.ID == t.trackID && isPlaying == t.isPlaying { + if tr.ID == t.trackID && isPlaying == t.isPlaying && tr.PlayCount == t.playCount { return } t.isPlaying = isPlaying