« get me outta code hell

Fix songs from before insert index queuing wrongly - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-09-06 15:49:04 -0300
committerFlorrie <towerofnix@gmail.com>2019-09-06 15:49:04 -0300
commit366b6e90f1d237a8e1e0d92224f65990157d0cd6 (patch)
tree021b43998820013cde5e21344c9640b98e62dd28
parentd51c3b00e4b097dbae0707d1ef14dc41298ad0a1 (diff)
Fix songs from before insert index queuing wrongly
Now /that/ was hard to fit in the commit line length. (:
-rw-r--r--backend.js17
-rw-r--r--todo.txt5
2 files changed, 20 insertions, 2 deletions
diff --git a/backend.js b/backend.js
index d88c779..0ddd15b 100644
--- a/backend.js
+++ b/backend.js
@@ -140,6 +140,10 @@ class Backend extends EventEmitter {
     // a whole group can be queued in order after a given item.
     let grouplikeOffset = 0
 
+    // Keeps track of how many tracks have been removed (times -1); this is
+    // used so we queue tracks at the intended spot.
+    let removeOffset = 0
+
     const recursivelyAddTracks = item => {
       // For groups, just queue all children.
       if (isGroup(item)) {
@@ -163,13 +167,22 @@ class Backend extends EventEmitter {
         if (!movePlayingTrack && item === this.playingTrack) {
           return
         }
-        items.splice(items.indexOf(item), 1)
+
+        const removeIndex = items.indexOf(item)
+        items.splice(removeIndex, 1)
+
+        // If the item we removed was positioned before the insertion index,
+        // we need to shift that index back one, so it's placed after the same
+        // intended track.
+        if (removeIndex <= afterIndex) {
+          removeOffset--
+        }
       }
 
       if (afterItem === 'FRONT') {
         items.unshift(item)
       } else if (afterItem) {
-        items.splice(afterIndex + 1 + grouplikeOffset, 0, item)
+        items.splice(afterIndex + 1 + grouplikeOffset + removeOffset, 0, item)
       } else {
         items.push(item)
       }
diff --git a/todo.txt b/todo.txt
index af17769..216a488 100644
--- a/todo.txt
+++ b/todo.txt
@@ -283,3 +283,8 @@ TODO: Remember the scroll position of each group. This should probably be done
 TODO: Apparently shift-up/down selecting doesn't keep the selected item within
       the scroll view. Fix that.
       (Done!)
+
+TODO: When you queue a song which is already before the current song / insert
+      index in the queue, it ends up being placed one index closer to the end
+      than intended. Fix this!
+      (Done!)