Revert "refactor play count update to be cleaner, code-wise"

This reverts commit cd52a893d4, which introduced a regression.
Pages with a tracklist may not have the same in-memory model of the track,
so it is necessary to directly inform them of play count updates.
This commit is contained in:
Drew Weymouth
2023-02-21 17:43:06 -08:00
parent cd52a893d4
commit d9ec269191
8 changed files with 36 additions and 28 deletions
+8 -4
View File
@@ -28,7 +28,10 @@ type PlaybackManager struct {
playQueue []*subsonic.Child
nowPlayingIdx int64
onSongChange []func(nowPlaying *subsonic.Child)
// 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)
}
@@ -95,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(nowPlaying *subsonic.Child)) {
func (p *PlaybackManager) OnSongChange(cb func(nowPlaying *subsonic.Child, justScrobbledIfAny *subsonic.Child)) {
p.onSongChange = append(p.onSongChange, cb)
}
@@ -225,15 +228,16 @@ func (p *PlaybackManager) checkScrobble(playDur time.Duration) {
song := p.playQueue[p.nowPlayingIdx]
if playDur.Seconds()/p.curTrackTime > ScrobbleThreshold {
log.Printf("Scrobbling %q", song.Title)
song.PlayCount += 1
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())
cb(p.NowPlaying(), p.lastScrobbled)
}
p.lastScrobbled = nil
}
func (p *PlaybackManager) startPollTimePos() {