From ef5dc7cb0e839ae2f80e3acf6c4c41bcf63f0a94 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Fri, 16 Nov 2012 19:55:59 +0100 Subject: [PATCH] Added state enum to feeditem class The getStatus() method should now be used to handle feeditems in different states instead of handling its attributes directly. --- .../adapter/FeedItemlistAdapter.java | 17 +++++++++++------ src/de/danoeh/antennapod/feed/Feed.java | 10 ++++++---- src/de/danoeh/antennapod/feed/FeedItem.java | 18 ++++++++++++++++-- src/de/danoeh/antennapod/feed/FeedManager.java | 4 ++-- .../util/menuhandler/FeedItemMenuHandler.java | 12 ++++++------ 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java b/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java index e7d2c322c..44e954b55 100644 --- a/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java +++ b/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java @@ -92,26 +92,31 @@ public class FeedItemlistAdapter extends ArrayAdapter { holder.feedtitle.setVisibility(View.VISIBLE); holder.feedtitle.setText(item.getFeed().getTitle()); } - - if (item.isPlaying()) { + + FeedItem.State state = item.getState(); + switch (state) { + case PLAYING: holder.title.setTypeface(Typeface.DEFAULT_BOLD); holder.statusLabel.setBackgroundColor(convertView .getResources().getColor(R.color.status_playing)); holder.statusLabel.setVisibility(View.VISIBLE); - - } else if (item.isInProgress()) { + break; + case IN_PROGRESS: holder.title.setTypeface(Typeface.DEFAULT_BOLD); holder.statusLabel.setBackgroundColor(convertView .getResources().getColor(R.color.status_progress)); holder.statusLabel.setVisibility(View.VISIBLE); - } else if (!item.isRead()) { + break; + case NEW: holder.title.setTypeface(Typeface.DEFAULT_BOLD); holder.statusLabel.setBackgroundColor(convertView .getResources().getColor(R.color.status_unread)); holder.statusLabel.setVisibility(View.VISIBLE); - } else { + break; + default: holder.title.setTypeface(Typeface.DEFAULT); holder.statusLabel.setVisibility(View.INVISIBLE); + break; } holder.published.setText(convertView.getResources().getString( diff --git a/src/de/danoeh/antennapod/feed/Feed.java b/src/de/danoeh/antennapod/feed/Feed.java index 8ca82be62..7640ef7d1 100644 --- a/src/de/danoeh/antennapod/feed/Feed.java +++ b/src/de/danoeh/antennapod/feed/Feed.java @@ -74,7 +74,7 @@ public class Feed extends FeedFile { .getBoolean(PodcastApp.PREF_DISPLAY_ONLY_EPISODES, false); for (FeedItem item : items) { - if (!item.isRead()) { + if (item.getState() == FeedItem.State.NEW) { if (!displayOnlyEpisodes || item.getMedia() != null) { count++; } @@ -91,7 +91,9 @@ public class Feed extends FeedFile { int count = 0; for (FeedItem item : items) { - if (item.isInProgress()) { + FeedItem.State state = item.getState(); + if (state == FeedItem.State.IN_PROGRESS + || state == FeedItem.State.PLAYING) { count++; } } @@ -108,7 +110,7 @@ public class Feed extends FeedFile { .getDefaultSharedPreferences(PodcastApp.getInstance()) .getBoolean(PodcastApp.PREF_DISPLAY_ONLY_EPISODES, false); for (FeedItem item : items) { - if (!item.isRead()) { + if (item.getState() == FeedItem.State.NEW) { if (!displayOnlyEpisodes || item.getMedia() != null) { return true; } @@ -159,7 +161,7 @@ public class Feed extends FeedFile { return download_url; } } - + /** Calls cacheDescriptions on all items. */ protected void cacheDescriptionsOfItems() { if (items != null) { diff --git a/src/de/danoeh/antennapod/feed/FeedItem.java b/src/de/danoeh/antennapod/feed/FeedItem.java index 7ce58d081..fa5c49436 100644 --- a/src/de/danoeh/antennapod/feed/FeedItem.java +++ b/src/de/danoeh/antennapod/feed/FeedItem.java @@ -157,7 +157,7 @@ public class FeedItem extends FeedComponent { } } - public boolean isInProgress() { + private boolean isInProgress() { return (media != null && media.isInProgress()); } @@ -201,7 +201,7 @@ public class FeedItem extends FeedComponent { return media != null; } - public boolean isPlaying() { + private boolean isPlaying() { if (media != null) { if (PodcastApp.getCurrentlyPlayingMediaId() == media.getId()) { return true; @@ -217,4 +217,18 @@ public class FeedItem extends FeedComponent { public void setCachedContentEncoded(String c) { cachedContentEncoded = new SoftReference(c); } + + public enum State {NEW, IN_PROGRESS, READ, PLAYING} + + public State getState() { + if (hasMedia()) { + if (isPlaying()) { + return State.PLAYING; + } + if (isInProgress()) { + return State.IN_PROGRESS; + } + } + return (isRead() ? State.READ : State.NEW); + } } diff --git a/src/de/danoeh/antennapod/feed/FeedManager.java b/src/de/danoeh/antennapod/feed/FeedManager.java index 0666261d4..03a0637ae 100644 --- a/src/de/danoeh/antennapod/feed/FeedManager.java +++ b/src/de/danoeh/antennapod/feed/FeedManager.java @@ -216,7 +216,7 @@ public class FeedManager { } // delete stored media files and mark them as read for (FeedItem item : feed.getItems()) { - if (!item.isRead()) { + if (item.getState() == FeedItem.State.NEW) { unreadItems.remove(item); } if (queue.contains(item)) { @@ -1173,7 +1173,7 @@ public class FeedManager { : false; item.setItemIdentifier(itemlistCursor .getString(PodDBAdapter.IDX_FI_SMALL_ITEM_IDENTIFIER)); - if (!item.read) { + if (item.getState() == FeedItem.State.NEW) { unreadItems.add(item); } diff --git a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java index 2c345fe0a..82de9a204 100644 --- a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java +++ b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java @@ -12,6 +12,7 @@ import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.FlattrClickWorker; import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.feed.FeedItem.State; import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.storage.DownloadRequester; @@ -32,6 +33,7 @@ public class FeedItemMenuHandler { && requester.isDownloadingFile(selectedItem.getMedia()); boolean notLoadedAndNotLoading = hasMedia && (!downloaded) && (!downloading); + FeedItem.State state = selectedItem.getState(); menu.findItem(R.id.play_item).setVisible(downloaded); menu.findItem(R.id.remove_item).setVisible(downloaded); @@ -50,13 +52,11 @@ public class FeedItemMenuHandler { selectedItem.getLink() != null); menu.findItem(R.id.mark_unread_item).setVisible( - !selectedItem.isPlaying() - && (selectedItem.isRead() || selectedItem - .isInProgress())); + state == FeedItem.State.IN_PROGRESS + || state == FeedItem.State.READ); menu.findItem(R.id.mark_read_item).setVisible( - !selectedItem.isPlaying() - && (!selectedItem.isRead() || selectedItem - .isInProgress())); + state == FeedItem.State.NEW + || state == FeedItem.State.IN_PROGRESS); if (selectedItem.getLink() != null) { menu.findItem(R.id.visit_website_item).setVisible(true);