diff options
author | orionlee <orionlee@yahoo.com> | 2019-10-04 13:06:29 -0700 |
---|---|---|
committer | orionlee <orionlee@yahoo.com> | 2019-11-05 12:33:58 -0800 |
commit | cd3d20d61338180bfa585fbf8fcc2b13261df709 (patch) | |
tree | 243e82df10dfc8c1199cf4486b85c47eeb66ee26 /core/src/main/java/de/danoeh | |
parent | 2d1ee52014aa6b171733261a698129f5de3f4036 (diff) | |
download | AntennaPod-cd3d20d61338180bfa585fbf8fcc2b13261df709.zip |
refactor - move ItemEnqueuePositionCalculator to top-level per review.
Diffstat (limited to 'core/src/main/java/de/danoeh')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java | 95 | ||||
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculator.java | 109 |
2 files changed, 109 insertions, 95 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java index 1ba58f3d3..57db6123c 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java @@ -398,101 +398,6 @@ public class DBWriter { events.add(QueueEvent.sorted(queue)); } - @VisibleForTesting - static class ItemEnqueuePositionCalculator { - - public static class Options { - private boolean enqueueAtFront = false; - private boolean keepInProgressAtFront = false; - - public boolean isEnqueueAtFront() { - return enqueueAtFront; - } - - public Options setEnqueueAtFront(boolean enqueueAtFront) { - 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; - - /** - * The logic needs to use {@link DownloadRequester#isDownloadingFile(FeedFile)} method only - */ - @VisibleForTesting - FeedFileDownloadStatusRequesterInterface requester = DownloadRequester.getInstance(); - - public ItemEnqueuePositionCalculator(@NonNull Options options) { - this.options = options; - } - - /** - * - * @param positionAmongToAdd Typically, the callers has a list of items to be inserted to - * the queue. This parameter indicates the position (0-based) of - * the item among the one to inserted. E.g., it is needed for - * enqueue at front option. - * - * @param item the item to be inserted - * @param curQueue the queue to which the item is to be inserted - * @return the position (0-based) the item should be inserted to the named queu - */ - public int calcPosition(int positionAmongToAdd, FeedItem item, List<FeedItem> curQueue) { - if (options.isEnqueueAtFront()) { - if (options.isKeepInProgressAtFront() && - curQueue.size() > 0 && - curQueue.get(0).getMedia() != null && - curQueue.get(0).getMedia().isInProgress()) { - // leave the front in progress item at the front - return getPositionOf1stNonDownloadingItem(positionAmongToAdd + 1, curQueue); - } 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 getPositionOf1stNonDownloadingItem(positionAmongToAdd, curQueue); - } - } else { - return curQueue.size(); - } - } - - private int getPositionOf1stNonDownloadingItem(int startPosition, List<FeedItem> curQueue) { - final int curQueueSize = curQueue.size(); - for (int i = startPosition; i < curQueueSize; i++) { - if (!isItemAtPositionDownloading(i, curQueue)) { - return i; - } // else continue to search; - } - return curQueueSize; - } - - private boolean isItemAtPositionDownloading(int position, List<FeedItem> curQueue) { - FeedItem curItem; - try { - curItem = curQueue.get(position); - } catch (IndexOutOfBoundsException e) { - curItem = null; - } - - if (curItem != null && - curItem.getMedia() != null && - requester.isDownloadingFile(curItem.getMedia())) { - return true; - } else { - return false; - } - } - } - /** * Removes all FeedItem objects from the queue. */ diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculator.java b/core/src/main/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculator.java new file mode 100644 index 000000000..6e7843836 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculator.java @@ -0,0 +1,109 @@ +package de.danoeh.antennapod.core.storage; + +import android.content.Context; + +import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; + +import java.util.List; + +import de.danoeh.antennapod.core.feed.FeedItem; + +/** + * @see DBWriter#addQueueItem(Context, boolean, long...) it uses the class to determine + * the positions of the {@link FeedItem} in the queue. + */ +class ItemEnqueuePositionCalculator { + + public static class Options { + private boolean enqueueAtFront = false; + private boolean keepInProgressAtFront = false; + + public boolean isEnqueueAtFront() { + return enqueueAtFront; + } + + public Options setEnqueueAtFront(boolean enqueueAtFront) { + 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; + + /** + * The logic needs to use {@link DownloadRequester#isDownloadingFile(FeedFile)} method only + */ + @VisibleForTesting + FeedFileDownloadStatusRequesterInterface requester = DownloadRequester.getInstance(); + + public ItemEnqueuePositionCalculator(@NonNull Options options) { + this.options = options; + } + + /** + * + * @param positionAmongToAdd Typically, the callers has a list of items to be inserted to + * the queue. This parameter indicates the position (0-based) of + * the item among the one to inserted. E.g., it is needed for + * enqueue at front option. + * + * @param item the item to be inserted + * @param curQueue the queue to which the item is to be inserted + * @return the position (0-based) the item should be inserted to the named queu + */ + public int calcPosition(int positionAmongToAdd, FeedItem item, List<FeedItem> curQueue) { + if (options.isEnqueueAtFront()) { + if (options.isKeepInProgressAtFront() && + curQueue.size() > 0 && + curQueue.get(0).getMedia() != null && + curQueue.get(0).getMedia().isInProgress()) { + // leave the front in progress item at the front + return getPositionOf1stNonDownloadingItem(positionAmongToAdd + 1, curQueue); + } 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 getPositionOf1stNonDownloadingItem(positionAmongToAdd, curQueue); + } + } else { + return curQueue.size(); + } + } + + private int getPositionOf1stNonDownloadingItem(int startPosition, List<FeedItem> curQueue) { + final int curQueueSize = curQueue.size(); + for (int i = startPosition; i < curQueueSize; i++) { + if (!isItemAtPositionDownloading(i, curQueue)) { + return i; + } // else continue to search; + } + return curQueueSize; + } + + private boolean isItemAtPositionDownloading(int position, List<FeedItem> curQueue) { + FeedItem curItem; + try { + curItem = curQueue.get(position); + } catch (IndexOutOfBoundsException e) { + curItem = null; + } + + if (curItem != null && + curItem.getMedia() != null && + requester.isDownloadingFile(curItem.getMedia())) { + return true; + } else { + return false; + } + } +} |