replaced context menu with popup menu. still a couple of other issues

This commit is contained in:
Tom Hennen 2015-10-10 19:17:49 -04:00
parent a02b84d2d2
commit 89bbc88826
2 changed files with 71 additions and 88 deletions

View File

@ -3,13 +3,15 @@ package de.danoeh.antennapod.adapter;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
@ -28,10 +30,12 @@ import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.storage.DownloadRequestException;
import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.NetworkUtils; import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.fragment.ItemFragment; import de.danoeh.antennapod.fragment.ItemFragment;
import de.danoeh.antennapod.menuhandler.FeedItemMenuHandler;
/** /**
* List adapter for the list of new episodes * List adapter for the list of new episodes
@ -82,6 +86,8 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<AllEpisodesR
holder.item = null; holder.item = null;
holder.mainActivity = mainActivity; holder.mainActivity = mainActivity;
holder.position = -1; holder.position = -1;
// so we can grab this later
view.setTag(holder);
return holder; return holder;
} }
@ -229,9 +235,58 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<AllEpisodesR
} }
}; };
private Menu popupMenu;
private final FeedItemMenuHandler.MenuInterface contextMenuInterface = new FeedItemMenuHandler.MenuInterface() {
@Override
public void setItemVisibility(int id, boolean visible) {
if(popupMenu == null) {
return;
}
MenuItem item = popupMenu.findItem(id);
if (item != null) {
item.setVisible(visible);
}
}
};
public static class Holder extends RecyclerView.ViewHolder private final boolean showContextMenu(View view) {
implements View.OnClickListener { // Create a PopupMenu, giving it the clicked view for an anchor
PopupMenu popup = new PopupMenu(mainActivity, view);
Menu menu = popup.getMenu();
// Inflate our menu resource into the PopupMenu's Menu
popup.getMenuInflater().inflate(R.menu.allepisodes_context, popup.getMenu());
Holder holder = (Holder) view.getTag();
FeedItem item = holder.item;
if (item == null) {
return false;
}
popupMenu = menu;
FeedItemMenuHandler.onPrepareMenu(context, contextMenuInterface, item, true, null);
// Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
try {
FeedItemMenuHandler.onMenuItemClicked(context, menuItem.getItemId(), item);
return true;
} catch (DownloadRequestException e) {
e.printStackTrace();
}
return false;
}
});
popup.show();
return true;
}
public class Holder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener{
TextView placeholder; TextView placeholder;
TextView title; TextView title;
TextView pubDate; TextView pubDate;
@ -248,7 +303,7 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<AllEpisodesR
public Holder(View itemView) { public Holder(View itemView) {
super(itemView); super(itemView);
itemView.setOnClickListener(this); itemView.setOnClickListener(this);
itemView.setOnCreateContextMenuListener(mainActivity); itemView.setOnLongClickListener(this);
} }
@Override @Override
@ -261,6 +316,11 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<AllEpisodesR
public FeedItem getFeedItem() { return item; } public FeedItem getFeedItem() { return item; }
public int getItemPosition() { return position; } public int getItemPosition() { return position; }
@Override
public boolean onLongClick(View view) {
return showContextMenu(view);
}
} }
public interface ItemAccess { public interface ItemAccess {

View File

@ -78,11 +78,8 @@ public class AllEpisodesFragment extends Fragment {
private AllEpisodesRecycleAdapter listAdapter; private AllEpisodesRecycleAdapter listAdapter;
private TextView txtvEmpty; private TextView txtvEmpty;
private ProgressBar progLoading; private ProgressBar progLoading;
private ContextMenu contextMenu;
private AdapterView.AdapterContextMenuInfo lastMenuInfo = null;
private List<FeedItem> episodes; private List<FeedItem> episodes;
private LongList queuedItemsIds;
private List<Downloader> downloaderList; private List<Downloader> downloaderList;
private boolean itemsLoaded = false; private boolean itemsLoaded = false;
@ -297,7 +294,7 @@ public class AllEpisodesFragment extends Fragment {
txtvEmpty = (TextView) root.findViewById(android.R.id.empty); txtvEmpty = (TextView) root.findViewById(android.R.id.empty);
progLoading = (ProgressBar) root.findViewById(R.id.progLoading); progLoading = (ProgressBar) root.findViewById(R.id.progLoading);
registerForContextMenu(listView); //registerForContextMenu(listView);
if (!itemsLoaded) { if (!itemsLoaded) {
progLoading.setVisibility(View.VISIBLE); progLoading.setVisibility(View.VISIBLE);
@ -313,75 +310,6 @@ public class AllEpisodesFragment extends Fragment {
return root; return root;
} }
private final FeedItemMenuHandler.MenuInterface contextMenuInterface = new FeedItemMenuHandler.MenuInterface() {
@Override
public void setItemVisibility(int id, boolean visible) {
if(contextMenu == null) {
return;
}
MenuItem item = contextMenu.findItem(id);
if (item != null) {
item.setVisible(visible);
}
}
};
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (menuInfo == null) { return; }
AdapterView.AdapterContextMenuInfo adapterInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
FeedItem item = itemAccess.getItem(adapterInfo.position);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.allepisodes_context, menu);
if (item != null) {
menu.setHeaderTitle(item.getTitle());
}
contextMenu = menu;
lastMenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
FeedItemMenuHandler.onPrepareMenu(getActivity(), contextMenuInterface, item, true, queuedItemsIds);
}
@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) {
menuInfo = lastMenuInfo;
}
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");
return super.onContextItemSelected(item);
}
try {
return FeedItemMenuHandler.onMenuItemClicked(getActivity(), item.getItemId(), selectedItem);
} catch (DownloadRequestException e) {
e.printStackTrace();
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
return true;
}
}
private void onFragmentLoaded() { private void onFragmentLoaded() {
if (listAdapter == null) { if (listAdapter == null) {
listAdapter = new AllEpisodesRecycleAdapter(activity.get(), activity.get(), itemAccess, listAdapter = new AllEpisodesRecycleAdapter(activity.get(), activity.get(), itemAccess,
@ -439,11 +367,10 @@ public class AllEpisodesFragment extends Fragment {
@Override @Override
public boolean isInQueue(FeedItem item) { public boolean isInQueue(FeedItem item) {
if (itemsLoaded) { if (item != null) {
return queuedItemsIds.contains(item.getId()); return item.isTagged(FeedItem.TAG_QUEUE);
} else {
return false;
} }
return false;
} }
}; };
@ -478,8 +405,7 @@ public class AllEpisodesFragment extends Fragment {
listView.setVisibility(View.VISIBLE); listView.setVisibility(View.VISIBLE);
progLoading.setVisibility(View.GONE); progLoading.setVisibility(View.GONE);
if (data != null) { if (data != null) {
episodes = data.first; episodes = data;
queuedItemsIds = data.second;
itemsLoaded = true; itemsLoaded = true;
if (viewsCreated && activity.get() != null) { if (viewsCreated && activity.get() != null) {
onFragmentLoaded(); onFragmentLoaded();
@ -490,11 +416,8 @@ public class AllEpisodesFragment extends Fragment {
}); });
} }
protected Pair<List<FeedItem>,LongList> loadData() { protected List<FeedItem> loadData() {
List<FeedItem> items; return DBReader.getRecentlyPublishedEpisodes(RECENT_EPISODES_LIMIT);
items = DBReader.getRecentlyPublishedEpisodes(RECENT_EPISODES_LIMIT);
LongList queuedIds = DBReader.getQueueIDList();
return Pair.create(items, queuedIds);
} }
} }