bulk remove from queue - backend - refactor existing logic to support bulk remove

This commit is contained in:
orionlee 2019-04-02 14:59:12 -07:00
parent 7d78b88a1c
commit 70287a740e
1 changed files with 41 additions and 12 deletions

View File

@ -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;
}
/**