Skip to content

Commit

Permalink
Merge pull request #406 from dweymouth/drag-and-drop-reorder
Browse files Browse the repository at this point in the history
Enable drag and drop reordering of playlists and the play queue
  • Loading branch information
dweymouth authored Jun 23, 2024
2 parents 5d4ad81 + ee38c36 commit a36be97
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 195 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/Microsoft/go-winio v0.6.2
github.com/cenkalti/dominantcolor v1.0.2
github.com/deluan/sanitize v0.0.0-20230310221930-6e18967d9fc1
github.com/dweymouth/fyne-advanced-list v0.0.0-20240623145729-9c6b8f99bcfe
github.com/dweymouth/fyne-lyrics v0.0.0-20240528234907-15eee7ce5e64
github.com/dweymouth/go-jellyfin v0.0.0-20240517151952-5ceca61cb645
github.com/dweymouth/go-mpv v0.0.0-20230406003141-7f1858e503ee
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ github.com/deluan/sanitize v0.0.0-20230310221930-6e18967d9fc1 h1:mGvOb3zxl4vCLv+
github.com/deluan/sanitize v0.0.0-20230310221930-6e18967d9fc1/go.mod h1:ZNCLJfehvEf34B7BbLKjgpsL9lyW7q938w/GY1XgV4E=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/dweymouth/fyne-advanced-list v0.0.0-20240623145729-9c6b8f99bcfe h1:owGwqph+Y+PqjDiWjZjOFOhlo8QsLs+LHrHojaaBo34=
github.com/dweymouth/fyne-advanced-list v0.0.0-20240623145729-9c6b8f99bcfe/go.mod h1:sbOhla4VcfFb4OjXiUFTLXMPTnhRUlVrDMhB8HtWR4o=
github.com/dweymouth/fyne-lyrics v0.0.0-20240528234907-15eee7ce5e64 h1:RUIrnGY034rDMlcOui/daurwX5b+52KdUKhH9aXaDSg=
github.com/dweymouth/fyne-lyrics v0.0.0-20240528234907-15eee7ce5e64/go.mod h1:3YrjFDHMlhCsSZ/OvmJCxWm9QHSgOVWZBxnraZz9Z7c=
github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20240604143614-256525c6a602 h1:k3jFLjmAuPJ5ZFNF57szZp8XrLIb6mIdEEGPkm6EZ7Q=
Expand Down
98 changes: 19 additions & 79 deletions sharedutil/sharedutil.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package sharedutil

import (
"math"
"slices"

"github.com/dweymouth/supersonic/backend/mediaprovider"
)

Expand Down Expand Up @@ -106,90 +103,33 @@ func TracksToIDs(tracks []*mediaprovider.Track) []string {
})
}

type TrackReorderOp int

const (
MoveToTop TrackReorderOp = iota
MoveToBottom
MoveUp
MoveDown
)

