Search in sources :

Example 1 with QueueEvent

use of de.danoeh.antennapod.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 markAsUnplayed      true if the items should be marked as unplayed when enqueueing
 * @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 boolean markAsUnplayed, final long... itemIds) {
    return dbExec.submit(() -> {
        if (itemIds.length < 1) {
            return;
        }
        final PodDBAdapter adapter = PodDBAdapter.getInstance();
        adapter.open();
        final List<FeedItem> queue = DBReader.getQueue(adapter);
        boolean queueModified = false;
        LongList markAsUnplayedIds = new LongList();
        List<QueueEvent> events = new ArrayList<>();
        List<FeedItem> updatedItems = new ArrayList<>();
        ItemEnqueuePositionCalculator positionCalculator = new ItemEnqueuePositionCalculator(UserPreferences.getEnqueueLocation());
        Playable currentlyPlaying = PlayableUtils.createInstanceFromPreferences(context);
        int insertPosition = positionCalculator.calcPosition(queue, currentlyPlaying);
        for (long itemId : itemIds) {
            if (!itemListContains(queue, itemId)) {
                final FeedItem item = DBReader.getFeedItem(itemId);
                if (item != null) {
                    queue.add(insertPosition, item);
                    events.add(QueueEvent.added(item, insertPosition));
                    item.addTag(FeedItem.TAG_QUEUE);
                    updatedItems.add(item);
                    queueModified = true;
                    if (item.isNew()) {
                        markAsUnplayedIds.add(item.getId());
                    }
                    insertPosition++;
                }
            }
        }
        if (queueModified) {
            applySortOrder(queue, events);
            adapter.setQueue(queue);
            for (QueueEvent event : events) {
                EventBus.getDefault().post(event);
            }
            EventBus.getDefault().post(FeedItemEvent.updated(updatedItems));
            if (markAsUnplayed && markAsUnplayedIds.size() > 0) {
                DBWriter.markItemPlayed(FeedItem.UNPLAYED, markAsUnplayedIds.toArray());
            }
        }
        adapter.close();
        if (performAutoDownload) {
            DBTasks.autodownloadUndownloadedItems(context);
        }
    });
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) Playable(de.danoeh.antennapod.model.playback.Playable) ArrayList(java.util.ArrayList) QueueEvent(de.danoeh.antennapod.event.QueueEvent) LongList(de.danoeh.antennapod.core.util.LongList)

Example 2 with QueueEvent

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

the class DBWriter method removeQueueItemSynchronous.

private static void removeQueueItemSynchronous(final Context context, 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) {
        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);
            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");
        }
    } else {
        Log.e(TAG, "removeQueueItem: Could not load queue");
    }
    adapter.close();
    if (performAutoDownload) {
        DBTasks.autodownloadUndownloadedItems(context);
    }
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) ArrayList(java.util.ArrayList) QueueEvent(de.danoeh.antennapod.event.QueueEvent)

Aggregations

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