From 306a0bf4c9c0c3a8db719d6bd6211aa5f6355c0c Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 16 Sep 2012 21:59:03 +0400 Subject: [PATCH] implement application singleton to store retrieved headlines activities --- AndroidManifest.xml | 1 + src/org/fox/ttrss/ArticlePager.java | 7 +++++-- src/org/fox/ttrss/FeedsActivity.java | 2 +- src/org/fox/ttrss/HeadlinesActivity.java | 13 ++++-------- src/org/fox/ttrss/HeadlinesFragment.java | 21 ++++++++++---------- src/org/fox/ttrss/TinyApplication.java | 25 ++++++++++++++++++++++++ src/org/fox/ttrss/types/Feed.java | 7 +++++++ 7 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 src/org/fox/ttrss/TinyApplication.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index e4e00753..1d48a3be 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -13,6 +13,7 @@ alist = i.getParcelableArrayListExtra("articles"); - ArticleList articles = new ArticleList(); - - for (Article a : alist) - articles.add(a); - - HeadlinesFragment hf = new HeadlinesFragment(feed, activeArticle, articles); - ArticlePager af = new ArticlePager(hf.getArticleById(article.id), hf.getAllArticles()); + HeadlinesFragment hf = new HeadlinesFragment(feed, activeArticle); + ArticlePager af = new ArticlePager(hf.getArticleById(article.id)); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); @@ -147,7 +141,7 @@ private final String TAG = this.getClass().getSimpleName(); HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); - Fragment frag = new ArticlePager(article, hf.getAllArticles()); + Fragment frag = new ArticlePager(article); ft.replace(R.id.article_fragment, frag, FRAG_ARTICLE); //ft.addToBackStack(null); @@ -156,6 +150,7 @@ private final String TAG = this.getClass().getSimpleName(); } else { HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); if (hf != null) hf.setActiveArticle(article); + initMenu(); } } diff --git a/src/org/fox/ttrss/HeadlinesFragment.java b/src/org/fox/ttrss/HeadlinesFragment.java index 32b27331..8b087fa8 100644 --- a/src/org/fox/ttrss/HeadlinesFragment.java +++ b/src/org/fox/ttrss/HeadlinesFragment.java @@ -71,12 +71,11 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, private boolean m_canLoadMore = false; private boolean m_combinedMode = true; private String m_searchQuery = ""; - private boolean m_noRefresh = false; private SharedPreferences m_prefs; private ArticleListAdapter m_adapter; - private ArticleList m_articles = new ArticleList(); + private ArticleList m_articles = TinyApplication.getInstance().m_articles; private ArticleList m_selectedArticles = new ArticleList(); private HeadlinesEventListener m_listener; @@ -98,11 +97,9 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, m_feed = feed; } - public HeadlinesFragment(Feed feed, Article activeArticle, ArticleList articles) { + public HeadlinesFragment(Feed feed, Article activeArticle) { m_feed = feed; m_activeArticle = activeArticle; - m_articles = articles; - m_noRefresh = true; } public HeadlinesFragment() { @@ -268,7 +265,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, if (savedInstanceState != null) { m_feed = savedInstanceState.getParcelable("feed"); - m_articles = savedInstanceState.getParcelable("articles"); + //m_articles = savedInstanceState.getParcelable("articles"); m_activeArticle = savedInstanceState.getParcelable("activeArticle"); m_selectedArticles = savedInstanceState.getParcelable("selectedArticles"); m_canLoadMore = savedInstanceState.getBoolean("canLoadMore"); @@ -297,10 +294,10 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, @Override public void onResume() { super.onResume(); - - if (!m_noRefresh) { + + if (m_articles.size() == 0 || !m_feed.equals(TinyApplication.getInstance().m_feed)) { refresh(false); - m_noRefresh = false; + TinyApplication.getInstance().m_feed = m_feed; } m_activity.initMenu(); @@ -349,6 +346,10 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, final boolean isCat = m_feed.is_cat; int skip = 0; + if (!m_feed.equals(TinyApplication.getInstance())) { + append = false; + } + if (append) { for (Article a : m_articles) { if (a.unread) ++skip; @@ -393,7 +394,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, super.onSaveInstanceState(out); out.putParcelable("feed", m_feed); - out.putParcelable("articles", m_articles); + //out.putParcelable("articles", m_articles); out.putParcelable("activeArticle", m_activeArticle); out.putParcelable("selectedArticles", m_selectedArticles); out.putBoolean("canLoadMore", m_canLoadMore); diff --git a/src/org/fox/ttrss/TinyApplication.java b/src/org/fox/ttrss/TinyApplication.java new file mode 100644 index 00000000..135de75f --- /dev/null +++ b/src/org/fox/ttrss/TinyApplication.java @@ -0,0 +1,25 @@ +package org.fox.ttrss; + +import org.fox.ttrss.types.Article; +import org.fox.ttrss.types.ArticleList; +import org.fox.ttrss.types.Feed; + +import android.app.Application; + +public class TinyApplication extends Application { + private static TinyApplication m_singleton; + + public ArticleList m_articles = new ArticleList(); + public Feed m_feed; + public Article m_selectedArticle; + + public static TinyApplication getInstance(){ + return m_singleton; + } + + @Override + public final void onCreate() { + super.onCreate(); + m_singleton = this; + } +} diff --git a/src/org/fox/ttrss/types/Feed.java b/src/org/fox/ttrss/types/Feed.java index 91d12308..5227468f 100644 --- a/src/org/fox/ttrss/types/Feed.java +++ b/src/org/fox/ttrss/types/Feed.java @@ -28,6 +28,13 @@ public class Feed implements Comparable, Parcelable { } + public boolean equals(Feed feed) { + if (feed == this) + return true; + + return feed.id == this.id && (this.title == null || this.title.equals(feed.title)) && this.is_cat == feed.is_cat; + } + @Override public int compareTo(Feed feed) { if (feed.unread != this.unread)