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