refactored after merging in changes from #1212

This commit is contained in:
Tom Hennen 2015-09-20 14:54:54 -04:00
parent 1993f779e8
commit ed679b8a17
5 changed files with 45 additions and 77 deletions

View File

@ -339,7 +339,6 @@ public class AllEpisodesFragment extends Fragment {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo adapterInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
FeedItem item = itemAccess.getItem(adapterInfo.position);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.allepisodes_context, menu);
@ -354,11 +353,27 @@ public class AllEpisodesFragment extends Fragment {
@Override
public boolean onContextItemSelected(MenuItem item) {
if (!getUserVisibleHint()) {
// we're not visible, don't do anything.
return false;
}
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
if(menuInfo == null) {
if (menuInfo == null) {
menuInfo = lastMenuInfo;
}
FeedItem selectedItem = itemAccess.getItem(menuInfo.position);
if (menuInfo == null) {
Log.e(TAG, "menuInfo is null, not doing anything");
return false;
}
FeedItem selectedItem = null;
// make sure the item still makes sense
if (menuInfo.position >= 0 && menuInfo.position < itemAccess.getCount()) {
selectedItem = itemAccess.getItem(menuInfo.position);
} else {
Log.d(TAG, "Selected item at position " + menuInfo.position + " does not exist, only " + itemAccess.getCount() + " items available");
}
if (selectedItem == null) {
Log.i(TAG, "Selected item at position " + menuInfo.position + " was null, ignoring selection");
@ -484,13 +499,9 @@ public class AllEpisodesFragment extends Fragment {
});
}
private Pair<List<FeedItem>,LongList> loadData() {
protected Pair<List<FeedItem>,LongList> loadData() {
List<FeedItem> items;
if(showOnlyNewEpisodes) {
items = DBReader.getNewItemsList();
} else {
items = DBReader.getRecentlyPublishedEpisodes(RECENT_EPISODES_LIMIT);
}
items = DBReader.getRecentlyPublishedEpisodes(RECENT_EPISODES_LIMIT);
LongList queuedIds = DBReader.getQueueIDList();
return Pair.create(items, queuedIds);
}

View File

@ -3,6 +3,7 @@ package de.danoeh.antennapod.fragment;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.util.Pair;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -10,12 +11,15 @@ import android.view.ViewGroup;
import com.mobeta.android.dslv.DragSortListView;
import java.util.List;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.QueueEvent;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.core.util.gui.FeedItemUndoToken;
import de.danoeh.antennapod.core.util.gui.UndoBarController;
import de.greenrobot.event.EventBus;
@ -40,7 +44,7 @@ public class FavoriteEpisodesFragment extends AllEpisodesFragment {
public void onEvent(QueueEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
startItemLoader();
loadItems();
}
@Override
@ -68,7 +72,9 @@ public class FavoriteEpisodesFragment extends AllEpisodesFragment {
listView.setRemoveListener(which -> {
Log.d(TAG, "remove(" + which + ")");
stopItemLoader();
if (subscription != null) {
subscription.unsubscribe();
}
FeedItem item = (FeedItem) listView.getAdapter().getItem(which);
// TODO: actually remove the item from favorites
@ -103,28 +109,10 @@ public class FavoriteEpisodesFragment extends AllEpisodesFragment {
}
@Override
protected void startItemLoader() {
if (itemLoader != null) {
itemLoader.cancel(true);
}
itemLoader = new FavItemLoader();
itemLoader.execute();
protected Pair<List<FeedItem>,LongList> loadData() {
List<FeedItem> items;
items = DBReader.getFavoriteItemsList();
LongList queuedIds = DBReader.getQueueIDList();
return Pair.create(items, queuedIds);
}
private class FavItemLoader extends AllEpisodesFragment.ItemLoader {
@Override
protected Object[] doInBackground(Void... params) {
Context context = mainActivity.get();
if (context != null) {
return new Object[]{
DBReader.getFavoriteItemsList(),
DBReader.getQueueIDList()
};
} else {
return null;
}
}
}
}

View File

@ -2,6 +2,7 @@ package de.danoeh.antennapod.fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.Pair;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -9,12 +10,15 @@ import android.view.ViewGroup;
import com.mobeta.android.dslv.DragSortListView;
import java.util.List;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.QueueEvent;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.core.util.gui.FeedItemUndoToken;
import de.danoeh.antennapod.core.util.gui.UndoBarController;
import de.greenrobot.event.EventBus;
@ -69,7 +73,7 @@ public class NewEpisodesFragment extends AllEpisodesFragment {
@Override
public void remove(int which) {
Log.d(TAG, "remove(" + which + ")");
if(subscription != null) {
if (subscription != null) {
subscription.unsubscribe();
}
FeedItem item = (FeedItem) listView.getAdapter().getItem(which);
@ -110,29 +114,11 @@ public class NewEpisodesFragment extends AllEpisodesFragment {
}
@Override
protected void startItemLoader() {
if (itemLoader != null) {
itemLoader.cancel(true);
}
itemLoader = new NewItemLoader();
itemLoader.execute();
}
private class NewItemLoader extends AllEpisodesFragment.ItemLoader {
@Override
protected Object[] doInBackground(Void... params) {
Context context = mainActivity.get();
if (context != null) {
return new Object[] {
DBReader.getNewItemsList(),
DBReader.getQueueIDList(),
null // see ItemAccess.isNew
};
} else {
return null;
}
}
protected Pair<List<FeedItem>,LongList> loadData() {
List<FeedItem> items;
items = DBReader.getNewItemsList();
LongList queuedIds = DBReader.getQueueIDList();
return Pair.create(items, queuedIds);
}
}

View File

@ -301,22 +301,6 @@ public final class DBReader {
return items;
}
/**
* Loads a list of FeedItems in Favorites
* @return
*/
public static List<FeedItem> getFavorites() {
Log.d(TAG, "getFavorites");
try (PodDBAdapter adapter = PodDBAdapter.getInstance().open()) {
try (Cursor itemListCursor = adapter.getQueueCursor()) {
List<FeedItem> items = extractItemlistFromCursor(adapter, itemListCursor);
loadFeedDataOfFeedItemlist(items);
return items;
}
}
}
/**
* Loads a list of FeedItems whose episode has been downloaded.
*

View File

@ -814,9 +814,8 @@ public class PodDBAdapter implements AutoCloseable {
}
public boolean isItemInFavorites(FeedItem item) {
String query = String.format("SELECT %s from %s WHERE %s=%d AND %s=%d",
KEY_ID, TABLE_NAME_FAVORITES, KEY_FEEDITEM, item.getId(), KEY_FEED,
item.getFeedId());
String query = String.format("SELECT %s from %s WHERE %s=%d",
KEY_ID, TABLE_NAME_FAVORITES, KEY_FEEDITEM, item.getId());
Cursor c = db.rawQuery(query, null);
int count = c.getCount();
c.close();