From 131dec0c1cb8799f0321938bfdfd7b32eeb7dcbd Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sat, 26 Nov 2011 09:52:50 +0300 Subject: [PATCH] support loading more headlines from main menu --- res/menu/main_menu.xml | 7 +++++- res/values/strings.xml | 1 + src/org/fox/ttrss/HeadlinesFragment.java | 31 ++++++++++++++++++++---- src/org/fox/ttrss/MainActivity.java | 31 ++++++++++++++++++------ 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/res/menu/main_menu.xml b/res/menu/main_menu.xml index 97e713e4..0e5380a6 100644 --- a/res/menu/main_menu.xml +++ b/res/menu/main_menu.xml @@ -20,7 +20,12 @@ android:title="@string/close_article" android:showAsAction="ifRoom|withText"/> - + + Share article Could not decode content (UnsupportedEncodingException) Sort feeds by unread count + More headlines... diff --git a/src/org/fox/ttrss/HeadlinesFragment.java b/src/org/fox/ttrss/HeadlinesFragment.java index c8da3371..f3790ec9 100644 --- a/src/org/fox/ttrss/HeadlinesFragment.java +++ b/src/org/fox/ttrss/HeadlinesFragment.java @@ -79,7 +79,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { Log.d(TAG, "onCreateView, feed=" + m_feed); if (m_feed != null && (m_articles == null || m_articles.size() == 0)) - refresh(); + refresh(false); else view.findViewById(R.id.loading_container).setVisibility(View.GONE); @@ -121,13 +121,26 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { } } - public void refresh() { + public void refresh(boolean append) { HeadlinesRequest req = new HeadlinesRequest(); req.setApi(m_prefs.getString("ttrss_url", null)); final String sessionId = ((MainActivity)getActivity()).getSessionId(); - + int skip = 0; + + if (append) { + for (Article a : m_articles) { + if (a.unread) ++skip; + } + + if (skip == 0) skip = m_articles.size(); + } + + final int fskip = skip; + + req.setOffset(skip); + HashMap map = new HashMap() { { put("op", "getHeadlines"); @@ -137,6 +150,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { put("limit", String.valueOf(30)); put("offset", String.valueOf(0)); put("view_mode", "adaptive"); + put("skip", String.valueOf(fskip)); } }; @@ -154,6 +168,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { } private class HeadlinesRequest extends ApiRequest { + int m_offset = 0; protected void onPostExecute(JsonElement result) { if (result != null) { @@ -172,12 +187,14 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { getActivity().runOnUiThread(new Runnable() { public void run() { - m_articles.clear(); + + if (m_offset == 0) + m_articles.clear(); for (Article f : articles) m_articles.add(f); - m_adapter.notifyDataSetInvalidated(); + m_adapter.notifyDataSetChanged(); showLoading(false); } @@ -199,6 +216,10 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { return; } + + public void setOffset(int skip) { + m_offset = skip; + } } public void catchupArticle(final Article article) { diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java index 1c7207bf..ea5921b5 100644 --- a/src/org/fox/ttrss/MainActivity.java +++ b/src/org/fox/ttrss/MainActivity.java @@ -225,6 +225,9 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe case R.id.close_article: closeArticle(); return true; + case R.id.load_more_articles: + viewFeed(m_activeFeed, true); + return true; case R.id.share_article: shareArticle(m_selectedArticle); return true; @@ -285,14 +288,17 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe m_menu.findItem(R.id.update_feeds).setEnabled(true); m_menu.findItem(R.id.show_feeds).setEnabled(true); } - + + m_menu.findItem(R.id.load_more_articles).setVisible(m_activeFeed != null); + } else { m_menu.findItem(R.id.login).setVisible(true); m_menu.findItem(R.id.logout).setVisible(false); m_menu.findItem(R.id.close_article).setVisible(false); m_menu.findItem(R.id.share_article).setVisible(false); - + m_menu.findItem(R.id.load_more_articles).setVisible(false); + m_menu.findItem(R.id.update_feeds).setEnabled(false); m_menu.findItem(R.id.show_feeds).setEnabled(false); } @@ -377,21 +383,30 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe @Override public void onFeedSelected(Feed feed) { Log.d(TAG, "Selected feed: " + feed.toString()); - viewFeed(feed); + viewFeed(feed, false); } public Article getSelectedArticle() { return m_selectedArticle; } - public void viewFeed(Feed feed) { + public void viewFeed(Feed feed, boolean append) { m_activeFeed = feed; + + initMainMenu(); - HeadlinesFragment hf = new HeadlinesFragment(); + if (!append) { + HeadlinesFragment hf = new HeadlinesFragment(); - FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - ft.replace(R.id.headlines_fragment, hf); - ft.commit(); + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + ft.replace(R.id.headlines_fragment, hf); + ft.commit(); + } else { + HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment); + if (hf != null) { + hf.refresh(true); + } + } } public void openArticle(Article article) {