#2652 (part of): The in-progress podcast at the front of the queue

should remain at the front.
This commit is contained in:
orionlee 2018-05-18 20:44:39 -07:00
parent bfde3c7315
commit 30f104f40b
2 changed files with 82 additions and 11 deletions

View File

@ -328,7 +328,9 @@ public class DBWriter {
ItemEnqueuePositionCalculator positionCalculator =
new ItemEnqueuePositionCalculator(
new ItemEnqueuePositionCalculator.Options()
.setEnqueueAtFront(UserPreferences.enqueueAtFront()));
.setEnqueueAtFront(UserPreferences.enqueueAtFront())
.setKeepInProgressAtFront(true) // TODO: to expose with preference
);
for (int i = 0; i < itemIds.length; i++) {
if (!itemListContains(queue, itemIds[i])) {
@ -401,6 +403,7 @@ public class DBWriter {
public static class Options {
private boolean enqueueAtFront = false;
private boolean keepInProgressAtFront = false;
public boolean isEnqueueAtFront() {
return enqueueAtFront;
@ -410,6 +413,15 @@ public class DBWriter {
this.enqueueAtFront = enqueueAtFront;
return this;
}
public boolean isKeepInProgressAtFront() {
return keepInProgressAtFront;
}
public Options setKeepInProgressAtFront(boolean keepInProgressAtFront) {
this.keepInProgressAtFront = keepInProgressAtFront;
return this;
}
}
private final @NonNull Options options;
@ -431,9 +443,16 @@ public class DBWriter {
*/
public int calcPosition(int positionAmongToAdd, FeedItem item, List<FeedItem> curQueue) {
if (options.isEnqueueAtFront()) {
// NOT 0, so that when a list of items are inserted, the items inserted
// keep the same order. Returning 0 will reverse the order
return positionAmongToAdd;
if (options.isKeepInProgressAtFront() &&
curQueue.size() > 0 &&
curQueue.get(0).getMedia() != null &&
curQueue.get(0).getMedia().isInProgress()) {
return positionAmongToAdd + 1; // leave the front in progress item at the front
} else { // typical case
// return NOT 0, so that when a list of items are inserted, the items inserted
// keep the same order. Returning 0 will reverse the order
return positionAmongToAdd;
}
} else {
return curQueue.size();
}

View File

@ -11,6 +11,7 @@ import java.util.Date;
import java.util.List;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.FeedMother;
import de.danoeh.antennapod.core.storage.DBWriter.ItemEnqueuePositionCalculator;
import de.danoeh.antennapod.core.storage.DBWriter.ItemEnqueuePositionCalculator.Options;
@ -26,17 +27,50 @@ public class DBWriterTest {
public static Iterable<Object[]> data() {
Options optDefault = new Options();
Options optEnqAtFront = new Options().setEnqueueAtFront(true);
Options optKeepInProgressAtFront =
new Options().setEnqueueAtFront(true).setKeepInProgressAtFront(true);
// edge case: keep in progress without enabling enqueue at front is meaningless
Options optKeepInProgressAtFrontWithNoEnqueueAtFront =
new Options().setKeepInProgressAtFront(true);
return Arrays.asList(new Object[][] {
{"case default, i.e., add to the end", QUEUE_DEFAULT.size(), optDefault , 0},
{"case default (2nd item)", QUEUE_DEFAULT.size(), optDefault , 1},
{"case option enqueue at front", 0, optEnqAtFront , 0},
{"case option enqueue at front (2nd item)", 1, optEnqAtFront , 1}
{"case default, i.e., add to the end",
QUEUE_DEFAULT.size(), optDefault , 0, QUEUE_DEFAULT},
{"case default (2nd item)",
QUEUE_DEFAULT.size(), optDefault , 1, QUEUE_DEFAULT},
{"case option enqueue at front",
0, optEnqAtFront , 0, QUEUE_DEFAULT},
{"case option enqueue at front (2nd item)",
1, optEnqAtFront , 1, QUEUE_DEFAULT},
{"case option keep in progress at front",
1, optKeepInProgressAtFront , 0, QUEUE_FRONT_IN_PROGRESS},
{"case option keep in progress at front (2nd item)",
2, optKeepInProgressAtFront , 1, QUEUE_FRONT_IN_PROGRESS},
{"case option keep in progress at front, front item not in progress",
0, optKeepInProgressAtFront , 0, QUEUE_DEFAULT},
{"case option keep in progress at front, front item no media at all",
0, optKeepInProgressAtFront , 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception
{"case option keep in progress at front, but enqueue at front is disabled",
QUEUE_FRONT_IN_PROGRESS.size(), optKeepInProgressAtFrontWithNoEnqueueAtFront , 0, QUEUE_FRONT_IN_PROGRESS},
{"case empty queue, option default",
0, optDefault, 0, QUEUE_EMPTY},
{"case empty queue, option enqueue at front",
0, optEnqAtFront, 0, QUEUE_EMPTY},
{"case empty queue, option keep in progress at front",
0, optKeepInProgressAtFront, 0, QUEUE_EMPTY},
});
}
private static final List<FeedItem> QUEUE_EMPTY = Arrays.asList();
private static final List<FeedItem> QUEUE_DEFAULT = Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14));
private static final List<FeedItem> QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13));
private static final List<FeedItem> QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13));
@Parameter
public String message;
@ -49,21 +83,39 @@ public class DBWriterTest {
@Parameter(3)
public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted.
@Parameter(4)
public List<FeedItem> curQueue;
@Test
public void test() {
ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options);
int posActual = calculator.calcPosition(posAmongAdded, tFI(101), QUEUE_DEFAULT);
int posActual = calculator.calcPosition(posAmongAdded, tFI(101), curQueue);
assertEquals(message, posExpected , posActual);
}
private static FeedItem tFI(int id) {
return tFI(id, -1);
}
private static FeedItem tFI(int id, int position) {
FeedItem item = tFINoMedia(id);
FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg");
item.setMedia(media);
if (position >= 0) {
media.setPosition(position);
}
return item;
}
private static FeedItem tFINoMedia(int id) {
FeedItem item = new FeedItem(0, "Item" + id, "ItemId" + id, "url",
new Date(), FeedItem.PLAYED, FeedMother.anyFeed());
return item;
}
}
}
}