Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test and clean up PlayQueue #6345

Merged
merged 20 commits into from
May 26, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
imericxu committed May 24, 2021

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit e1a6347c4eef0bc374109b3efcb57562f6a8440b
Original file line number Diff line number Diff line change
@@ -422,34 +422,41 @@ public synchronized void unsetRecovery(final int index) {
}

/**
* Shuffles the current play queue.
* Shuffles the current play queue
* <p>
* This method first backs up the existing play queue and item being played.
* Then a newly shuffled play queue will be generated along with currently
* playing item placed at the beginning of the queue.
* This method first backs up the existing play queue and item being played. Then a newly
* shuffled play queue will be generated along with currently playing item placed at the
* beginning of the queue. This item will also be added to the history.
* </p>
* <p>
* Will emit a {@link ReorderEvent} in any context.
* Will emit a {@link ReorderEvent} if shuffled.
* </p>
*
* @implNote Does nothing if the queue is empty or has a size of 1
*/
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) {
backup = new ArrayList<>(streams);
}
final int originIndex = getIndex();
final PlayQueueItem current = getItem();

final int originalIndex = getIndex();
final PlayQueueItem currentItem = getItem();

Collections.shuffle(streams);

final int newIndex = streams.indexOf(current);
if (newIndex != -1) {
streams.add(0, streams.remove(newIndex));
}
// Move currentItem to the head of the queue
streams.remove(currentItem);
streams.add(0, currentItem);
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));
}

/**