diff --git a/backend/playbackmanager.go b/backend/playbackmanager.go index c105631..e02138f 100644 --- a/backend/playbackmanager.go +++ b/backend/playbackmanager.go @@ -28,10 +28,7 @@ type PlaybackManager struct { 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) + onSongChange []func(nowPlaying *subsonic.Child) onPlayTimeUpdate []func(float64, float64) } @@ -98,7 +95,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, justScrobbledIfAny *subsonic.Child)) { +func (p *PlaybackManager) OnSongChange(cb func(nowPlaying *subsonic.Child)) { p.onSongChange = append(p.onSongChange, cb) } @@ -228,16 +225,15 @@ 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 + song.PlayCount += 1 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) + cb(p.NowPlaying()) } - p.lastScrobbled = nil } func (p *PlaybackManager) startPollTimePos() { diff --git a/ui/bottompanel.go b/ui/bottompanel.go index b21561a..bc03c00 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, _ *subsonic.Child) { +func (bp *BottomPanel) onSongChange(song *subsonic.Child) { if song == nil { bp.NowPlaying.Update("", "", "", nil) } else { diff --git a/ui/browsing/albumpage.go b/ui/browsing/albumpage.go index c5a58f9..d31602f 100644 --- a/ui/browsing/albumpage.go +++ b/ui/browsing/albumpage.go @@ -100,14 +100,15 @@ func (a *AlbumPage) Route() Route { return AlbumRoute(a.albumID) } -func (a *AlbumPage) OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) { +var _ CanShowNowPlaying = (*AlbumPage)(nil) + +func (a *AlbumPage) OnSongChange(song *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 194402a..28fe2d9 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, lastScrobbledIfAny *subsonic.Child) + OnSongChange(song *subsonic.Child) } type BrowsingPane struct { @@ -150,7 +150,7 @@ func (b *BrowsingPane) doSetPage(p Page) bool { b.curPage = p if np, ok := p.(CanShowNowPlaying); ok { // inform page of currently playing track - np.OnSongChange(b.app.PlaybackManager.NowPlaying(), nil) + np.OnSongChange(b.app.PlaybackManager.NowPlaying()) } b.pageContainer.Remove(b.curPage) b.pageContainer.Objects[1] = p @@ -158,12 +158,12 @@ func (b *BrowsingPane) doSetPage(p Page) bool { return true } -func (b *BrowsingPane) onSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) { +func (b *BrowsingPane) onSongChange(song *subsonic.Child) { if b.curPage == nil { return } if p, ok := b.curPage.(CanShowNowPlaying); ok { - p.OnSongChange(song, lastScrobbledIfAny) + p.OnSongChange(song) } } diff --git a/ui/browsing/nowplayingpage.go b/ui/browsing/nowplayingpage.go index 827b334..2a522d0 100644 --- a/ui/browsing/nowplayingpage.go +++ b/ui/browsing/nowplayingpage.go @@ -79,14 +79,15 @@ func (a *NowPlayingPage) SelectAll() { a.tracklist.SelectAll() } -func (a *NowPlayingPage) OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) { +var _ CanShowNowPlaying = (*NowPlayingPage)(nil) + +func (a *NowPlayingPage) OnSongChange(song *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 56ef385..4009354 100644 --- a/ui/browsing/playlistpage.go +++ b/ui/browsing/playlistpage.go @@ -86,14 +86,15 @@ func (a *PlaylistPage) Route() Route { return PlaylistRoute(a.playlistID) } -func (a *PlaylistPage) OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) { +var _ CanShowNowPlaying = (*PlaylistPage)(nil) + +func (a *PlaylistPage) OnSongChange(song *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 52cb85a..a936042 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -64,7 +64,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, _ *subsonic.Child) { + app.PlaybackManager.OnSongChange(func(song *subsonic.Child) { if song == nil { m.Window.SetTitle(appName) return diff --git a/ui/widgets/tracklist.go b/ui/widgets/tracklist.go index d85ed56..bd21779 100644 --- a/ui/widgets/tracklist.go +++ b/ui/widgets/tracklist.go @@ -145,19 +145,6 @@ 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() @@ -360,8 +347,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 || tr.PlayCount != t.playCount { - t.isPlaying = isPlaying + if tr.ID != t.trackID { t.trackID = tr.ID t.playCount = tr.PlayCount @@ -376,7 +362,15 @@ func (t *TrackRow) Update(tr *subsonic.Child, isPlaying bool, rowNum int) { t.year.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(tr.Year) t.plays.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(int(tr.PlayCount)) t.bitrate.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(tr.BitRate) + } + if tr.PlayCount != t.playCount { + t.playCount = tr.PlayCount + t.plays.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(int(tr.PlayCount)) + } + + if isPlaying != t.isPlaying { + t.isPlaying = isPlaying t.name.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying t.artist.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying t.album.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying @@ -384,7 +378,6 @@ func (t *TrackRow) Update(tr *subsonic.Child, isPlaying bool, rowNum int) { t.year.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying t.plays.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying t.bitrate.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying - if isPlaying { t.container.Objects[1].(*fyne.Container).Objects[0] = container.NewCenter(t.playingIcon) } else {