From 7237003e33264defd817e3c3a8d6204ff210b67c Mon Sep 17 00:00:00 2001 From: jakobkukla Date: Fri, 18 Mar 2022 01:51:11 +0100 Subject: [PATCH] rework shuffle --- .../gramophone/helper/ShuffleHelper.java | 23 ----- .../gramophone/service/MusicService.java | 16 +--- .../gramophone/service/QueueManager.java | 89 +++++++++++-------- 3 files changed, 56 insertions(+), 72 deletions(-) delete mode 100644 app/src/main/java/com/dkanada/gramophone/helper/ShuffleHelper.java diff --git a/app/src/main/java/com/dkanada/gramophone/helper/ShuffleHelper.java b/app/src/main/java/com/dkanada/gramophone/helper/ShuffleHelper.java deleted file mode 100644 index 5dbc148f..00000000 --- a/app/src/main/java/com/dkanada/gramophone/helper/ShuffleHelper.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.dkanada.gramophone.helper; - -import androidx.annotation.NonNull; - -import com.dkanada.gramophone.model.Song; - -import java.util.Collections; -import java.util.List; - -public class ShuffleHelper { - public static void makeShuffleList(@NonNull List listToShuffle, final int current) { - if (listToShuffle.isEmpty()) return; - - if (current >= 0) { - Song song = listToShuffle.remove(current); - - Collections.shuffle(listToShuffle); - listToShuffle.add(0, song); - } else { - Collections.shuffle(listToShuffle); - } - } -} diff --git a/app/src/main/java/com/dkanada/gramophone/service/MusicService.java b/app/src/main/java/com/dkanada/gramophone/service/MusicService.java index 4107fadd..b7281701 100644 --- a/app/src/main/java/com/dkanada/gramophone/service/MusicService.java +++ b/app/src/main/java/com/dkanada/gramophone/service/MusicService.java @@ -39,7 +39,6 @@ import com.dkanada.gramophone.BuildConfig; import com.dkanada.gramophone.R; import com.dkanada.gramophone.glide.BlurTransformation; import com.dkanada.gramophone.glide.CustomGlideRequest; -import com.dkanada.gramophone.helper.ShuffleHelper; import com.dkanada.gramophone.model.Playlist; import com.dkanada.gramophone.model.Song; import com.dkanada.gramophone.service.notifications.PlayingNotification; @@ -569,19 +568,9 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP uiThreadHandler.post(runnable); } - // FIXME This will be refactored and partly moved to QueueManager in a subsequent PR public void openQueue(@Nullable final List playingQueue, final int startPosition, final boolean startPlaying) { if (playingQueue != null && !playingQueue.isEmpty() && startPosition >= 0 && startPosition < playingQueue.size()) { - queueManager.originalPlayingQueue = new ArrayList<>(playingQueue); - queueManager.playingQueue = new ArrayList<>(queueManager.originalPlayingQueue); - - int position = startPosition; - if (queueManager.getShuffleMode() == QueueManager.SHUFFLE_MODE_SHUFFLE) { - ShuffleHelper.makeShuffleList(queueManager.playingQueue, startPosition); - position = 0; - } - - queueManager.setPlayingQueueAndPosition(queueManager.playingQueue, queueManager.originalPlayingQueue, startPosition); + queueManager.setPlayingQueueAndPosition(playingQueue, startPosition); if (startPlaying) play(); } } @@ -713,7 +702,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP } switch (msg.what) { - case TRACK_ENDED: + /*case TRACK_ENDED: // FIXME This isn't used anywhere. This means releaseWakeLock() is never called // if there is a timer finished, don't continue @@ -727,6 +716,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP service.releaseWakeLock(); break; + */ } } } diff --git a/app/src/main/java/com/dkanada/gramophone/service/QueueManager.java b/app/src/main/java/com/dkanada/gramophone/service/QueueManager.java index 4bd0bf60..0cfc9745 100644 --- a/app/src/main/java/com/dkanada/gramophone/service/QueueManager.java +++ b/app/src/main/java/com/dkanada/gramophone/service/QueueManager.java @@ -3,12 +3,13 @@ package com.dkanada.gramophone.service; import android.content.Context; import com.dkanada.gramophone.App; -import com.dkanada.gramophone.helper.ShuffleHelper; import com.dkanada.gramophone.model.Song; import com.dkanada.gramophone.util.PreferenceUtil; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Optional; public class QueueManager { public static final int REPEAT_MODE_NONE = 0; @@ -21,8 +22,8 @@ public class QueueManager { private final Context context; private final QueueCallbacks callbacks; - List playingQueue = new ArrayList<>(); - List originalPlayingQueue = new ArrayList<>(); + private List playingQueue = new ArrayList<>(); + private List shuffledQueue = new ArrayList<>(); private int position = 0; private int restoredProgress = 0; @@ -36,14 +37,19 @@ public class QueueManager { this.callbacks = callbacks; } - public void setPlayingQueueAndPosition(List queue, List originalPlayingQueue, int position) { + public void setPlayingQueueAndPosition(List queue, int position) { this.position = position; this.playingQueue = new ArrayList<>(queue); - this.originalPlayingQueue = new ArrayList<>(originalPlayingQueue); + this.shuffledQueue = new ArrayList<>(queue); + shuffleQueue(); callbacks.onQueueChanged(); } + public List getPlayingQueue() { + return shuffleMode == SHUFFLE_MODE_SHUFFLE ? shuffledQueue : playingQueue; + } + public int getPosition() { return position; } @@ -88,14 +94,6 @@ public class QueueManager { } } - public boolean isLastTrack() { - return getPosition() == getPlayingQueue().size() - 1; - } - - public List getPlayingQueue() { - return playingQueue; - } - public int getRepeatMode() { return repeatMode; } @@ -130,21 +128,25 @@ public class QueueManager { switch (shuffleMode) { case SHUFFLE_MODE_SHUFFLE: this.shuffleMode = shuffleMode; - ShuffleHelper.makeShuffleList(this.getPlayingQueue(), getPosition()); - position = 0; + shuffleQueue(); + break; case SHUFFLE_MODE_NONE: - this.shuffleMode = shuffleMode; String currentSongId = getCurrentSong().id; - playingQueue = new ArrayList<>(originalPlayingQueue); int newPosition = 0; - for (Song song : getPlayingQueue()) { - if (song.id == currentSongId) { - newPosition = getPlayingQueue().indexOf(song); - } + + Optional currentSong = playingQueue.stream() + .filter(song -> song.id.equals(currentSongId)) + .findFirst(); + + if (currentSong.isPresent()) { + newPosition = playingQueue.indexOf(currentSong.get()); } + shuffledQueue = new ArrayList<>(playingQueue); + position = newPosition; + this.shuffleMode = shuffleMode; break; } @@ -153,9 +155,28 @@ public class QueueManager { callbacks.onQueueChanged(); } + private void shuffleQueue() { + if (shuffleMode == SHUFFLE_MODE_SHUFFLE) { + this.shuffledQueue = new ArrayList<>(playingQueue); + + if (!shuffledQueue.isEmpty()) { + if (getPosition() >= 0) { + Song song = shuffledQueue.remove(getPosition()); + + Collections.shuffle(shuffledQueue); + shuffledQueue.add(0, song); + } else { + Collections.shuffle(shuffledQueue); + } + } + + position = 0; + } + } + public void addSong(int position, Song song) { playingQueue.add(position, song); - originalPlayingQueue.add(position, song); + shuffledQueue.add(position, song); resetCurrentSong = false; callbacks.onQueueChanged(); @@ -163,7 +184,7 @@ public class QueueManager { public void addSong(Song song) { playingQueue.add(song); - originalPlayingQueue.add(song); + shuffledQueue.add(song); resetCurrentSong = false; callbacks.onQueueChanged(); @@ -171,7 +192,7 @@ public class QueueManager { public void addSongs(int position, List songs) { playingQueue.addAll(position, songs); - originalPlayingQueue.addAll(position, songs); + shuffledQueue.addAll(position, songs); resetCurrentSong = false; callbacks.onQueueChanged(); @@ -179,7 +200,7 @@ public class QueueManager { public void addSongs(List songs) { playingQueue.addAll(songs); - originalPlayingQueue.addAll(songs); + shuffledQueue.addAll(songs); resetCurrentSong = false; callbacks.onQueueChanged(); @@ -188,9 +209,9 @@ public class QueueManager { public void removeSong(int position) { if (getShuffleMode() == SHUFFLE_MODE_NONE) { playingQueue.remove(position); - originalPlayingQueue.remove(position); + shuffledQueue.remove(position); } else { - originalPlayingQueue.remove(playingQueue.remove(position)); + playingQueue.remove(shuffledQueue.remove(position)); } int currentPosition = getPosition(); @@ -209,12 +230,8 @@ public class QueueManager { if (from == to) return; final int currentPosition = getPosition(); - Song songToMove = playingQueue.remove(from); - playingQueue.add(to, songToMove); - if (getShuffleMode() == SHUFFLE_MODE_NONE) { - Song tmpSong = originalPlayingQueue.remove(from); - originalPlayingQueue.add(to, tmpSong); - } + Song songToMove = getPlayingQueue().remove(from); + getPlayingQueue().add(to, songToMove); if (from > currentPosition && to <= currentPosition) { position = currentPosition + 1; @@ -230,7 +247,7 @@ public class QueueManager { public void clearQueue() { playingQueue.clear(); - originalPlayingQueue.clear(); + shuffledQueue.clear(); position = 0; callbacks.onQueueChanged(); @@ -264,7 +281,7 @@ public class QueueManager { restoredProgress = PreferenceUtil.getInstance(context).getProgress(); playingQueue = new ArrayList<>(App.getDatabase().queueSongDao().getQueue(0)); - originalPlayingQueue = new ArrayList<>(App.getDatabase().queueSongDao().getQueue(1)); + shuffledQueue = new ArrayList<>(App.getDatabase().queueSongDao().getQueue(1)); shuffleMode = PreferenceUtil.getInstance(context).getShuffle(); repeatMode = PreferenceUtil.getInstance(context).getRepeat(); @@ -283,7 +300,7 @@ public class QueueManager { App.getDatabase().queueSongDao().deleteQueueSongs(); App.getDatabase().queueSongDao().setQueue(new ArrayList<>(playingQueue), 0); - App.getDatabase().queueSongDao().setQueue(new ArrayList<>(originalPlayingQueue), 1); + App.getDatabase().queueSongDao().setQueue(new ArrayList<>(shuffledQueue), 1); } public int getRestoredProgress() {