From 9513099a10b44e5570505910764f9a1a71f2b14c Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Sun, 19 Aug 2012 16:27:37 +0200 Subject: [PATCH] Itemlists were not updating their content properly if "Display only episodes'-preference was enabled --- src/de/danoeh/antennapod/PodcastApp.java | 10 ++ .../adapter/FeedItemlistAdapter.java | 158 +++++++++++------- .../antennapod/fragment/ItemlistFragment.java | 20 +-- .../antennapod/fragment/QueueFragment.java | 3 +- .../danoeh/antennapod/util/EpisodeFilter.java | 30 +++- 5 files changed, 136 insertions(+), 85 deletions(-) diff --git a/src/de/danoeh/antennapod/PodcastApp.java b/src/de/danoeh/antennapod/PodcastApp.java index 0d2f0e253..414e6b3ba 100644 --- a/src/de/danoeh/antennapod/PodcastApp.java +++ b/src/de/danoeh/antennapod/PodcastApp.java @@ -37,6 +37,8 @@ public class PodcastApp extends Application implements private static float LOGICAL_DENSITY; private static PodcastApp singleton; + + private boolean displayOnlyEpisodes; public static PodcastApp getInstance() { return singleton; @@ -49,6 +51,7 @@ public class PodcastApp extends Application implements LOGICAL_DENSITY = getResources().getDisplayMetrics().density; SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(this); + displayOnlyEpisodes = prefs.getBoolean(PREF_DISPLAY_ONLY_EPISODES, false); createImportDirectory(); createNoMediaFile(); prefs.registerOnSharedPreferenceChangeListener(this); @@ -124,12 +127,19 @@ public class PodcastApp extends Application implements if (AppConfig.DEBUG) Log.d(TAG, "Automatic update was deactivated"); } + } else if (key.equals(PREF_DISPLAY_ONLY_EPISODES)) { + if (AppConfig.DEBUG) Log.d(TAG, "PREF_DISPLAY_ONLY_EPISODES changed"); + displayOnlyEpisodes = sharedPreferences.getBoolean(PREF_DISPLAY_ONLY_EPISODES, false); } } public static float getLogicalDensity() { return LOGICAL_DENSITY; } + + public boolean displayOnlyEpisodes() { + return displayOnlyEpisodes; + } public boolean isLargeScreen() { return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE || (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE; diff --git a/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java b/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java index 9dbc225d0..e6824b122 100644 --- a/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java +++ b/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java @@ -7,7 +7,10 @@ import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.storage.DownloadRequester; import de.danoeh.antennapod.util.Converter; +import de.danoeh.antennapod.util.EpisodeFilter; +import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; +import android.widget.Adapter; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageButton; @@ -27,6 +30,7 @@ public class FeedItemlistAdapter extends ArrayAdapter { private OnClickListener onButActionClicked; private boolean showFeedtitle; private int selectedItemIndex; + private List objects; public static final int SELECTION_NONE = -1; @@ -34,6 +38,7 @@ public class FeedItemlistAdapter extends ArrayAdapter { List objects, OnClickListener onButActionClicked, boolean showFeedtitle) { super(context, textViewResourceId, objects); + this.objects = objects; this.onButActionClicked = onButActionClicked; this.showFeedtitle = showFeedtitle; this.selectedItemIndex = SELECTION_NONE; @@ -75,74 +80,79 @@ public class FeedItemlistAdapter extends ArrayAdapter { } else { holder = (Holder) convertView.getTag(); } + if (!(getItemViewType(position) == Adapter.IGNORE_ITEM_VIEW_TYPE)) { + convertView.setVisibility(View.VISIBLE); + if (position == selectedItemIndex) { + convertView.setBackgroundColor(convertView.getResources() + .getColor(R.color.selection_background)); + } else { + convertView.setBackgroundResource(0); + } - if (position == selectedItemIndex) { - convertView.setBackgroundColor(convertView.getResources().getColor( - R.color.selection_background)); + holder.title.setText(item.getTitle()); + if (showFeedtitle) { + holder.feedtitle.setVisibility(View.VISIBLE); + holder.feedtitle.setText(item.getFeed().getTitle()); + } + if (!item.isRead()) { + holder.title.setTypeface(Typeface.DEFAULT_BOLD); + } else { + holder.title.setTypeface(Typeface.DEFAULT); + } + + holder.published.setText(convertView.getResources().getString( + R.string.published_prefix) + + DateUtils.formatSameDayTime(item.getPubDate().getTime(), + System.currentTimeMillis(), DateFormat.SHORT, + DateFormat.SHORT)); + + if (item.getMedia() == null) { + holder.encInfo.setVisibility(View.GONE); + } else { + holder.encInfo.setVisibility(View.VISIBLE); + if (FeedManager.getInstance().isInQueue(item)) { + holder.inPlaylist.setVisibility(View.VISIBLE); + } else { + holder.inPlaylist.setVisibility(View.GONE); + } + if (item.getMedia().isDownloaded()) { + holder.lenSize.setText(convertView.getResources() + .getString(R.string.length_prefix) + + Converter.getDurationStringLong(item.getMedia() + .getDuration())); + holder.downloaded.setVisibility(View.VISIBLE); + } else { + holder.lenSize + .setText(convertView.getResources().getString( + R.string.size_prefix) + + Converter.byteToString(item.getMedia() + .getSize())); + holder.downloaded.setVisibility(View.GONE); + } + + if (DownloadRequester.getInstance().isDownloadingFile( + item.getMedia())) { + holder.downloading.setVisibility(View.VISIBLE); + } else { + holder.downloading.setVisibility(View.GONE); + } + + String type = item.getMedia().getMime_type(); + + if (type.startsWith("audio")) { + holder.type.setImageResource(R.drawable.type_audio); + } else if (type.startsWith("video")) { + holder.type.setImageResource(R.drawable.type_video); + } else { + holder.type.setImageBitmap(null); + } + } + + holder.butAction.setFocusable(false); + holder.butAction.setOnClickListener(onButActionClicked); } else { - convertView.setBackgroundResource(0); + convertView.setVisibility(View.GONE); } - - holder.title.setText(item.getTitle()); - if (showFeedtitle) { - holder.feedtitle.setVisibility(View.VISIBLE); - holder.feedtitle.setText(item.getFeed().getTitle()); - } - if (!item.isRead()) { - holder.title.setTypeface(Typeface.DEFAULT_BOLD); - } else { - holder.title.setTypeface(Typeface.DEFAULT); - } - - holder.published.setText(convertView.getResources().getString( - R.string.published_prefix) - + DateUtils.formatSameDayTime(item.getPubDate().getTime(), - System.currentTimeMillis(), DateFormat.SHORT, - DateFormat.SHORT)); - - if (item.getMedia() == null) { - holder.encInfo.setVisibility(View.GONE); - } else { - holder.encInfo.setVisibility(View.VISIBLE); - if (FeedManager.getInstance().isInQueue(item)) { - holder.inPlaylist.setVisibility(View.VISIBLE); - } else { - holder.inPlaylist.setVisibility(View.GONE); - } - if (item.getMedia().isDownloaded()) { - holder.lenSize.setText(convertView.getResources().getString( - R.string.length_prefix) - + Converter.getDurationStringLong(item.getMedia() - .getDuration())); - holder.downloaded.setVisibility(View.VISIBLE); - } else { - holder.lenSize.setText(convertView.getResources().getString( - R.string.size_prefix) - + Converter.byteToString(item.getMedia().getSize())); - holder.downloaded.setVisibility(View.GONE); - } - - if (DownloadRequester.getInstance().isDownloadingFile( - item.getMedia())) { - holder.downloading.setVisibility(View.VISIBLE); - } else { - holder.downloading.setVisibility(View.GONE); - } - - String type = item.getMedia().getMime_type(); - - if (type.startsWith("audio")) { - holder.type.setImageResource(R.drawable.type_audio); - } else if (type.startsWith("video")) { - holder.type.setImageResource(R.drawable.type_video); - } else { - holder.type.setImageBitmap(null); - } - } - - holder.butAction.setFocusable(false); - holder.butAction.setOnClickListener(onButActionClicked); - return convertView; } @@ -169,4 +179,22 @@ public class FeedItemlistAdapter extends ArrayAdapter { notifyDataSetChanged(); } + @Override + public int getCount() { + if (PodcastApp.getInstance().displayOnlyEpisodes()) { + return EpisodeFilter.countItemsWithEpisodes(objects); + } else { + return super.getCount(); + } + } + + @Override + public FeedItem getItem(int position) { + if (PodcastApp.getInstance().displayOnlyEpisodes()) { + return EpisodeFilter.accessEpisodeByIndex(objects, position); + } else { + return super.getItem(position); + } + } + } diff --git a/src/de/danoeh/antennapod/fragment/ItemlistFragment.java b/src/de/danoeh/antennapod/fragment/ItemlistFragment.java index e8df221f2..61f7d3256 100644 --- a/src/de/danoeh/antennapod/fragment/ItemlistFragment.java +++ b/src/de/danoeh/antennapod/fragment/ItemlistFragment.java @@ -102,14 +102,7 @@ public class ItemlistFragment extends SherlockListFragment implements feed = FeedManager.getInstance().getFeed(feedId); items = feed.getItems(); } - - boolean displayOnlyEpisodes = PreferenceManager - .getDefaultSharedPreferences(PodcastApp.getInstance()) - .getBoolean(PodcastApp.PREF_DISPLAY_ONLY_EPISODES, false); - if (displayOnlyEpisodes) { - items = EpisodeFilter.getEpisodeList(items); - } - + fila = new FeedItemlistAdapter(getActivity(), 0, items, onButActionClicked, showFeedtitle); setListAdapter(fila); @@ -170,15 +163,8 @@ public class ItemlistFragment extends SherlockListFragment implements DownloadRequester.ACTION_DOWNLOAD_QUEUED)) { updateProgressBarVisibility(); } else { - getActivity().runOnUiThread(new Runnable() { - - @Override - public void run() { - fila.notifyDataSetChanged(); - updateProgressBarVisibility(); - } - - }); + fila.notifyDataSetChanged(); + updateProgressBarVisibility(); } } }; diff --git a/src/de/danoeh/antennapod/fragment/QueueFragment.java b/src/de/danoeh/antennapod/fragment/QueueFragment.java index 48d2f3836..eeb24173d 100644 --- a/src/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/src/de/danoeh/antennapod/fragment/QueueFragment.java @@ -15,7 +15,8 @@ import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.R; public class QueueFragment extends ItemlistFragment { - + private static final String TAG = "QueueFragment"; + public QueueFragment() { super(FeedManager.getInstance().getQueue(), true); } diff --git a/src/de/danoeh/antennapod/util/EpisodeFilter.java b/src/de/danoeh/antennapod/util/EpisodeFilter.java index debfa926c..ee627b161 100644 --- a/src/de/danoeh/antennapod/util/EpisodeFilter.java +++ b/src/de/danoeh/antennapod/util/EpisodeFilter.java @@ -7,9 +7,9 @@ import de.danoeh.antennapod.feed.FeedItem; public class EpisodeFilter { private EpisodeFilter() { - + } - + /** Return a copy of the itemlist without items which have no media. */ public static ArrayList getEpisodeList(List items) { ArrayList episodes = new ArrayList(items); @@ -20,4 +20,30 @@ public class EpisodeFilter { } return episodes; } + + public static int countItemsWithEpisodes(List items) { + int count = 0; + for (FeedItem item : items) { + if (item.getMedia() != null) { + count++; + } + } + return count; + } + + public static FeedItem accessEpisodeByIndex(List items, + int position) { + int count = 0; + for (FeedItem item : items) { + + if (item.getMedia() != null) { + if (count == position) { + return item; + } else { + count++; + } + } + } + return null; + } }