renumber correctly after removing tracks from playlist

This commit is contained in:
Drew Weymouth
2023-06-03 08:40:49 -07:00
parent f17c48efa0
commit 17f9370e0a
+11 -6
View File
@@ -132,12 +132,7 @@ func (a *PlaylistPage) load() {
log.Printf("Failed to get playlist: %s", err.Error())
return
}
// Playlists, like albums, have a sequential running order. We want the number column to
// represent the track's original position in the playlist, if the user applies a sort.
// Re-purpose the TrackNumber field for the track's position in the playlist, rather than its parent album.
for i, tr := range playlist.Tracks {
tr.TrackNumber = i + 1
}
renumberTracks(playlist.Tracks)
a.tracks = playlist.Tracks
a.tracklist.SetTracks(playlist.Tracks)
a.tracklist.SetNowPlaying(a.nowPlayingID)
@@ -145,6 +140,15 @@ func (a *PlaylistPage) load() {
a.header.Update(playlist)
}
func renumberTracks(tracks []*mediaprovider.Track) {
// Playlists, like albums, have a sequential running order. We want the number column to
// represent the track's original position in the playlist, if the user applies a sort.
// Re-purpose the TrackNumber field for the track's position in the playlist, rather than its parent album.
for i, tr := range tracks {
tr.TrackNumber = i + 1
}
}
func (a *PlaylistPage) onMoveSelectedToTop() {
a.doSetNewTrackOrder(sharedutil.MoveToTop)
}
@@ -177,6 +181,7 @@ func (a *PlaylistPage) doSetNewTrackOrder(op sharedutil.TrackReorderOp) {
if err := a.sm.Server.ReplacePlaylistTracks(a.playlistID, ids); err != nil {
log.Printf("error updating playlist: %s", err.Error())
} else {
renumberTracks(newTracks)
// force-switch back to unsorted view to show new track order
a.tracklist.SetSorting(widgets.TracklistSort{})
a.tracklist.SetTracks(newTracks)