From 8ca5522971f7ff894abb48a900ad64f9f39cdd91 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Thu, 24 Dec 2015 20:24:29 +0100 Subject: [PATCH] Fixed a bug in the playing queue where it would get messed up --- .../gramophone/service/MusicService.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java b/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java index 3dfbcda1..e983925c 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java +++ b/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java @@ -615,19 +615,40 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP } else { originalPlayingQueue.remove(playingQueue.remove(position)); } + + rePosition(position); + notifyChange(QUEUE_CHANGED); } public void removeSong(@NonNull Song song) { for (int i = 0; i < playingQueue.size(); i++) { - if (playingQueue.get(i).id == song.id) playingQueue.remove(i); + if (playingQueue.get(i).id == song.id) { + playingQueue.remove(i); + rePosition(i); + } } for (int i = 0; i < originalPlayingQueue.size(); i++) { - if (originalPlayingQueue.get(i).id == song.id) originalPlayingQueue.remove(i); + if (originalPlayingQueue.get(i).id == song.id) { + originalPlayingQueue.remove(i); + } } notifyChange(QUEUE_CHANGED); } + private void rePosition(int deletedPosition) { + int currentPosition = getPosition(); + if (deletedPosition < currentPosition) { + setPositionInternal(currentPosition - 1); + } else if (deletedPosition == currentPosition) { + if (playingQueue.size() > deletedPosition) { + setPosition(position); + } else { + setPosition(position - 1); + } + } + } + public void moveSong(int from, int to) { if (from == to) return; final int currentPosition = getPosition(); @@ -638,9 +659,9 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP originalPlayingQueue.add(to, tmpSong); } if (from > currentPosition && to <= currentPosition) { - setPositionInternal(getPosition() + 1); + setPositionInternal(currentPosition + 1); } else if (from < currentPosition && to >= currentPosition) { - setPositionInternal(getPosition() - 1); + setPositionInternal(currentPosition - 1); } else if (from == currentPosition) { setPositionInternal(to); } @@ -982,6 +1003,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP case SET_POSITION: service.openTrackAndPrepareNextAt(msg.arg1); + service.notifyChange(PLAY_STATE_CHANGED); break; case PREPARE_NEXT: