From 14845e9a422e15462467cb0d2fe0582f9d387026 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Sun, 17 Jun 2012 13:58:36 +0200 Subject: [PATCH] Added list which shows unread items --- .../activity/PodfetcherActivity.java | 15 +++-- src/de/podfetcher/feed/FeedItem.java | 10 ++-- .../feed/FeedItemPubdateComparator.java | 14 +++++ src/de/podfetcher/feed/FeedManager.java | 59 ++++++++++++++----- .../fragment/FeedItemlistFragment.java | 16 ++--- .../fragment/UnreadItemlistFragment.java | 13 ++++ 6 files changed, 92 insertions(+), 35 deletions(-) create mode 100644 src/de/podfetcher/feed/FeedItemPubdateComparator.java create mode 100644 src/de/podfetcher/fragment/UnreadItemlistFragment.java diff --git a/src/de/podfetcher/activity/PodfetcherActivity.java b/src/de/podfetcher/activity/PodfetcherActivity.java index 6487c018b..b9cb3883f 100644 --- a/src/de/podfetcher/activity/PodfetcherActivity.java +++ b/src/de/podfetcher/activity/PodfetcherActivity.java @@ -10,18 +10,21 @@ import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.SherlockFragmentActivity; import de.podfetcher.R; +import de.podfetcher.fragment.FeedItemlistFragment; import de.podfetcher.fragment.FeedlistFragment; +import de.podfetcher.fragment.UnreadItemlistFragment; public class PodfetcherActivity extends SherlockFragmentActivity { private static final String TAG = "PodfetcherActivity"; private FeedlistFragment feedlist; + FeedItemlistFragment unreadList; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); - // Set up tabs ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); @@ -36,13 +39,14 @@ public class PodfetcherActivity extends SherlockFragmentActivity { FeedlistFragment.class)); actionBar.addTab(tab); + tab = actionBar .newTab() .setText(getText(R.string.new_label).toString()) .setTabListener( - new TabListener(this, getText( + new TabListener(this, getText( R.string.new_label).toString(), - FeedlistFragment.class)); + UnreadItemlistFragment.class)); actionBar.addTab(tab); } @@ -54,7 +58,6 @@ public class PodfetcherActivity extends SherlockFragmentActivity { private final String tag; private final Class fClass; private Fragment fragment; - private boolean attachedOnce = false; public TabListener(Activity activity, String tag, Class fClass) { this.activity = activity; @@ -76,10 +79,6 @@ public class PodfetcherActivity extends SherlockFragmentActivity { if (fragment == null) { fragment = Fragment.instantiate(activity, fClass.getName()); ft.replace(R.id.main_fragment, fragment); - attachedOnce = true; - } else if (!attachedOnce) { - ft.replace(R.id.main_fragment, fragment); - attachedOnce = true; } else { ft.attach(fragment); } diff --git a/src/de/podfetcher/feed/FeedItem.java b/src/de/podfetcher/feed/FeedItem.java index 129c25b4b..0d4ce4977 100644 --- a/src/de/podfetcher/feed/FeedItem.java +++ b/src/de/podfetcher/feed/FeedItem.java @@ -8,14 +8,14 @@ import java.util.Date; * @author daniel * */ -public class FeedItem extends FeedComponent{ +public class FeedItem extends FeedComponent implements Comparable{ private String title; private String description; private String link; private Date pubDate; private FeedMedia media; private Feed feed; - private boolean read; + protected boolean read; public FeedItem() { this.read = true; @@ -85,8 +85,10 @@ public class FeedItem extends FeedComponent{ return read; } - public void setRead(boolean read) { - this.read = read; + @Override + public int compareTo(FeedItem another) { + long diff = pubDate.getTime() - another.getPubDate().getTime(); + return (int) Math.signum(diff); } diff --git a/src/de/podfetcher/feed/FeedItemPubdateComparator.java b/src/de/podfetcher/feed/FeedItemPubdateComparator.java new file mode 100644 index 000000000..90967b72b --- /dev/null +++ b/src/de/podfetcher/feed/FeedItemPubdateComparator.java @@ -0,0 +1,14 @@ +package de.podfetcher.feed; + +import java.util.Comparator; + +/** Compares the pubDate of two FeedItems */ +public class FeedItemPubdateComparator implements Comparator { + + @Override + public int compare(FeedItem lhs, FeedItem rhs) { + long diff = lhs.getPubDate().getTime() - rhs.getPubDate().getTime(); + return (int) Math.signum(diff); + } + +} diff --git a/src/de/podfetcher/feed/FeedManager.java b/src/de/podfetcher/feed/FeedManager.java index 716aa36f7..6aab35159 100644 --- a/src/de/podfetcher/feed/FeedManager.java +++ b/src/de/podfetcher/feed/FeedManager.java @@ -24,11 +24,15 @@ public class FeedManager { private ArrayList feeds; private ArrayList categories; + + /** Contains all items where 'read' is false */ + private ArrayList unreadItems; private DownloadRequester requester; private FeedManager() { feeds = new ArrayList(); categories = new ArrayList(); + unreadItems = new ArrayList(); requester = DownloadRequester.getInstance(); } @@ -39,19 +43,21 @@ public class FeedManager { } return singleton; } - - /** Play FeedMedia and start the playback service + launch Mediaplayer Activity. */ + + /** + * Play FeedMedia and start the playback service + launch Mediaplayer + * Activity. + */ public void playMedia(Context context, FeedMedia media) { // Start playback Service - Intent launchIntent = new Intent(context, - PlaybackService.class); + Intent launchIntent = new Intent(context, PlaybackService.class); launchIntent.putExtra(PlaybackService.EXTRA_MEDIA_ID, media.getId()); - launchIntent.putExtra(PlaybackService.EXTRA_FEED_ID, media.getItem().getFeed().getId()); + launchIntent.putExtra(PlaybackService.EXTRA_FEED_ID, media.getItem() + .getFeed().getId()); context.startService(launchIntent); // Launch Mediaplayer - Intent playerIntent = new Intent(context, - MediaplayerActivity.class); + Intent playerIntent = new Intent(context, MediaplayerActivity.class); context.startActivity(playerIntent); } @@ -65,12 +71,26 @@ public class FeedManager { } media.setDownloaded(false); media.setFile_url(""); - setFeedMedia(context, media); + setFeedMedia(context, media); } Log.d(TAG, "Deleting File. Result: " + result); return result; } + /** + * Sets the 'read'-attribute of a FeedItem. Should be used by all Classes + * instead of the setters of FeedItem. + */ + public void markItemRead(Context context, FeedItem item, boolean read) { + item.read = read; + setFeedItem(context, item); + if (read == true) { + unreadItems.remove(item); + } else { + unreadItems.add(item); + } + } + public void refreshAllFeeds(Context context) { Log.d(TAG, "Refreshing all feeds."); for (Feed feed : feeds) { @@ -86,8 +106,8 @@ public class FeedManager { setFeedItem(context, item); } } - - /** Adds a new Feeditem if its not in the list */ +/* TODO Decide if still useful + public void addFeedItem(Context context, FeedItem item) { PodDBAdapter adapter = new PodDBAdapter(context); // Search list for feeditem @@ -104,7 +124,7 @@ public class FeedManager { item.id = adapter.setFeedItem(item); } } - +*/ public void updateFeed(Context context, Feed newFeed) { // Look up feed in the feedslist Feed savedFeed = searchFeedByLink(newFeed.getLink()); @@ -113,7 +133,7 @@ public class FeedManager { "Found no existing Feed with title " + newFeed.getTitle() + ". Adding as new one."); // Add a new Feed - newFeed.getItems().get(0).setRead(false); + markItemRead(context, newFeed.getItems().get(0), false); addNewFeed(context, newFeed); } else { Log.d(TAG, "Feed with title " + newFeed.getTitle() @@ -125,6 +145,7 @@ public class FeedManager { if (oldItem == null) { // item is new savedFeed.getItems().add(item); + markItemRead(context, item, false); } } savedFeed.setLastUpdate(newFeed.getLastUpdate()); @@ -293,10 +314,12 @@ public class FeedManager { item.setMedia(adapter.getFeedMedia(itemlistCursor .getLong(itemlistCursor .getColumnIndex(PodDBAdapter.KEY_MEDIA)), item)); - item.setRead((itemlistCursor.getInt(itemlistCursor + item.read = (itemlistCursor.getInt(itemlistCursor .getColumnIndex(PodDBAdapter.KEY_READ)) > 0) ? true - : false); - + : false; + if (!item.read) { + unreadItems.add(item); + } items.add(item); } while (itemlistCursor.moveToNext()); } @@ -308,4 +331,10 @@ public class FeedManager { return feeds; } + public ArrayList getUnreadItems() { + return unreadItems; + } + + + } diff --git a/src/de/podfetcher/fragment/FeedItemlistFragment.java b/src/de/podfetcher/fragment/FeedItemlistFragment.java index e14da78cd..00d55905f 100644 --- a/src/de/podfetcher/fragment/FeedItemlistFragment.java +++ b/src/de/podfetcher/fragment/FeedItemlistFragment.java @@ -26,15 +26,15 @@ public class FeedItemlistFragment extends SherlockListFragment { private static final String TAG = "FeedItemlistFragment"; public static final String EXTRA_SELECTED_FEEDITEM = "extra.de.podfetcher.activity.selected_feeditem"; - private FeedItemlistAdapter fila; - private FeedManager manager; - private DownloadRequester requester; + protected FeedItemlistAdapter fila; + protected FeedManager manager; + protected DownloadRequester requester; /** The feed which the activity displays */ - private ArrayList items; + protected ArrayList items; - private FeedItem selectedItem; - private ActionMode mActionMode; + protected FeedItem selectedItem; + protected ActionMode mActionMode; public FeedItemlistFragment(ArrayList items) { super(); @@ -150,10 +150,10 @@ public class FeedItemlistFragment extends SherlockListFragment { .getMedia().getDownloadId()); break; case R.id.mark_read_item: - selectedItem.setRead(true); + manager.markItemRead(getSherlockActivity(), selectedItem, true); break; case R.id.mark_unread_item: - selectedItem.setRead(false); + manager.markItemRead(getSherlockActivity(), selectedItem, false); break; } diff --git a/src/de/podfetcher/fragment/UnreadItemlistFragment.java b/src/de/podfetcher/fragment/UnreadItemlistFragment.java new file mode 100644 index 000000000..6d2e71226 --- /dev/null +++ b/src/de/podfetcher/fragment/UnreadItemlistFragment.java @@ -0,0 +1,13 @@ +package de.podfetcher.fragment; + +import de.podfetcher.feed.FeedManager; + +/** Contains all unread items. */ +public class UnreadItemlistFragment extends FeedItemlistFragment { + + public UnreadItemlistFragment() { + super(FeedManager.getInstance().getUnreadItems()); + + } + +}