From c52ea94c4418626847c19bb6346a1e81d536e5ec Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 16 Sep 2012 19:25:47 +0400 Subject: [PATCH] more experimental stuff --- res/layout/headlines.xml | 2 +- res/menu/main_menu.xml | 43 +++++- res/menu/online_menu.xml | 29 ---- src/org/fox/ttrss/ArticlePager.java | 33 +++-- src/org/fox/ttrss/FeedsActivity.java | 79 +++++++---- src/org/fox/ttrss/HeadlinesActivity.java | 9 +- src/org/fox/ttrss/HeadlinesEventListener.java | 2 +- src/org/fox/ttrss/OnlineActivity.java | 125 +++++++++++++++++- 8 files changed, 249 insertions(+), 73 deletions(-) delete mode 100644 res/menu/online_menu.xml diff --git a/res/layout/headlines.xml b/res/layout/headlines.xml index 60c24b91..c3027217 100644 --- a/res/layout/headlines.xml +++ b/res/layout/headlines.xml @@ -19,7 +19,7 @@ diff --git a/res/menu/main_menu.xml b/res/menu/main_menu.xml index 68625729..4bacf2f9 100644 --- a/res/menu/main_menu.xml +++ b/res/menu/main_menu.xml @@ -2,6 +2,41 @@ + + + + + + + + + + + + + - + android:title="@string/close_feed"/> --> @@ -105,11 +140,11 @@ android:showAsAction="" android:title="@string/article_set_note"/> - + android:title="@string/close_article"/> --> diff --git a/res/menu/online_menu.xml b/res/menu/online_menu.xml deleted file mode 100644 index d299eb6f..00000000 --- a/res/menu/online_menu.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/org/fox/ttrss/ArticlePager.java b/src/org/fox/ttrss/ArticlePager.java index 9818e08c..dcd5adb1 100644 --- a/src/org/fox/ttrss/ArticlePager.java +++ b/src/org/fox/ttrss/ArticlePager.java @@ -1,6 +1,7 @@ package org.fox.ttrss; import org.fox.ttrss.types.Article; +import org.fox.ttrss.types.ArticleList; import android.app.Activity; import android.os.Bundle; @@ -17,8 +18,8 @@ public class ArticlePager extends Fragment { private final String TAG = "ArticlePager"; private PagerAdapter m_adapter; private HeadlinesEventListener m_onlineServices; - private HeadlinesFragment m_hf; private Article m_article; + private ArticleList m_articles; private class PagerAdapter extends FragmentStatePagerAdapter { @@ -28,7 +29,7 @@ public class ArticlePager extends Fragment { @Override public Fragment getItem(int position) { - Article article = m_hf.getArticleAtPosition(position); + Article article = m_articles.get(position); if (article != null) { ArticleFragment af = new ArticleFragment(article); @@ -39,7 +40,7 @@ public class ArticlePager extends Fragment { @Override public int getCount() { - return m_hf.getAllArticles().size(); + return m_articles.size(); } } @@ -48,21 +49,27 @@ public class ArticlePager extends Fragment { super(); } - public ArticlePager(Article article) { + public ArticlePager(Article article, ArticleList articles) { super(); m_article = article; + m_articles = articles; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.article_pager, container, false); + if (savedInstanceState != null) { + m_articles = savedInstanceState.getParcelable("articles"); + m_article = savedInstanceState.getParcelable("article"); + } + m_adapter = new PagerAdapter(getActivity().getSupportFragmentManager()); ViewPager pager = (ViewPager) view.findViewById(R.id.article_pager); - int position = m_hf.getArticlePosition(m_article); + int position = m_articles.indexOf(m_article); pager.setAdapter(m_adapter); pager.setCurrentItem(position); @@ -78,7 +85,7 @@ public class ArticlePager extends Fragment { @Override public void onPageSelected(int position) { - Article article = m_hf.getArticleAtPosition(position); + Article article = m_articles.get(position); if (article != null) { if (article.unread) { @@ -90,7 +97,8 @@ public class ArticlePager extends Fragment { //Log.d(TAG, "Page #" + position + "/" + m_adapter.getCount()); if (position == m_adapter.getCount() - 5) { - m_hf.refresh(true); + // FIXME load more articles somehow + //m_hf.refresh(true); m_adapter.notifyDataSetChanged(); } } @@ -100,12 +108,21 @@ public class ArticlePager extends Fragment { return view; } + @Override + public void onSaveInstanceState(Bundle out) { + super.onSaveInstanceState(out); + + out.putParcelable("articles", m_articles); + out.putParcelable("article", m_article); + } + @Override public void onAttach(Activity activity) { super.onAttach(activity); - m_hf = (HeadlinesFragment) getActivity().getSupportFragmentManager().findFragmentByTag(CommonActivity.FRAG_HEADLINES); m_onlineServices = (HeadlinesEventListener)activity; + ((OnlineActivity)getActivity()).initMenu(); + } } diff --git a/src/org/fox/ttrss/FeedsActivity.java b/src/org/fox/ttrss/FeedsActivity.java index 5cc1d176..0a372aa8 100644 --- a/src/org/fox/ttrss/FeedsActivity.java +++ b/src/org/fox/ttrss/FeedsActivity.java @@ -25,9 +25,6 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe protected SharedPreferences m_prefs; - private boolean m_unreadOnly = true; - private boolean m_unreadArticlesOnly = true; - @Override public void onCreate(Bundle savedInstanceState) { m_prefs = PreferenceManager @@ -55,36 +52,46 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe } ft.commit(); - } else if (isSmallScreen()) { + } /* else if (isSmallScreen()) { Fragment frag = getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE); if (frag != null) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.remove(frag); ft.commit(); - } - } - } - - public boolean getUnreadOnly() { - return m_unreadOnly; + } + } */ } @Override protected void initMenu() { super.initMenu(); - if (m_menu != null) { + + Log.d(TAG, "initMenu: " + m_menu); + + if (m_menu != null && m_sessionId != null) { Fragment ff = getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS); Fragment cf = getSupportFragmentManager().findFragmentByTag(FRAG_CATS); Fragment af = getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE); + + HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); + + Log.d(TAG, "ff/cf/af/hf " + ff + " " + cf + " " + af + " " + hf); m_menu.setGroupVisible(R.id.menu_group_feeds, ff != null || cf != null); m_menu.setGroupVisible(R.id.menu_group_article, af != null); - HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); - m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.getSelectedArticles().size() == 0); m_menu.setGroupVisible(R.id.menu_group_headlines_selection, hf != null && hf.getSelectedArticles().size() != 0); + + MenuItem item = m_menu.findItem(R.id.show_feeds); + + if (getUnreadOnly()) { + item.setTitle(R.string.menu_all_feeds); + } else { + item.setTitle(R.string.menu_unread_feeds); + } + } } @@ -127,9 +134,37 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe onCatSelected(cat, m_prefs.getBoolean("browse_cats_like_feeds", false)); } + private void refresh() { + FeedCategoriesFragment cf = (FeedCategoriesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_CATS); + + if (cf != null) { + cf.refresh(false); + } + + FeedsFragment ff = (FeedsFragment) getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS); + + if (ff != null) { + ff.refresh(false); + } + + HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); + + if (hf != null) { + hf.refresh(false); + } + } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { + case R.id.show_feeds: + m_unreadOnly = !m_unreadOnly; + initMenu(); + refresh(); + return true; + case R.id.update_feeds: + refresh(); + return true; default: Log.d(TAG, "onOptionsItemSelected, unhandled id=" + item.getItemId()); return super.onOptionsItemSelected(item); @@ -140,7 +175,7 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe protected void loginSuccess() { setLoadingStatus(R.string.blank, false); findViewById(R.id.loading_container).setVisibility(View.GONE); - + initMenu(); } @Override @@ -154,11 +189,6 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe super.onResume(); } - @Override - public boolean getUnreadArticlesOnly() { - return m_unreadArticlesOnly; - } - @Override public void onArticleListSelectionChange(ArticleList m_selectedArticles) { initMenu(); @@ -175,10 +205,11 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); - Fragment frag = new ArticlePager(article); + HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); + + Fragment frag = new ArticlePager(article, hf.getAllArticles()); - ft.hide(getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES)); - ft.add(R.id.feeds_fragment, frag, FRAG_ARTICLE); + ft.replace(R.id.feeds_fragment, frag, FRAG_ARTICLE); ft.addToBackStack(null); ft.commit(); @@ -199,8 +230,8 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe startActivityForResult(intent, 0); } } else { - HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); - if (hf != null) hf.setActiveArticle(article); + /* HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); + if (hf != null) hf.setActiveArticle(article); */ } } diff --git a/src/org/fox/ttrss/HeadlinesActivity.java b/src/org/fox/ttrss/HeadlinesActivity.java index dd2a5a8c..1b9ead10 100644 --- a/src/org/fox/ttrss/HeadlinesActivity.java +++ b/src/org/fox/ttrss/HeadlinesActivity.java @@ -64,7 +64,7 @@ private final String TAG = this.getClass().getSimpleName(); ft.replace(R.id.headlines_fragment, hf, FRAG_HEADLINES); - ArticlePager af = new ArticlePager(article); + ArticlePager af = new ArticlePager(article, hf.getAllArticles()); ft.replace(R.id.article_fragment, af, FRAG_ARTICLE); } @@ -80,6 +80,7 @@ private final String TAG = this.getClass().getSimpleName(); setLoadingStatus(R.string.blank, false); findViewById(R.id.loading_container).setVisibility(View.GONE); + initMenu(); } @Override @@ -114,7 +115,7 @@ private final String TAG = this.getClass().getSimpleName(); protected void initMenu() { super.initMenu(); - if (m_menu != null) { + if (m_menu != null && m_sessionId != null) { m_menu.setGroupVisible(R.id.menu_group_feeds, false); HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); @@ -145,7 +146,9 @@ private final String TAG = this.getClass().getSimpleName(); FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); - Fragment frag = new ArticlePager(article); + HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); + + Fragment frag = new ArticlePager(article, hf.getAllArticles()); ft.replace(R.id.article_fragment, frag, FRAG_ARTICLE); //ft.addToBackStack(null); diff --git a/src/org/fox/ttrss/HeadlinesEventListener.java b/src/org/fox/ttrss/HeadlinesEventListener.java index 14f0ef92..024fbfad 100644 --- a/src/org/fox/ttrss/HeadlinesEventListener.java +++ b/src/org/fox/ttrss/HeadlinesEventListener.java @@ -9,6 +9,6 @@ public interface HeadlinesEventListener { void onArticleListSelectionChange(ArticleList m_selectedArticles); void onArticleSelected(Article article); void saveArticleUnread(Article article); - void onArticleSelected(Article article, boolean b); + void onArticleSelected(Article article, boolean open); } diff --git a/src/org/fox/ttrss/OnlineActivity.java b/src/org/fox/ttrss/OnlineActivity.java index 71c4a278..9b61b281 100644 --- a/src/org/fox/ttrss/OnlineActivity.java +++ b/src/org/fox/ttrss/OnlineActivity.java @@ -15,6 +15,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.FragmentTransaction; @@ -24,6 +25,7 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.Window; +import android.widget.EditText; import android.widget.SearchView; public class OnlineActivity extends CommonActivity { @@ -33,6 +35,9 @@ public class OnlineActivity extends CommonActivity { protected SharedPreferences m_prefs; protected int m_apiLevel = 0; protected Menu m_menu; + + protected boolean m_unreadOnly = true; + protected boolean m_unreadArticlesOnly = true; @Override public void onCreate(Bundle savedInstanceState) { @@ -61,6 +66,9 @@ public class OnlineActivity extends CommonActivity { if (savedInstanceState != null) { m_sessionId = savedInstanceState.getString("sessionId"); m_apiLevel = savedInstanceState.getInt("apiLevel"); + + m_unreadOnly = savedInstanceState.getBoolean("unreadOnly"); + m_unreadArticlesOnly = savedInstanceState.getBoolean("unreadArticlesOnly"); } Log.d(TAG, "m_sessionId=" + m_sessionId); @@ -130,6 +138,8 @@ public class OnlineActivity extends CommonActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { + final HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); + switch (item.getItemId()) { case R.id.logout: logout(); @@ -137,11 +147,113 @@ public class OnlineActivity extends CommonActivity { case R.id.login: login(); return true; + case R.id.go_offline: + // FIXME go offline + return true; case R.id.preferences: Intent intent = new Intent(OnlineActivity.this, PreferencesActivity.class); startActivityForResult(intent, 0); return true; + case R.id.search: + if (hf != null && isCompatMode()) { + Dialog dialog = new Dialog(this); + + final EditText edit = new EditText(this); + + AlertDialog.Builder builder = new AlertDialog.Builder(this) + .setTitle(R.string.search) + .setPositiveButton(getString(R.string.search), + new OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, + int which) { + + String query = edit.getText().toString().trim(); + + hf.setSearchQuery(query); + + } + }) + .setNegativeButton(getString(R.string.cancel), + new OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, + int which) { + + // + + } + }).setView(edit); + + dialog = builder.create(); + dialog.show(); + } + return true; + case R.id.headlines_mark_as_read: + if (hf != null) { + ArticleList articles = hf.getUnreadArticles(); + + for (Article a : articles) + a.unread = false; + + ApiRequest req = new ApiRequest(getApplicationContext()) { + protected void onPostExecute(JsonElement result) { + hf.refresh(false); + } + }; + + final String articleIds = articlesToIdString(articles); + + @SuppressWarnings("serial") + HashMap map = new HashMap() { + { + put("sid", m_sessionId); + put("op", "updateArticle"); + put("article_ids", articleIds); + put("mode", "0"); + put("field", "2"); + } + }; + req.execute(map); + } + return true; + case R.id.headlines_select: + if (hf != null) { + Dialog dialog = new Dialog(this); + AlertDialog.Builder builder = new AlertDialog.Builder(this) + .setTitle(R.string.headlines_select_dialog) + .setSingleChoiceItems( + new String[] { + getString(R.string.headlines_select_all), + getString(R.string.headlines_select_unread), + getString(R.string.headlines_select_none) }, + 0, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, + int which) { + switch (which) { + case 0: + hf.setSelection(HeadlinesFragment.ArticlesSelection.ALL); + break; + case 1: + hf.setSelection(HeadlinesFragment.ArticlesSelection.UNREAD); + break; + case 2: + hf.setSelection(HeadlinesFragment.ArticlesSelection.NONE); + break; + } + dialog.cancel(); + initMenu(); + } + }); + + dialog = builder.create(); + dialog.show(); + } + return true; default: Log.d(TAG, "onOptionsItemSelected, unhandled id=" + item.getItemId()); return super.onOptionsItemSelected(item); @@ -161,6 +273,14 @@ public class OnlineActivity extends CommonActivity { m_sessionId = null; initMenu(); } + + public boolean getUnreadArticlesOnly() { + return m_unreadArticlesOnly; + } + + public boolean getUnreadOnly() { + return m_unreadOnly; + } @Override public void onSaveInstanceState(Bundle out) { @@ -168,6 +288,8 @@ public class OnlineActivity extends CommonActivity { out.putString("sessionId", m_sessionId); out.putInt("apiLevel", m_apiLevel); + out.putBoolean("unreadOnly", m_unreadOnly); + out.putBoolean("unreadArticlesOnly", m_unreadArticlesOnly); } @Override @@ -415,9 +537,6 @@ public class OnlineActivity extends CommonActivity { m_menu.findItem(R.id.set_labels).setEnabled(m_apiLevel >= 1); m_menu.findItem(R.id.article_set_note).setEnabled(m_apiLevel >= 1); - - m_menu.findItem(R.id.close_feed).setVisible(!isSmallScreen()); - m_menu.findItem(R.id.close_article).setVisible(!isSmallScreen()); MenuItem search = m_menu.findItem(R.id.search); search.setEnabled(m_apiLevel >= 2);