Search in sources :

Example 1 with QueueEvent

use of de.danoeh.antennapod.core.event.QueueEvent in project AntennaPod by AntennaPod.

the class DBWriter method addQueueItem.

/**
     * Appends FeedItem objects to the end of the queue. The 'read'-attribute of all items will be set to true.
     * If a FeedItem is already in the queue, the FeedItem will not change its position in the queue.
     *
     * @param context A context that is used for opening a database connection.
     * @param performAutoDownload true if an auto-download process should be started after the operation.
     * @param itemIds IDs of the FeedItem objects that should be added to the queue.
     */
public static Future<?> addQueueItem(final Context context, final boolean performAutoDownload, final long... itemIds) {
    return dbExec.submit(() -> {
        if (itemIds.length > 0) {
            final PodDBAdapter adapter = PodDBAdapter.getInstance();
            adapter.open();
            final List<FeedItem> queue = DBReader.getQueue(adapter);
            if (queue != null) {
                boolean queueModified = false;
                LongList markAsUnplayedIds = new LongList();
                List<QueueEvent> events = new ArrayList<>();
                List<FeedItem> updatedItems = new ArrayList<>();
                for (int i = 0; i < itemIds.length; i++) {
                    if (!itemListContains(queue, itemIds[i])) {
                        final FeedItem item = DBReader.getFeedItem(itemIds[i]);
                        if (item != null) {
                            // add item to either front ot back of queue
                            boolean addToFront = UserPreferences.enqueueAtFront();
                            if (addToFront) {
                                queue.add(0 + i, item);
                                events.add(QueueEvent.added(item, 0 + i));
                            } else {
                                queue.add(item);
                                events.add(QueueEvent.added(item, queue.size() - 1));
                            }
                            item.addTag(FeedItem.TAG_QUEUE);
                            updatedItems.add(item);
                            queueModified = true;
                            if (item.isNew()) {
                                markAsUnplayedIds.add(item.getId());
                            }
                        }
                    }
                }
                if (queueModified) {
                    adapter.setQueue(queue);
                    for (QueueEvent event : events) {
                        EventBus.getDefault().post(event);
                    }
                    EventBus.getDefault().post(FeedItemEvent.updated(updatedItems));
                    if (markAsUnplayedIds.size() > 0) {
                        DBWriter.markItemPlayed(FeedItem.UNPLAYED, markAsUnplayedIds.toArray());
                    }
                }
            }
            adapter.close();
            if (performAutoDownload) {
                DBTasks.autodownloadUndownloadedItems(context);
            }
        }
    });
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) ArrayList(java.util.ArrayList) QueueEvent(de.danoeh.antennapod.core.event.QueueEvent) LongList(de.danoeh.antennapod.core.util.LongList)

Aggregations

QueueEvent (de.danoeh.antennapod.core.event.QueueEvent)1 FeedItem (de.danoeh.antennapod.core.feed.FeedItem)1 LongList (de.danoeh.antennapod.core.util.LongList)1 ArrayList (java.util.ArrayList)1