diff options
author | orionlee <orionlee@yahoo.com> | 2019-04-02 14:59:12 -0700 |
---|---|---|
committer | orionlee <orionlee@yahoo.com> | 2019-04-02 14:59:12 -0700 |
commit | 70287a740ee0cfba23233e6cfb3757e2faaf65ad (patch) | |
tree | 45778d16d0b96ae84611a5560a5411b8c0844ad7 /core/src/main/java | |
parent | 7d78b88a1cf51fc14912fcf36ae2e1efff368f75 (diff) | |
download | AntennaPod-70287a740ee0cfba23233e6cfb3757e2faaf65ad.zip |
bulk remove from queue - backend - refactor existing logic to support bulk remove
Diffstat (limited to 'core/src/main/java')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java | 53 |
1 files changed, 41 insertions, 12 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 451bb8685..b2f28008a 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 @@ -90,7 +90,7 @@ public class DBWriter { boolean result = deleteFeedMediaSynchronous(context, media); if (result && UserPreferences.shouldDeleteRemoveFromQueue()) { - DBWriter.removeQueueItemSynchronous(context, media.getItem(), false); + DBWriter.removeQueueItemSynchronous(context, false, media.getItem().getId()); } } }); @@ -431,23 +431,47 @@ public class DBWriter { */ public static Future<?> removeQueueItem(final Context context, final FeedItem item, final boolean performAutoDownload) { - return dbExec.submit(() -> removeQueueItemSynchronous(context, item, performAutoDownload)); + return dbExec.submit(() -> removeQueueItemSynchronous(context, performAutoDownload, item.getId())); } private static void removeQueueItemSynchronous(final Context context, - final FeedItem item, final boolean performAutoDownload) { + final boolean performAutoDownload, + final long... itemIds) { + if (itemIds.length < 1) { + return; + } final PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); final List<FeedItem> queue = DBReader.getQueue(adapter); if (queue != null) { - int position = queue.indexOf(item); - if (position >= 0) { - queue.remove(position); + boolean queueModified = false; + List<QueueEvent> events = new ArrayList<>(); + List<FeedItem> updatedItems = new ArrayList<>(); + for (long itemId : itemIds) { + int position = indexInItemList(queue, itemId); + if (position >= 0) { + final FeedItem item = DBReader.getFeedItem(itemId); + if (item == null) { + Log.e(TAG, "removeQueueItem - item in queue but somehow cannot be loaded." + + " Item ignored. It should never happen. id:" + itemId); + continue; + } + queue.remove(position); + item.removeTag(FeedItem.TAG_QUEUE); + events.add(QueueEvent.removed(item)); + updatedItems.add(item); + queueModified = true; + } else { + Log.v(TAG, "removeQueueItem - item not in queue:" + itemId); + } + } + if (queueModified) { adapter.setQueue(queue); - item.removeTag(FeedItem.TAG_QUEUE); - EventBus.getDefault().post(QueueEvent.removed(item)); - EventBus.getDefault().post(FeedItemEvent.updated(item)); + for (QueueEvent event : events) { + EventBus.getDefault().post(event); + } + EventBus.getDefault().post(FeedItemEvent.updated(updatedItems)); } else { Log.w(TAG, "Queue was not modified by call to removeQueueItem"); } @@ -793,12 +817,17 @@ public class DBWriter { } private static boolean itemListContains(List<FeedItem> items, long itemId) { - for (FeedItem item : items) { + return indexInItemList(items, itemId) >= 0; + } + + private static int indexInItemList(List<FeedItem> items, long itemId) { + for (int i = 0; i < items.size(); i ++) { + FeedItem item = items.get(i); if (item.getId() == itemId) { - return true; + return i; } } - return false; + return -1; } /** |