Merge pull request #1582 from mfietz/issue/1367-add-favorites

Feed view: Add "Add/Remove to/from Favorites" to context menu
This commit is contained in:
Tom Hennen 2016-01-23 10:35:44 -05:00
commit 5c3258d40b
9 changed files with 122 additions and 32 deletions

View File

@ -35,6 +35,7 @@ import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.DateUtils;
import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.fragment.ItemFragment;
import de.danoeh.antennapod.menuhandler.FeedItemMenuHandler;
@ -328,8 +329,8 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<AllEpisodesR
item1.setVisible(visible);
}
};
FeedItemMenuHandler.onPrepareMenu(mainActivityRef.get(), contextMenuInterface, item, true,
null);
FeedItemMenuHandler.onPrepareMenu(contextMenuInterface, item, true,
itemAccess.getQueueIds(), itemAccess.getFavoritesIds());
}
}
@ -344,6 +345,10 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<AllEpisodesR
boolean isInQueue(FeedItem item);
LongList getQueueIds();
LongList getFavoritesIds();
}
/**

View File

@ -183,8 +183,8 @@ public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdap
item1.setVisible(visible);
}
};
FeedItemMenuHandler.onPrepareMenu(mainActivity.get(), contextMenuInterface, item, true,
itemAccess.getQueueIds());
FeedItemMenuHandler.onPrepareMenu(contextMenuInterface, item, true,
itemAccess.getQueueIds(), itemAccess.getFavoritesIds());
}
@Override
@ -351,6 +351,7 @@ public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdap
long getItemDownloadSize(FeedItem item);
int getItemDownloadProgressPercent(FeedItem item);
LongList getQueueIds();
LongList getFavoritesIds();
}
/**

View File

@ -46,6 +46,7 @@ import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.DownloadRequestException;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.FeedItemUtil;
import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.menuhandler.FeedItemMenuHandler;
import de.danoeh.antennapod.menuhandler.MenuItemUtils;
import de.greenrobot.event.EventBus;
@ -382,6 +383,34 @@ public class AllEpisodesFragment extends Fragment {
}
return false;
}
@Override
public LongList getQueueIds() {
LongList queueIds = new LongList();
if(episodes == null) {
return queueIds;
}
for(FeedItem item : episodes) {
if(item.isTagged(FeedItem.TAG_QUEUE)) {
queueIds.add(item.getId());
}
}
return queueIds;
}
@Override
public LongList getFavoritesIds() {
LongList favoritesIds = new LongList();
if(episodes == null) {
return favoritesIds;
}
for(FeedItem item : episodes) {
if(item.isTagged(FeedItem.TAG_FAVORITE)) {
favoritesIds.add(item.getId());
}
}
return favoritesIds;
}
};
public void onEventMainThread(FeedItemEvent event) {

View File

@ -9,7 +9,6 @@ import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
@ -40,6 +39,7 @@ import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.adapter.DefaultActionButtonCallback;
import de.danoeh.antennapod.core.event.DownloadEvent;
import de.danoeh.antennapod.core.event.DownloaderUpdate;
import de.danoeh.antennapod.core.event.FavoritesEvent;
import de.danoeh.antennapod.core.event.FeedItemEvent;
import de.danoeh.antennapod.core.event.QueueEvent;
import de.danoeh.antennapod.core.feed.EventDistributor;
@ -95,6 +95,7 @@ public class ItemFragment extends Fragment {
private long itemID;
private FeedItem item;
private LongList queue;
private LongList favorites;
private String webviewData;
private List<Downloader> downloaderList;
@ -249,10 +250,10 @@ public class ItemFragment extends Fragment {
inflater.inflate(R.menu.feeditem_options, menu);
popupMenu = menu;
if (item.hasMedia()) {
FeedItemMenuHandler.onPrepareMenu(getActivity(), popupMenuInterface, item, true, queue);
FeedItemMenuHandler.onPrepareMenu(popupMenuInterface, item, true, queue, favorites);
} else {
// these are already available via button1 and button2
FeedItemMenuHandler.onPrepareMenu(getActivity(), popupMenuInterface, item, true, queue,
FeedItemMenuHandler.onPrepareMenu(popupMenuInterface, item, true, queue, favorites,
R.id.mark_read_item, R.id.visit_website_item);
}
}
@ -457,6 +458,12 @@ public class ItemFragment extends Fragment {
}
}
public void onEventMainThread(FavoritesEvent event) {
if(event.item.getId() == itemID) {
load();
}
}
public void onEventMainThread(FeedItemEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
for(FeedItem item : event.items) {
@ -500,8 +507,9 @@ public class ItemFragment extends Fragment {
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
item = result.first;
queue = result.second;
item = (FeedItem) result[0];
queue = (LongList) result[1];
favorites = (LongList) result[2];
if (!itemsLoaded) {
itemsLoaded = true;
onFragmentLoaded();
@ -513,14 +521,25 @@ public class ItemFragment extends Fragment {
});
}
private Pair<FeedItem,LongList> loadInBackground() {
FeedItem data1 = DBReader.getFeedItem(itemID);
if (data1 != null) {
Timeline t = new Timeline(getActivity(), data1);
private Object[] loadInBackground() {
FeedItem feedItem = DBReader.getFeedItem(itemID);
if (feedItem != null) {
Timeline t = new Timeline(getActivity(), feedItem);
webviewData = t.processShownotes(false);
}
LongList data2 = DBReader.getQueueIDList();
return Pair.create(data1, data2);
LongList queue;
if(feedItem.isTagged(FeedItem.TAG_QUEUE)) {
queue = LongList.of(feedItem.getId());
} else {
queue = new LongList(0);
}
LongList favorites;
if(feedItem.isTagged(FeedItem.TAG_FAVORITE)) {
favorites = LongList.of(feedItem.getId());
} else {
favorites = new LongList(0);
}
return new Object[] { feedItem, queue, favorites };
}
}

View File

@ -10,7 +10,6 @@ import android.graphics.LightingColorFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.util.Pair;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.util.Log;
@ -48,6 +47,7 @@ import de.danoeh.antennapod.core.dialog.ConfirmationDialog;
import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.core.event.DownloadEvent;
import de.danoeh.antennapod.core.event.DownloaderUpdate;
import de.danoeh.antennapod.core.event.FavoritesEvent;
import de.danoeh.antennapod.core.event.FeedItemEvent;
import de.danoeh.antennapod.core.event.QueueEvent;
import de.danoeh.antennapod.core.feed.EventDistributor;
@ -99,6 +99,7 @@ public class ItemlistFragment extends ListFragment {
private long feedID;
private Feed feed;
private LongList queuedItemsIds;
private LongList favoritedItemsId;
private boolean itemsLoaded = false;
private boolean viewsCreated = false;
@ -324,7 +325,8 @@ public class ItemlistFragment extends ListFragment {
contextMenu = menu;
lastMenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
FeedItemMenuHandler.onPrepareMenu(getActivity(), contextMenuInterface, item, true, queuedItemsIds);
FeedItemMenuHandler.onPrepareMenu(contextMenuInterface, item, true, queuedItemsIds,
favoritedItemsId);
}
@Override
@ -389,6 +391,11 @@ public class ItemlistFragment extends ListFragment {
loadItems();
}
public void onEvent(FavoritesEvent event) {
Log.d(TAG, "onEvent() called with: " + "event = [" + event + "]");
loadItems();
}
public void onEvent(FeedEvent event) {
Log.d(TAG, "onEvent() called with: " + "event = [" + event + "]");
if(event.feedId == feedID) {
@ -626,8 +633,9 @@ public class ItemlistFragment extends ListFragment {
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
if (result != null) {
feed = result.first;
queuedItemsIds = result.second;
feed = (Feed) result[0];
queuedItemsIds = (LongList) result[1];
favoritedItemsId = (LongList) result[2];
itemsLoaded = true;
if (viewsCreated) {
onFragmentLoaded();
@ -638,14 +646,15 @@ public class ItemlistFragment extends ListFragment {
});
}
private Pair<Feed, LongList> loadData() {
private Object[] loadData() {
Feed feed = DBReader.getFeed(feedID);
if(feed != null && feed.getItemFilter() != null) {
FeedItemFilter filter = feed.getItemFilter();
feed.setItems(filter.filter(feed.getItems()));
}
LongList queuedItemsIds = DBReader.getQueueIDList();
return Pair.create(feed, queuedItemsIds);
LongList favoritedItemsId = DBReader.getFavoriteIDList();
return new Object[] { feed, queuedItemsIds, favoritedItemsId };
}
}

View File

@ -568,6 +568,20 @@ public class QueueFragment extends Fragment {
public LongList getQueueIds() {
return queue != null ? LongList.of(FeedItemUtil.getIds(queue)) : new LongList(0);
}
@Override
public LongList getFavoritesIds() {
LongList favoritesIds = new LongList();
if(queue == null) {
return favoritesIds;
}
for(FeedItem item : queue) {
if(item.isTagged(FeedItem.TAG_FAVORITE)) {
favoritesIds.add(item.getId());
}
}
return favoritesIds;
}
};
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {

View File

@ -57,16 +57,16 @@ public class FeedItemMenuHandler {
* @param queueAccess Used for testing if the queue contains the selected item
* @return Returns true if selectedItem is not null.
*/
public static boolean onPrepareMenu(Context context, MenuInterface mi, FeedItem selectedItem,
boolean showExtendedMenu, LongList queueAccess) {
public static boolean onPrepareMenu(MenuInterface mi,
FeedItem selectedItem,
boolean showExtendedMenu,
LongList queueAccess,
LongList favorites) {
if (selectedItem == null) {
return false;
}
boolean hasMedia = selectedItem.getMedia() != null;
boolean isPlaying = hasMedia
&& selectedItem.getState() == FeedItem.State.PLAYING;
FeedItem.State state = selectedItem.getState();
boolean isPlaying = hasMedia && selectedItem.getState() == FeedItem.State.PLAYING;
if (!isPlaying) {
mi.setItemVisibility(R.id.skip_episode_item, false);
@ -126,7 +126,7 @@ public class FeedItemMenuHandler {
mi.setItemVisibility(R.id.support_item, false);
}
boolean isFavorite = selectedItem.isTagged(FeedItem.TAG_FAVORITE);
boolean isFavorite = favorites != null && favorites.contains(selectedItem.getId());
mi.setItemVisibility(R.id.add_to_favorites_item, !isFavorite);
mi.setItemVisibility(R.id.remove_from_favorites_item, isFavorite);
@ -140,9 +140,13 @@ public class FeedItemMenuHandler {
* @param excludeIds Menu item that should be excluded
* @return true if selectedItem is not null.
*/
public static boolean onPrepareMenu(Context context, MenuInterface mi, FeedItem selectedItem,
boolean showExtendedMenu, LongList queueAccess, int... excludeIds) {
boolean rc = onPrepareMenu(context, mi, selectedItem, showExtendedMenu, queueAccess);
public static boolean onPrepareMenu(MenuInterface mi,
FeedItem selectedItem,
boolean showExtendedMenu,
LongList queueAccess,
LongList favorites,
int... excludeIds) {
boolean rc = onPrepareMenu(mi, selectedItem, showExtendedMenu, queueAccess, favorites);
if (rc && excludeIds != null) {
for (int id : excludeIds) {
mi.setItemVisibility(id, false);

View File

@ -25,6 +25,15 @@
android:menuCategory="container"
android:title="@string/remove_from_queue_label" />
<item
android:id="@+id/add_to_favorites_item"
android:menuCategory="container"
android:title="@string/add_to_favorite_label" />
<item
android:id="@+id/remove_from_favorites_item"
android:menuCategory="container"
android:title="@string/remove_from_favorite_label" />
<item
android:id="@+id/reset_position"
android:menuCategory="container"

View File

@ -408,7 +408,7 @@ public final class DBReader {
return items;
}
static LongList getFavoriteIDList() {
public static LongList getFavoriteIDList() {
Log.d(TAG, "getFavoriteIDList() called");
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();