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:
@@ -28,7 +28,10 @@ type PlaybackManager struct {
|
|||||||
playQueue []*subsonic.Child
|
playQueue []*subsonic.Child
|
||||||
nowPlayingIdx int64
|
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)
|
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.
|
// 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)
|
p.onSongChange = append(p.onSongChange, cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,15 +228,16 @@ func (p *PlaybackManager) checkScrobble(playDur time.Duration) {
|
|||||||
song := p.playQueue[p.nowPlayingIdx]
|
song := p.playQueue[p.nowPlayingIdx]
|
||||||
if playDur.Seconds()/p.curTrackTime > ScrobbleThreshold {
|
if playDur.Seconds()/p.curTrackTime > ScrobbleThreshold {
|
||||||
log.Printf("Scrobbling %q", song.Title)
|
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)})
|
p.sm.Server.Scrobble(song.ID, map[string]string{"time": strconv.FormatInt(time.Now().Unix()*1000, 10)})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PlaybackManager) invokeOnSongChangeCallbacks() {
|
func (p *PlaybackManager) invokeOnSongChangeCallbacks() {
|
||||||
for _, cb := range p.onSongChange {
|
for _, cb := range p.onSongChange {
|
||||||
cb(p.NowPlaying())
|
cb(p.NowPlaying(), p.lastScrobbled)
|
||||||
}
|
}
|
||||||
|
p.lastScrobbled = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PlaybackManager) startPollTimePos() {
|
func (p *PlaybackManager) startPollTimePos() {
|
||||||
|
|||||||
+1
-1
@@ -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 {
|
if song == nil {
|
||||||
bp.NowPlaying.Update("", "", "", nil)
|
bp.NowPlaying.Update("", "", "", nil)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -100,15 +100,14 @@ func (a *AlbumPage) Route() Route {
|
|||||||
return AlbumRoute(a.albumID)
|
return AlbumRoute(a.albumID)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ CanShowNowPlaying = (*AlbumPage)(nil)
|
func (a *AlbumPage) OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) {
|
||||||
|
|
||||||
func (a *AlbumPage) OnSongChange(song *subsonic.Child) {
|
|
||||||
if song == nil {
|
if song == nil {
|
||||||
a.nowPlayingID = ""
|
a.nowPlayingID = ""
|
||||||
} else {
|
} else {
|
||||||
a.nowPlayingID = song.ID
|
a.nowPlayingID = song.ID
|
||||||
}
|
}
|
||||||
a.tracklist.SetNowPlaying(a.nowPlayingID)
|
a.tracklist.SetNowPlaying(a.nowPlayingID)
|
||||||
|
a.tracklist.IncrementPlayCount(lastScrobbledIfAny)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AlbumPage) Reload() {
|
func (a *AlbumPage) Reload() {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ type CanSelectAll interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CanShowNowPlaying interface {
|
type CanShowNowPlaying interface {
|
||||||
OnSongChange(song *subsonic.Child)
|
OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child)
|
||||||
}
|
}
|
||||||
|
|
||||||
type BrowsingPane struct {
|
type BrowsingPane struct {
|
||||||
@@ -150,7 +150,7 @@ func (b *BrowsingPane) doSetPage(p Page) bool {
|
|||||||
b.curPage = p
|
b.curPage = p
|
||||||
if np, ok := p.(CanShowNowPlaying); ok {
|
if np, ok := p.(CanShowNowPlaying); ok {
|
||||||
// inform page of currently playing track
|
// inform page of currently playing track
|
||||||
np.OnSongChange(b.app.PlaybackManager.NowPlaying())
|
np.OnSongChange(b.app.PlaybackManager.NowPlaying(), nil)
|
||||||
}
|
}
|
||||||
b.pageContainer.Remove(b.curPage)
|
b.pageContainer.Remove(b.curPage)
|
||||||
b.pageContainer.Objects[1] = p
|
b.pageContainer.Objects[1] = p
|
||||||
@@ -158,12 +158,12 @@ func (b *BrowsingPane) doSetPage(p Page) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BrowsingPane) onSongChange(song *subsonic.Child) {
|
func (b *BrowsingPane) onSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) {
|
||||||
if b.curPage == nil {
|
if b.curPage == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if p, ok := b.curPage.(CanShowNowPlaying); ok {
|
if p, ok := b.curPage.(CanShowNowPlaying); ok {
|
||||||
p.OnSongChange(song)
|
p.OnSongChange(song, lastScrobbledIfAny)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,15 +79,14 @@ func (a *NowPlayingPage) SelectAll() {
|
|||||||
a.tracklist.SelectAll()
|
a.tracklist.SelectAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ CanShowNowPlaying = (*NowPlayingPage)(nil)
|
func (a *NowPlayingPage) OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) {
|
||||||
|
|
||||||
func (a *NowPlayingPage) OnSongChange(song *subsonic.Child) {
|
|
||||||
if song == nil {
|
if song == nil {
|
||||||
a.nowPlayingID = ""
|
a.nowPlayingID = ""
|
||||||
} else {
|
} else {
|
||||||
a.nowPlayingID = song.ID
|
a.nowPlayingID = song.ID
|
||||||
}
|
}
|
||||||
a.tracklist.SetNowPlaying(a.nowPlayingID)
|
a.tracklist.SetNowPlaying(a.nowPlayingID)
|
||||||
|
a.tracklist.IncrementPlayCount(lastScrobbledIfAny)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *NowPlayingPage) Reload() {
|
func (a *NowPlayingPage) Reload() {
|
||||||
|
|||||||
@@ -86,15 +86,14 @@ func (a *PlaylistPage) Route() Route {
|
|||||||
return PlaylistRoute(a.playlistID)
|
return PlaylistRoute(a.playlistID)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ CanShowNowPlaying = (*PlaylistPage)(nil)
|
func (a *PlaylistPage) OnSongChange(song *subsonic.Child, lastScrobbledIfAny *subsonic.Child) {
|
||||||
|
|
||||||
func (a *PlaylistPage) OnSongChange(song *subsonic.Child) {
|
|
||||||
if song == nil {
|
if song == nil {
|
||||||
a.nowPlayingID = ""
|
a.nowPlayingID = ""
|
||||||
} else {
|
} else {
|
||||||
a.nowPlayingID = song.ID
|
a.nowPlayingID = song.ID
|
||||||
}
|
}
|
||||||
a.tracklist.SetNowPlaying(a.nowPlayingID)
|
a.tracklist.SetNowPlaying(a.nowPlayingID)
|
||||||
|
a.tracklist.IncrementPlayCount(lastScrobbledIfAny)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *PlaylistPage) Reload() {
|
func (a *PlaylistPage) Reload() {
|
||||||
|
|||||||
+1
-1
@@ -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.container = container.NewBorder(nil, m.BottomPanel, nil, nil, m.BrowsingPane)
|
||||||
m.Window.SetContent(m.container)
|
m.Window.SetContent(m.container)
|
||||||
m.Window.Resize(size)
|
m.Window.Resize(size)
|
||||||
app.PlaybackManager.OnSongChange(func(song *subsonic.Child) {
|
app.PlaybackManager.OnSongChange(func(song *subsonic.Child, _ *subsonic.Child) {
|
||||||
if song == nil {
|
if song == nil {
|
||||||
m.Window.SetTitle(appName)
|
m.Window.SetTitle(appName)
|
||||||
return
|
return
|
||||||
|
|||||||
+16
-9
@@ -145,6 +145,19 @@ func (t *Tracklist) SetNowPlaying(trackID string) {
|
|||||||
t.list.Refresh()
|
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() {
|
func (t *Tracklist) SelectAll() {
|
||||||
t.selectionMgr.SelectAll()
|
t.selectionMgr.SelectAll()
|
||||||
t.Refresh()
|
t.Refresh()
|
||||||
@@ -347,7 +360,8 @@ func NewTrackRow(tracklist *Tracklist, playingIcon fyne.CanvasObject) *TrackRow
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TrackRow) Update(tr *subsonic.Child, isPlaying bool, rowNum int) {
|
func (t *TrackRow) Update(tr *subsonic.Child, isPlaying bool, rowNum int) {
|
||||||
if tr.ID != t.trackID {
|
if tr.ID != t.trackID || isPlaying != t.isPlaying || tr.PlayCount != t.playCount {
|
||||||
|
t.isPlaying = isPlaying
|
||||||
t.trackID = tr.ID
|
t.trackID = tr.ID
|
||||||
t.playCount = tr.PlayCount
|
t.playCount = tr.PlayCount
|
||||||
|
|
||||||
@@ -362,15 +376,7 @@ func (t *TrackRow) Update(tr *subsonic.Child, isPlaying bool, rowNum int) {
|
|||||||
t.year.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(tr.Year)
|
t.year.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(tr.Year)
|
||||||
t.plays.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(int(tr.PlayCount))
|
t.plays.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(int(tr.PlayCount))
|
||||||
t.bitrate.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(tr.BitRate)
|
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.name.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying
|
||||||
t.artist.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
|
t.album.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying
|
||||||
@@ -378,6 +384,7 @@ func (t *TrackRow) Update(tr *subsonic.Child, isPlaying bool, rowNum int) {
|
|||||||
t.year.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying
|
t.year.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying
|
||||||
t.plays.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
|
t.bitrate.Segments[0].(*widget.TextSegment).Style.TextStyle.Bold = isPlaying
|
||||||
|
|
||||||
if isPlaying {
|
if isPlaying {
|
||||||
t.container.Objects[1].(*fyne.Container).Objects[0] = container.NewCenter(t.playingIcon)
|
t.container.Objects[1].(*fyne.Container).Objects[0] = container.NewCenter(t.playingIcon)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user