From e38746f906f9ddfb7600b2113f8934bc4117e6ec Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Mon, 23 Sep 2013 10:37:43 +0200 Subject: [PATCH] Statistics for Feeds with no items are now loaded correctly. fixes #283 --- .../antennapod/adapter/FeedlistAdapter.java | 2 +- .../storage/FeedItemStatistics.java | 30 ++++++++++++++++++- .../antennapod/storage/PodDBAdapter.java | 5 ++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/de/danoeh/antennapod/adapter/FeedlistAdapter.java b/src/de/danoeh/antennapod/adapter/FeedlistAdapter.java index 10013d7f2..37600838e 100644 --- a/src/de/danoeh/antennapod/adapter/FeedlistAdapter.java +++ b/src/de/danoeh/antennapod/adapter/FeedlistAdapter.java @@ -89,7 +89,7 @@ public class FeedlistAdapter extends BaseAdapter { if (DownloadRequester.getInstance().isDownloadingFile(feed)) { holder.lastUpdate.setText(R.string.refreshing_label); } else { - if (feedItemStatistics.getNumberOfItems() > 0) { + if (feedItemStatistics.lastUpdateKnown()) { holder.lastUpdate.setText(convertView.getResources().getString( R.string.most_recent_prefix) + DateUtils.getRelativeTimeSpanString( diff --git a/src/de/danoeh/antennapod/storage/FeedItemStatistics.java b/src/de/danoeh/antennapod/storage/FeedItemStatistics.java index 6b79dd144..8cb040756 100644 --- a/src/de/danoeh/antennapod/storage/FeedItemStatistics.java +++ b/src/de/danoeh/antennapod/storage/FeedItemStatistics.java @@ -11,13 +11,29 @@ public class FeedItemStatistics { private int numberOfNewItems; private int numberOfInProgressItems; private Date lastUpdate; + private static final Date UNKNOWN_DATE = new Date(0); + + /** + * Creates new FeedItemStatistics object. + * + * @param feedID ID of the feed. + * @param numberOfItems Number of items that this feed has. + * @param numberOfNewItems Number of unread items this feed has. + * @param numberOfInProgressItems Number of items that the user has started listening to. + * @param lastUpdate pubDate of the latest episode. A lastUpdate value of 0 will be interpreted as DATE_UNKOWN if + * numberOfItems is 0. + */ public FeedItemStatistics(long feedID, int numberOfItems, int numberOfNewItems, int numberOfInProgressItems, Date lastUpdate) { this.feedID = feedID; this.numberOfItems = numberOfItems; this.numberOfNewItems = numberOfNewItems; this.numberOfInProgressItems = numberOfInProgressItems; - this.lastUpdate = (lastUpdate != null) ? (Date) lastUpdate.clone() : null; + if (numberOfItems > 0) { + this.lastUpdate = (lastUpdate != null) ? (Date) lastUpdate.clone() : null; + } else { + this.lastUpdate = UNKNOWN_DATE; + } } public long getFeedID() { @@ -36,7 +52,19 @@ public class FeedItemStatistics { return numberOfInProgressItems; } + /** + * Returns the pubDate of the latest item in the feed. Users of this method + * should check if this value is unkown or not by calling lastUpdateKnown() first. + */ public Date getLastUpdate() { return (lastUpdate != null) ? (Date) lastUpdate.clone() : null; } + + /** + * Returns true if the lastUpdate value is known. The lastUpdate value is unkown if the + * feed has no items. + */ + public boolean lastUpdateKnown() { + return lastUpdate != UNKNOWN_DATE; + } } diff --git a/src/de/danoeh/antennapod/storage/PodDBAdapter.java b/src/de/danoeh/antennapod/storage/PodDBAdapter.java index 6d41f6dfd..e22fe56c9 100644 --- a/src/de/danoeh/antennapod/storage/PodDBAdapter.java +++ b/src/de/danoeh/antennapod/storage/PodDBAdapter.java @@ -1017,14 +1017,15 @@ public class PodDBAdapter { * Select number of items, new items, the date of the latest episode and the number of episodes in progress. The result * is sorted by the title of the feed. */ - private static final String FEED_STATISTICS_QUERY = "SELECT feed, num_items, new_items, latest_episode, in_progress FROM " + + private static final String FEED_STATISTICS_QUERY = "SELECT Feeds.id, num_items, new_items, latest_episode, in_progress FROM " + + " Feeds LEFT JOIN " + "(SELECT feed,count(*) AS num_items," + " COUNT(CASE WHEN read=0 THEN 1 END) AS new_items," + " MAX(pubDate) AS latest_episode," + " COUNT(CASE WHEN position>0 THEN 1 END) AS in_progress," + " COUNT(CASE WHEN downloaded=1 THEN 1 END) AS episodes_downloaded " + " FROM FeedItems LEFT JOIN FeedMedia ON FeedItems.id=FeedMedia.feeditem GROUP BY FeedItems.feed)" + - " INNER JOIN Feeds ON Feeds.id = feed ORDER BY Feeds.title;"; + " ON Feeds.id = feed ORDER BY Feeds.title;"; public Cursor getFeedStatisticsCursor() { return db.rawQuery(FEED_STATISTICS_QUERY, null);