1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-03-29 23:07:01 +00:00

slightly optimized model

This commit is contained in:
zelva 2022-09-02 21:27:37 +03:00 committed by TobiGr
parent 6456e0c3f2
commit 3513f7ac03

View File

@ -283,9 +283,10 @@ public abstract class PlayQueue implements Serializable {
*
* @param items {@link PlayQueueItem}s to append
*/
public synchronized void append(@NonNull final List<PlayQueueItem> items) {
public void append(@NonNull final List<PlayQueueItem> items) {
final List<PlayQueueItem> itemList = new ArrayList<>(items);
synchronized (this) {
if (isShuffled()) {
backup.addAll(itemList);
Collections.shuffle(itemList);
@ -298,6 +299,7 @@ public abstract class PlayQueue implements Serializable {
broadcast(new AppendEvent(itemList.size()));
}
}
/**
* Removes the item at the given index from the play queue.
@ -327,12 +329,12 @@ public abstract class PlayQueue implements Serializable {
* </p>
*/
public synchronized void error() {
final int oldIndex = getIndex();
queueIndex.incrementAndGet();
if (streams.size() > queueIndex.get()) {
history.add(streams.get(queueIndex.get()));
final int oldIndex = queueIndex.getAndIncrement();
final int nextIndex = oldIndex + 1;
if (streams.size() > nextIndex) {
history.add(streams.get(nextIndex));
}
broadcast(new ErrorEvent(oldIndex, getIndex()));
broadcast(new ErrorEvent(oldIndex, nextIndex));
}
private synchronized void removeInternal(final int removeIndex) {
@ -353,9 +355,10 @@ public abstract class PlayQueue implements Serializable {
backup.remove(getItem(removeIndex));
}
final int nextIndex = queueIndex.get();
history.remove(streams.remove(removeIndex));
if (streams.size() > queueIndex.get()) {
history.add(streams.get(queueIndex.get()));
if (streams.size() > nextIndex) {
history.add(streams.get(nextIndex));
}
}
@ -372,10 +375,11 @@ public abstract class PlayQueue implements Serializable {
* @param source the original index of the item
* @param target the new index of the item
*/
public synchronized void move(final int source, final int target) {
public void move(final int source, final int target) {
if (source < 0 || target < 0) {
return;
}
synchronized (this) {
if (source >= streams.size() || target >= streams.size()) {
return;
}
@ -394,6 +398,7 @@ public abstract class PlayQueue implements Serializable {
streams.add(target, playQueueItem);
broadcast(new MoveEvent(source, target));
}
}
/**
* Sets the recovery record of the item at the index.
@ -452,7 +457,7 @@ public abstract class PlayQueue implements Serializable {
}
final int originalIndex = getIndex();
final PlayQueueItem currentItem = getItem();
final PlayQueueItem currentItem = getItem(originalIndex);
Collections.shuffle(streams);
@ -481,22 +486,21 @@ public abstract class PlayQueue implements Serializable {
return;
}
final int originIndex = getIndex();
final PlayQueueItem current = getItem();
final PlayQueueItem current = getItem(originIndex);
streams = backup;
backup = null;
final int newIndex = streams.indexOf(current);
if (newIndex != -1) {
queueIndex.set(newIndex);
} else {
queueIndex.set(0);
}
if (streams.size() > queueIndex.get()) {
history.add(streams.get(queueIndex.get()));
final int nextIndex = newIndex != -1 ? newIndex : 0;
queueIndex.set(nextIndex);
if (streams.size() > nextIndex) {
history.add(streams.get(nextIndex));
}
broadcast(new ReorderEvent(originIndex, queueIndex.get()));
broadcast(new ReorderEvent(originIndex, nextIndex));
}
/**
@ -508,13 +512,14 @@ public abstract class PlayQueue implements Serializable {
* @return true if history is not empty and the item can be played
* */
public synchronized boolean previous() {
if (history.size() <= 1) {
final int sz = history.size();
if (sz <= 1) {
return false;
}
history.remove(history.size() - 1);
history.remove(sz - 1);
final PlayQueueItem last = history.remove(history.size() - 1);
final PlayQueueItem last = history.remove(sz - 1);
setIndex(indexOf(last));
return true;