Skip to content

Commit

Permalink
restore index only if the incoming tracks dont start with currently p…
Browse files Browse the repository at this point in the history
…laying

related #411
  • Loading branch information
sentriz committed Nov 24, 2023
1 parent eab848c commit 8463bee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
18 changes: 11 additions & 7 deletions jukebox/jukebox.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (j *Jukebox) SetPlaylist(items []string) error {
if err := getDecode(j.conn, &playlist, "playlist"); err != nil {
return fmt.Errorf("get playlist: %w", err)
}
currentPlaylistIndex := slices.IndexFunc(playlist, func(pl mpvPlaylistItem) bool {
currentPlayingIndex := slices.IndexFunc(playlist, func(pl mpvPlaylistItem) bool {
return pl.Current
})

Expand All @@ -131,19 +131,18 @@ func (j *Jukebox) SetPlaylist(items []string) error {
}
defer cleanup()

var foundExistingTrack bool
for _, item := range items {
var newPlayingIndex int = -1

Check warning on line 134 in jukebox/jukebox.go

View workflow job for this annotation

GitHub Actions / Lint and test

var-declaration: should omit type int from declaration of var newPlayingIndex; it will be inferred from the right-hand side (revive)
for i, item := range items {
item, _ = filepath.Abs(item)

if currentPlaylistIndex >= 0 && playlist[currentPlaylistIndex].Filename == item {
foundExistingTrack = true
if currentPlayingIndex >= 0 && playlist[currentPlayingIndex].Filename == item {
newPlayingIndex = i
continue // don't add current track to loadlist
}

fmt.Fprintln(tmp, item)
}

if !foundExistingTrack {
if newPlayingIndex < 0 {
if _, err := j.conn.Call("loadlist", tmp.Name(), "replace"); err != nil {
return fmt.Errorf("load list: %w", err)
}
Expand All @@ -156,6 +155,11 @@ func (j *Jukebox) SetPlaylist(items []string) error {
if _, err := j.conn.Call("loadlist", tmp.Name(), "append-play"); err != nil {
return fmt.Errorf("load list: %w", err)
}
if newPlayingIndex > 0 {
if _, err := j.conn.Call("playlist-move", 0, newPlayingIndex+1); err != nil {
return fmt.Errorf("playlist move: %w", err)
}
}
return nil
}

Expand Down
14 changes: 2 additions & 12 deletions jukebox/jukebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestPlaySkipReset(t *testing.T) {
require.Equal(t, true, status.Playing)

// just add one more by overwriting the playlist like some clients do
// we should move the now play playing index (3) back to the start if we find it
// we should move keep the playing indedx
require.NoError(t, j.SetPlaylist([]string{
"testdata/tr_0.mp3",
"testdata/tr_1.mp3",
Expand All @@ -86,21 +86,11 @@ func TestPlaySkipReset(t *testing.T) {

status, err = j.GetStatus()
require.NoError(t, err)
require.Equal(t, 0, status.CurrentIndex) // index moved to start
require.Equal(t, 3, status.CurrentIndex) // index moved to start
require.Equal(t, testPath("tr_3.mp3"), status.CurrentFilename)
require.Equal(t, 6, status.Length) // we added one more track
require.Equal(t, true, status.Playing)

// skip to index 3 again, which is now tr_2 after re-order
require.NoError(t, j.SkipToPlaylistIndex(3, 0))

status, err = j.GetStatus()
require.NoError(t, err)
require.Equal(t, 3, status.CurrentIndex)
require.Equal(t, testPath("tr_2.mp3"), status.CurrentFilename)
require.Equal(t, 6, status.Length)
require.Equal(t, true, status.Playing)

// new playlist with out current track (tr_2)
require.NoError(t, j.SetPlaylist([]string{
"testdata/tr_6.mp3",
Expand Down

0 comments on commit 8463bee

Please sign in to comment.