implement application singleton to store retrieved headlines

activities
This commit is contained in:
Andrew Dolgov 2012-09-16 21:59:03 +04:00
parent a5ee43a4e6
commit 306a0bf4c9
7 changed files with 54 additions and 22 deletions

View File

@ -13,6 +13,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name="TinyApplication"
android:allowBackup="true"
android:backupAgent=".util.PrefsBackupAgent"
android:hardwareAccelerated="true"

View File

@ -49,11 +49,10 @@ public class ArticlePager extends Fragment {
super();
}
public ArticlePager(Article article, ArticleList articles) {
public ArticlePager(Article article) {
super();
m_article = article;
m_articles = articles;
}
@Override
@ -88,6 +87,8 @@ public class ArticlePager extends Fragment {
Article article = m_articles.get(position);
if (article != null) {
m_article = article;
if (article.unread) {
article.unread = false;
m_onlineServices.saveArticleUnread(article);
@ -121,6 +122,8 @@ public class ArticlePager extends Fragment {
super.onAttach(activity);
m_onlineServices = (HeadlinesEventListener)activity;
m_articles = TinyApplication.getInstance().m_articles;
}
@Override

View File

@ -208,7 +208,7 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
Fragment frag = new ArticlePager(article, hf.getAllArticles());
Fragment frag = new ArticlePager(article);
ft.replace(R.id.feeds_fragment, frag, FRAG_ARTICLE);
ft.addToBackStack(null);

View File

@ -52,14 +52,8 @@ private final String TAG = this.getClass().getSimpleName();
Article activeArticle = i.getParcelableExtra("activeArticle");
Article article = i.getParcelableExtra("article");
ArrayList<Article> 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();
}
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -28,6 +28,13 @@ public class Feed implements Comparable<Feed>, 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)