// Reorder items and return a new track slice.
// idxToMove must contain only valid indexes into tracks, and no repeats
func ReorderItems[T any](items []T, idxToMove []int, op TrackReorderOp) []T {
newItems := make([]T, len(items))
switch op {
case MoveToTop:
topIdx := 0
botIdx := len(idxToMove)
idxToMoveSet := ToSet(idxToMove)
for i, t := range items {
if _, ok := idxToMoveSet[i]; ok {
newItems[topIdx] = t
topIdx++
} else {
newItems[botIdx] = t
botIdx++
}
}
case MoveToBottom:
topIdx := 0
botIdx := len(items) - len(idxToMove)
idxToMoveSet := ToSet(idxToMove)
for i, t := range items {
if _, ok := idxToMoveSet[i]; ok {
newItems[botIdx] = t
botIdx++
} else {
newItems[topIdx] = t
topIdx++
}
}
case MoveUp:
first := firstIdxCanMoveUp(idxToMove)
copy(newItems, items)
for _, i := range idxToMove {
if i < first {
continue
}
newItems[i-1], newItems[i] = newItems[i], newItems[i-1]
func ReorderItems[T any](items []T, idxToMove []int, insertIdx int) []T {
idxToMoveSet := ToSet(idxToMove)

newItems := make([]T, 0, len(items))

// collect items that will end up before the insertion set
i := 0
for ; i < len(items); i++ {
if insertIdx == i {
break
}
case MoveDown:
last := lastIdxCanMoveDown(idxToMove, len(items))
copy(newItems, items)
for i := len(idxToMove) - 1; i >= 0; i-- {
idx := idxToMove[i]
if idx > last {
continue
}
newItems[idx+1], newItems[idx] = newItems[idx], newItems[idx+1]
if _, ok := idxToMoveSet[i]; !ok {
newItems = append(newItems, items[i])
}
}
return newItems
}

func firstIdxCanMoveUp(idxs []int) int {
prevIdx := -1
slices.Sort(idxs)
for _, idx := range idxs {
if idx > prevIdx+1 {
return idx
}
prevIdx = idx
for _, idx := range idxToMove {
newItems = append(newItems, items[idx])
}
return math.MaxInt
}

func lastIdxCanMoveDown(idxs []int, lenSlice int) int {
prevIdx := lenSlice
slices.Sort(idxs)
for i := len(idxs) - 1; i >= 0; i-- {
idx := idxs[i]
if idx < prevIdx-1 {
return idx
for ; i < len(items); i++ {
if _, ok := idxToMoveSet[i]; !ok {
newItems = append(newItems, items[i])
}
prevIdx = idx
}
return -1

return newItems
}
35 changes: 3 additions & 32 deletions sharedutil/sharedutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func Test_ReorderItems(t *testing.T) {

tracks := []*mediaprovider.Track{
{ID: "a"}, // 0
{ID: "b"}, // 1
Expand All @@ -27,7 +28,7 @@ func Test_ReorderItems(t *testing.T) {
{ID: "b"},
{ID: "e"},
}
newTracks := ReorderItems(tracks, idxToMove, MoveToTop)
newTracks := ReorderItems(tracks, idxToMove, 0)
if !tracklistsEqual(t, newTracks, want) {
t.Error("ReorderTracks: MoveToTop order incorrect")
}
Expand All @@ -42,40 +43,10 @@ func Test_ReorderItems(t *testing.T) {
{ID: "c"},
{ID: "f"},
}
newTracks = ReorderItems(tracks, idxToMove, MoveToBottom)
newTracks = ReorderItems(tracks, idxToMove, len(tracks))
if !tracklistsEqual(t, newTracks, want) {
t.Error("ReorderTracks: MoveToBottom order incorrect")
}

// test MoveUp:
idxToMove = []int{0, 1, 3, 5}
want = []*mediaprovider.Track{
{ID: "a"},
{ID: "b"},
{ID: "d"},
{ID: "c"},
{ID: "f"},
{ID: "e"},
}
newTracks = ReorderItems(tracks, idxToMove, MoveUp)
if !tracklistsEqual(t, newTracks, want) {
t.Error("ReorderTracks: MoveUp order incorrect")
}

// test MoveDown:
idxToMove = []int{2, 4, 5}
want = []*mediaprovider.Track{
{ID: "a"},
{ID: "b"},
{ID: "d"},
{ID: "c"},
{ID: "e"},
{ID: "f"},
}
newTracks = ReorderItems(tracks, idxToMove, MoveDown)
if !tracklistsEqual(t, newTracks, want) {
t.Error("ReorderTracks: MoveDown order incorrect")
}
}

func tracklistsEqual(t *testing.T, a, b []*mediaprovider.Track) bool {
Expand Down
1 change: 0 additions & 1 deletion ui/browsing/genrespage.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func NewGenreList(sorting widgets.ListHeaderSort) *GenreList {
},
func(id widget.ListItemID, item fyne.CanvasObject) {
row := item.(*GenreListRow)
a.list.SetItemForID(id, row)
if row.Item != a.genres[id] {
row.EnsureUnfocused()
row.ListItemID = id
Expand Down
5 changes: 3 additions & 2 deletions ui/browsing/nowplayingpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func NewNowPlayingPage(

a.queueList = widgets.NewPlayQueueList(a.im, false)
a.relatedList = widgets.NewPlayQueueList(a.im, true)
a.queueList.Reorderable = true
a.queueList.OnReorderItems = a.doSetNewTrackOrder
a.queueList.OnDownload = contr.ShowDownloadDialog
a.queueList.OnShare = func(tracks []*mediaprovider.Track) {
Expand Down Expand Up @@ -502,15 +503,15 @@ func (a *NowPlayingPage) Refresh() {
a.BaseWidget.Refresh()
}

func (a *NowPlayingPage) doSetNewTrackOrder(trackIDs []string, op sharedutil.TrackReorderOp) {
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)
}
}
newTracks := sharedutil.ReorderItems(a.queue, idxs, op)
newTracks := sharedutil.ReorderItems(a.queue, idxs, insertPos)
a.pm.UpdatePlayQueue(newTracks)
}

Expand Down
42 changes: 23 additions & 19 deletions ui/browsing/playlistpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,16 @@ func newPlaylistPage(
a.tracklist.OnVisibleColumnsChanged = func(cols []string) {
conf.TracklistColumns = cols
}
a.tracklist.OnReorderTracks = a.doSetNewTrackOrder
_, canRate := a.sm.Server.(mediaprovider.SupportsRating)
_, canShare := a.sm.Server.(mediaprovider.SupportsSharing)
remove := fyne.NewMenuItem("Remove from playlist", a.onRemoveSelectedFromPlaylist)
remove.Icon = theme.ContentClearIcon()
a.tracklist.Options = widgets.TracklistOptions{
DisableRating: !canRate,
DisableSharing: !canShare,
AuxiliaryMenuItems: []*fyne.MenuItem{
util.NewReorderTracksSubmenu(a.doSetNewTrackOrder),
remove,
},
Reorderable: true,
DisableRating: !canRate,
DisableSharing: !canShare,
AuxiliaryMenuItems: []*fyne.MenuItem{remove},
}
// connect tracklist actions
a.contr.ConnectTracklistActions(a.tracklist)
Expand All @@ -118,6 +117,7 @@ func (a *PlaylistPage) Save() SavedPage {
p.trackSort = a.tracklist.Sorting()
p.widgetPool.Release(util.WidgetTypePlaylistPageHeader, a.header)
a.tracklist.Clear()
a.tracklist.OnReorderTracks = nil
p.widgetPool.Release(util.WidgetTypeTracklist, a.tracklist)
return &p
}
Expand Down Expand Up @@ -178,28 +178,32 @@ func renumberTracks(tracks []*mediaprovider.Track) {
}
}

func (a *PlaylistPage) doSetNewTrackOrder(op sharedutil.TrackReorderOp) {
func (a *PlaylistPage) doSetNewTrackOrder(ids []string, newPos int) {
// Since the tracklist view may be sorted in a different order than the
// actual running order, we need to get the IDs of the selected tracks
// from the tracklist and convert them to indices in the *original* run order
idSet := sharedutil.ToSet(a.tracklist.SelectedTrackIDs())
idSet := sharedutil.ToSet(ids)
idxs := make([]int, 0, len(idSet))
for i, tr := range a.tracks {
if _, ok := idSet[tr.ID]; ok {
idxs = append(idxs, i)
}
}
newTracks := sharedutil.ReorderItems(a.tracks, idxs, op)
ids := sharedutil.TracksToIDs(newTracks)
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)
a.tracklist.UnselectAll()
}
newTracks := sharedutil.ReorderItems(a.tracks, idxs, newPos)
// we can't block the UI waiting for the server so assume it will succeed
go func() {
ids = sharedutil.TracksToIDs(newTracks)
if err := a.sm.Server.ReplacePlaylistTracks(a.playlistID, ids); err != nil {
log.Printf("error updating playlist: %s", err.Error())
}
}()

renumberTracks(newTracks)
// force-switch back to unsorted view to show new track order
a.tracklist.SetSorting(widgets.TracklistSort{})
a.tracklist.SetTracks(newTracks)
a.tracklist.UnselectAll()
a.tracks = newTracks
}

func (a *PlaylistPage) onRemoveSelectedFromPlaylist() {
Expand Down
1 change: 0 additions & 1 deletion ui/browsing/playlistspage.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ func NewPlaylistList(initialSort widgets.ListHeaderSort) *PlaylistList {
row := item.(*PlaylistListRow)
if row.PlaylistID != a.playlists[id].ID {
row.EnsureUnfocused()
a.list.SetItemForID(id, row)
row.ListItemID = id
row.PlaylistID = a.playlists[id].ID
row.nameLabel.Text = a.playlists[id].Name
Expand Down
1 change: 0 additions & 1 deletion ui/browsing/radiospage.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ func NewRadioList(nowPlayingIDPtr *string) *RadioList {
},
func(id widget.ListItemID, item fyne.CanvasObject) {
row := item.(*RadioListRow)
a.list.SetItemForID(id, row)
changed := false
if row.Item != a.radios[id] {
row.EnsureUnfocused()
Expand Down
13 changes: 0 additions & 13 deletions ui/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"fyne.io/fyne/v2/widget"
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/res"
"github.com/dweymouth/supersonic/sharedutil"
myTheme "github.com/dweymouth/supersonic/ui/theme"
"golang.org/x/net/html"
)
Expand Down Expand Up @@ -227,18 +226,6 @@ func NewRatingSubmenu(onSetRating func(int)) *fyne.MenuItem {
return ratingMenu
}

func NewReorderTracksSubmenu(onReorderTracks func(sharedutil.TrackReorderOp)) *fyne.MenuItem {
reorderMenu := fyne.NewMenuItem("Reorder tracks", nil)
reorderMenu.Icon = myTheme.SortIcon
reorderMenu.ChildMenu = fyne.NewMenu("", []*fyne.MenuItem{
fyne.NewMenuItem("Move to top", func() { onReorderTracks(sharedutil.MoveToTop) }),
fyne.NewMenuItem("Move up", func() { onReorderTracks(sharedutil.MoveUp) }),
fyne.NewMenuItem("Move down", func() { onReorderTracks(sharedutil.MoveDown) }),
fyne.NewMenuItem("Move to bottom", func() { onReorderTracks(sharedutil.MoveToBottom) }),
}...)
return reorderMenu
}

func AddHeaderBackground(obj fyne.CanvasObject) *fyne.Container {
return AddHeaderBackgroundWithColorName(obj, myTheme.ColorNamePageHeader)
}
Expand Down
1 change: 0 additions & 1 deletion ui/widgets/albumfilterbutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ func NewGenreFilterSubsection(onChanged func([]string), initialSelectedGenres []
_, selected := g.selectedGenres[genre]
g.selectedGenresMutex.RUnlock()
row := obj.(*genreListViewRow)
g.genreListView.SetItemForID(id, row)
row.ListItemID = id
row.Content.(*widget.Check).Text = genre
row.Content.(*widget.Check).Checked = selected
Expand Down
Loading

0 comments on commit a36be97

Please sign in to comment.