mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-01-11 09:50:32 +00:00
Refactor shuffle and update documentation
- Add early return for invalid sizes to shuffle - Rename variables to be more descriptive - Refactor moving list element, removing unnecessary operations - Unwrap if clause for adding to history because the condition is guaranteed by the guard clause - Inline the value 0 for the ReorderEvent - Update documentation to reflect new changes
This commit is contained in:
parent
bf8e8798d9
commit
e1a6347c4e
@ -422,34 +422,41 @@ public abstract class PlayQueue implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shuffles the current play queue.
|
* Shuffles the current play queue
|
||||||
* <p>
|
* <p>
|
||||||
* This method first backs up the existing play queue and item being played.
|
* This method first backs up the existing play queue and item being played. Then a newly
|
||||||
* Then a newly shuffled play queue will be generated along with currently
|
* shuffled play queue will be generated along with currently playing item placed at the
|
||||||
* playing item placed at the beginning of the queue.
|
* beginning of the queue. This item will also be added to the history.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* Will emit a {@link ReorderEvent} in any context.
|
* Will emit a {@link ReorderEvent} if shuffled.
|
||||||
* </p>
|
* </p>
|
||||||
|
*
|
||||||
|
* @implNote Does nothing if the queue is empty or has a size of 1
|
||||||
*/
|
*/
|
||||||
public synchronized void shuffle() {
|
public synchronized void shuffle() {
|
||||||
|
// Can't shuffle an list that's empty or only has one element
|
||||||
|
if (size() <= 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Create a backup if it doesn't already exist
|
||||||
if (backup == null) {
|
if (backup == null) {
|
||||||
backup = new ArrayList<>(streams);
|
backup = new ArrayList<>(streams);
|
||||||
}
|
}
|
||||||
final int originIndex = getIndex();
|
|
||||||
final PlayQueueItem current = getItem();
|
final int originalIndex = getIndex();
|
||||||
|
final PlayQueueItem currentItem = getItem();
|
||||||
|
|
||||||
Collections.shuffle(streams);
|
Collections.shuffle(streams);
|
||||||
|
|
||||||
final int newIndex = streams.indexOf(current);
|
// Move currentItem to the head of the queue
|
||||||
if (newIndex != -1) {
|
streams.remove(currentItem);
|
||||||
streams.add(0, streams.remove(newIndex));
|
streams.add(0, currentItem);
|
||||||
}
|
|
||||||
queueIndex.set(0);
|
queueIndex.set(0);
|
||||||
if (streams.size() > 0) {
|
|
||||||
history.add(streams.get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
broadcast(new ReorderEvent(originIndex, queueIndex.get()));
|
history.add(currentItem);
|
||||||
|
|
||||||
|
broadcast(new ReorderEvent(originalIndex, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user