Skip to content

Commit

Permalink
Fix #421: properly handle reordering and deleting repeat tracks in queue
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jul 13, 2024
1 parent 69a1900 commit 3f30448
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
8 changes: 4 additions & 4 deletions backend/playbackengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,15 @@ func (p *playbackEngine) UpdatePlayQueue(items []mediaprovider.MediaItem) error
return nil
}

func (p *playbackEngine) RemoveTracksFromQueue(trackIDs []string) {
newQueue := make([]mediaprovider.MediaItem, 0, len(p.playQueue)-len(trackIDs))
idSet := sharedutil.ToSet(trackIDs)
func (p *playbackEngine) RemoveTracksFromQueue(idxs []int) {
newQueue := make([]mediaprovider.MediaItem, 0, len(p.playQueue)-len(idxs))
idxSet := sharedutil.ToSet(idxs)
isPlayingTrackRemoved := false
isNextPlayingTrackremoved := false
nowPlaying := p.NowPlayingIndex()
newNowPlaying := nowPlaying
for i, tr := range p.playQueue {
if _, ok := idSet[tr.Metadata().ID]; ok {
if _, ok := idxSet[i]; ok {
if i < nowPlaying {
// if removing a track earlier than the currently playing one (if any),
// decrement new now playing index by one to account for new position in queue
Expand Down
4 changes: 2 additions & 2 deletions backend/playbackmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ func (p *PlaybackManager) OnTrackRatingChanged(id string, rating int) {
p.engine.OnTrackRatingChanged(id, rating)
}

func (p *PlaybackManager) RemoveTracksFromQueue(trackIDs []string) {
p.engine.RemoveTracksFromQueue(trackIDs)
func (p *PlaybackManager) RemoveTracksFromQueue(idxs []int) {
p.engine.RemoveTracksFromQueue(idxs)
}

// Stop playback and clear the play queue.
Expand Down
13 changes: 3 additions & 10 deletions ui/browsing/nowplayingpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ func NewNowPlayingPage(
a.queueList.OnShowArtistPage = func(artistID string) {
a.contr.NavigateTo(controller.ArtistRoute(artistID))
}
a.queueList.OnRemoveFromQueue = func(trackIDs []string) {
a.queueList.OnRemoveFromQueue = func(idxs []int) {
a.queueList.UnselectAll()
a.pm.RemoveTracksFromQueue(trackIDs)
a.pm.RemoveTracksFromQueue(idxs)
}
a.queueList.OnSetRating = func(trackIDs []string, rating int) {
contr.SetTrackRatings(trackIDs, rating)
Expand Down Expand Up @@ -515,14 +515,7 @@ func (a *NowPlayingPage) Refresh() {
a.BaseWidget.Refresh()
}

func (a *NowPlayingPage) doSetNewTrackOrder(trackIDs []string, insertPos int) {
trackIDSet := sharedutil.ToSet(trackIDs)
idxs := make([]int, 0, len(trackIDs))
for i, tr := range a.queue {
if _, ok := trackIDSet[tr.Metadata().ID]; ok {
idxs = append(idxs, i)
}
}
func (a *NowPlayingPage) doSetNewTrackOrder(idxs []int, insertPos int) {
newTracks := sharedutil.ReorderItems(a.queue, idxs, insertPos)
a.pm.UpdatePlayQueue(newTracks)
}
Expand Down
10 changes: 10 additions & 0 deletions ui/util/tracklistutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func SelectedItemIDs(items []*TrackListModel) []string {
})
}

func SelectedIndexes(items []*TrackListModel) []int {
var selected []int
for i, tm := range items {
if tm.Selected {
selected = append(selected, i)
}
}
return selected
}

func SelectItem(items []*TrackListModel, idx int) {
if items[idx].Selected {
return
Expand Down
16 changes: 11 additions & 5 deletions ui/widgets/playqueuelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ type PlayQueueList struct {
OnAddToPlaylist func(trackIDs []string)
OnSetFavorite func(trackIDs []string, fav bool)
OnSetRating func(trackIDs []string, rating int)
OnRemoveFromQueue func(itemIDs []string)
OnRemoveFromQueue func(idxs []int)
OnDownload func(tracks []*mediaprovider.Track, downloadName string)
OnShare func(tracks []*mediaprovider.Track)
OnShowArtistPage func(artistID string)
OnReorderItems func(itemIDs []string, reorderTo int)
OnReorderItems func(idxs []int, reorderTo int)

useNonQueueMenu bool
menu *widget.PopUpMenu // ctx menu for when only tracks are selected
Expand Down Expand Up @@ -111,7 +111,7 @@ func NewPlayQueueList(im *backend.ImageManager, useNonQueueMenu bool) *PlayQueue
}
p.list.OnDragEnd = func(dragged, insertPos int) {
if p.OnReorderItems != nil {
p.OnReorderItems(p.selectedItemIDs(), insertPos)
p.OnReorderItems(p.selectedIdxs(), insertPos)
}
}

Expand Down Expand Up @@ -309,7 +309,7 @@ func (p *PlayQueueList) ensureTracksMenu() {
if !p.useNonQueueMenu {
remove := fyne.NewMenuItem("Remove from queue", func() {
if p.OnRemoveFromQueue != nil {
p.OnRemoveFromQueue(p.selectedItemIDs())
p.OnRemoveFromQueue(p.selectedIdxs())
}
})
remove.Icon = theme.ContentRemoveIcon()
Expand All @@ -330,7 +330,7 @@ func (p *PlayQueueList) ensureRadiosMenu() {
}
remove := fyne.NewMenuItem("Remove from queue", func() {
if p.OnRemoveFromQueue != nil {
p.OnRemoveFromQueue(p.selectedItemIDs())
p.OnRemoveFromQueue(p.selectedIdxs())
}
})
remove.Icon = theme.ContentRemoveIcon()
Expand Down Expand Up @@ -386,6 +386,12 @@ func (t *PlayQueueList) selectedItemIDs() []string {
return util.SelectedItemIDs(t.items)
}

func (t *PlayQueueList) selectedIdxs() []int {
t.tracksMutex.RLock()
defer t.tracksMutex.RUnlock()
return util.SelectedIndexes(t.items)
}

func (p *PlayQueueList) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(p.list)
}
Expand Down

0 comments on commit 3f30448

Please sign in to comment.