support loading more headlines from main menu

This commit is contained in:
Andrew Dolgov 2011-11-26 09:52:50 +03:00
parent 10672b110f
commit 131dec0c1c
4 changed files with 56 additions and 14 deletions

View File

@ -20,7 +20,12 @@
android:title="@string/close_article" android:title="@string/close_article"
android:showAsAction="ifRoom|withText"/> android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/load_more_articles"
android:visible="false"
android:icon="@android:drawable/ic_menu_more"
android:title="@string/load_more_articles"
android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/update_feeds" <item android:id="@+id/update_feeds"
android:enabled="false" android:enabled="false"
android:icon="@android:drawable/ic_menu_rotate" android:icon="@android:drawable/ic_menu_rotate"

View File

@ -33,4 +33,5 @@
<string name="share_article">Share article</string> <string name="share_article">Share article</string>
<string name="could_not_decode_content">Could not decode content (UnsupportedEncodingException)</string> <string name="could_not_decode_content">Could not decode content (UnsupportedEncodingException)</string>
<string name="sort_feeds_by_unread">Sort feeds by unread count</string> <string name="sort_feeds_by_unread">Sort feeds by unread count</string>
<string name="load_more_articles">More headlines...</string>
</resources> </resources>

View File

@ -79,7 +79,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
Log.d(TAG, "onCreateView, feed=" + m_feed); Log.d(TAG, "onCreateView, feed=" + m_feed);
if (m_feed != null && (m_articles == null || m_articles.size() == 0)) if (m_feed != null && (m_articles == null || m_articles.size() == 0))
refresh(); refresh(false);
else else
view.findViewById(R.id.loading_container).setVisibility(View.GONE); 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(); HeadlinesRequest req = new HeadlinesRequest();
req.setApi(m_prefs.getString("ttrss_url", null)); req.setApi(m_prefs.getString("ttrss_url", null));
final String sessionId = ((MainActivity)getActivity()).getSessionId(); 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<String,String> map = new HashMap<String,String>() { HashMap<String,String> map = new HashMap<String,String>() {
{ {
put("op", "getHeadlines"); put("op", "getHeadlines");
@ -137,6 +150,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
put("limit", String.valueOf(30)); put("limit", String.valueOf(30));
put("offset", String.valueOf(0)); put("offset", String.valueOf(0));
put("view_mode", "adaptive"); 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 { private class HeadlinesRequest extends ApiRequest {
int m_offset = 0;
protected void onPostExecute(JsonElement result) { protected void onPostExecute(JsonElement result) {
if (result != null) { if (result != null) {
@ -172,12 +187,14 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
getActivity().runOnUiThread(new Runnable() { getActivity().runOnUiThread(new Runnable() {
public void run() { public void run() {
m_articles.clear();
if (m_offset == 0)
m_articles.clear();
for (Article f : articles) for (Article f : articles)
m_articles.add(f); m_articles.add(f);
m_adapter.notifyDataSetInvalidated(); m_adapter.notifyDataSetChanged();
showLoading(false); showLoading(false);
} }
@ -199,6 +216,10 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
return; return;
} }
public void setOffset(int skip) {
m_offset = skip;
}
} }
public void catchupArticle(final Article article) { public void catchupArticle(final Article article) {

View File

@ -225,6 +225,9 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
case R.id.close_article: case R.id.close_article:
closeArticle(); closeArticle();
return true; return true;
case R.id.load_more_articles:
viewFeed(m_activeFeed, true);
return true;
case R.id.share_article: case R.id.share_article:
shareArticle(m_selectedArticle); shareArticle(m_selectedArticle);
return true; 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.update_feeds).setEnabled(true);
m_menu.findItem(R.id.show_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 { } else {
m_menu.findItem(R.id.login).setVisible(true); m_menu.findItem(R.id.login).setVisible(true);
m_menu.findItem(R.id.logout).setVisible(false); m_menu.findItem(R.id.logout).setVisible(false);
m_menu.findItem(R.id.close_article).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.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.update_feeds).setEnabled(false);
m_menu.findItem(R.id.show_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 @Override
public void onFeedSelected(Feed feed) { public void onFeedSelected(Feed feed) {
Log.d(TAG, "Selected feed: " + feed.toString()); Log.d(TAG, "Selected feed: " + feed.toString());
viewFeed(feed); viewFeed(feed, false);
} }
public Article getSelectedArticle() { public Article getSelectedArticle() {
return m_selectedArticle; return m_selectedArticle;
} }
public void viewFeed(Feed feed) { public void viewFeed(Feed feed, boolean append) {
m_activeFeed = feed; m_activeFeed = feed;
initMainMenu();
HeadlinesFragment hf = new HeadlinesFragment(); if (!append) {
HeadlinesFragment hf = new HeadlinesFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.headlines_fragment, hf); ft.replace(R.id.headlines_fragment, hf);
ft.commit(); ft.commit();
} else {
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
if (hf != null) {
hf.refresh(true);
}
}
} }
public void openArticle(Article article) { public void openArticle(Article article) {