Itemlists were not updating their content properly if "Display only

episodes'-preference was enabled
This commit is contained in:
daniel oeh 2012-08-19 16:27:37 +02:00
parent c772471268
commit 9513099a10
5 changed files with 136 additions and 85 deletions

View File

@ -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;

View File

@ -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<FeedItem> {
private OnClickListener onButActionClicked;
private boolean showFeedtitle;
private int selectedItemIndex;
private List<FeedItem> objects;
public static final int SELECTION_NONE = -1;
@ -34,6 +38,7 @@ public class FeedItemlistAdapter extends ArrayAdapter<FeedItem> {
List<FeedItem> 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<FeedItem> {
} 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<FeedItem> {
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);
}
}
}

View File

@ -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();
}
}
};

View File

@ -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);
}

View File

@ -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<FeedItem> getEpisodeList(List<FeedItem> items) {
ArrayList<FeedItem> episodes = new ArrayList<FeedItem>(items);
@ -20,4 +20,30 @@ public class EpisodeFilter {
}
return episodes;
}
public static int countItemsWithEpisodes(List<FeedItem> items) {
int count = 0;
for (FeedItem item : items) {
if (item.getMedia() != null) {
count++;
}
}
return count;
}
public static FeedItem accessEpisodeByIndex(List<FeedItem> items,
int position) {
int count = 0;
for (FeedItem item : items) {
if (item.getMedia() != null) {
if (count == position) {
return item;
} else {
count++;
}
}
}
return null;
}
}