various session handling fixes; try to reload headlines when

shared application data has been erased
This commit is contained in:
Andrew Dolgov 2012-09-19 12:49:10 +04:00
parent 8f88419419
commit 7abd056146
5 changed files with 74 additions and 31 deletions

View File

@ -2,6 +2,7 @@ package org.fox.ttrss;
import java.util.HashMap; import java.util.HashMap;
import org.fox.ttrss.ApiRequest.ApiError;
import org.fox.ttrss.types.Article; import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.ArticleList; import org.fox.ttrss.types.ArticleList;
import org.fox.ttrss.types.Feed; import org.fox.ttrss.types.Feed;
@ -29,6 +30,7 @@ public class ArticlePager extends Fragment {
private ArticleList m_articles = GlobalState.getInstance().m_loadedArticles; private ArticleList m_articles = GlobalState.getInstance().m_loadedArticles;
private OnlineActivity m_activity; private OnlineActivity m_activity;
private String m_searchQuery = ""; private String m_searchQuery = "";
private Feed m_feed;
private class PagerAdapter extends FragmentStatePagerAdapter { private class PagerAdapter extends FragmentStatePagerAdapter {
@ -58,10 +60,11 @@ public class ArticlePager extends Fragment {
super(); super();
} }
public ArticlePager(Article article) { public ArticlePager(Article article, Feed feed) {
super(); super();
m_article = article; m_article = article;
m_feed = feed;
} }
public void setSearchQuery(String searchQuery) { public void setSearchQuery(String searchQuery) {
@ -74,6 +77,7 @@ public class ArticlePager extends Fragment {
if (savedInstanceState != null) { if (savedInstanceState != null) {
m_article = savedInstanceState.getParcelable("article"); m_article = savedInstanceState.getParcelable("article");
m_feed = savedInstanceState.getParcelable("feed");
} }
m_adapter = new PagerAdapter(getActivity().getSupportFragmentManager()); m_adapter = new PagerAdapter(getActivity().getSupportFragmentManager());
@ -111,7 +115,7 @@ public class ArticlePager extends Fragment {
if (m_activity.isSmallScreen() && position == m_adapter.getCount() - 5) { if (m_activity.isSmallScreen() && position == m_adapter.getCount() - 5) {
Log.d(TAG, "loading more articles..."); Log.d(TAG, "loading more articles...");
loadMoreArticles(); refresh(true);
} }
} }
} }
@ -121,27 +125,43 @@ public class ArticlePager extends Fragment {
} }
@SuppressWarnings({ "unchecked", "serial" }) @SuppressWarnings({ "unchecked", "serial" })
private void loadMoreArticles() { private void refresh(boolean append) {
m_activity.setLoadingStatus(R.string.blank, true); m_activity.setLoadingStatus(R.string.blank, true);
if (!m_feed.equals(GlobalState.getInstance().m_activeFeed)) {
append = false;
}
HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext(), m_activity) { HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext(), m_activity) {
protected void onPostExecute(JsonElement result) { protected void onPostExecute(JsonElement result) {
super.onPostExecute(result); super.onPostExecute(result);
if (result != null) {
m_adapter.notifyDataSetChanged(); m_adapter.notifyDataSetChanged();
} else {
if (m_lastError == ApiError.LOGIN_FAILED) {
m_activity.login();
} else {
m_activity.toast(getErrorMessage());
//setLoadingStatus(getErrorMessage(), false);
}
}
} }
}; };
final Feed feed = GlobalState.getInstance().m_activeFeed; final Feed feed = m_feed;
final String sessionId = m_activity.getSessionId(); final String sessionId = m_activity.getSessionId();
final boolean showUnread = m_activity.getUnreadArticlesOnly(); final boolean showUnread = m_activity.getUnreadArticlesOnly();
int skip = 0; int skip = 0;
if (append) {
for (Article a : m_articles) { for (Article a : m_articles) {
if (a.unread) ++skip; if (a.unread) ++skip;
} }
if (skip == 0) skip = m_articles.size(); if (skip == 0) skip = m_articles.size();
}
final int fskip = skip; final int fskip = skip;
@ -178,6 +198,7 @@ public class ArticlePager extends Fragment {
super.onSaveInstanceState(out); super.onSaveInstanceState(out);
out.putParcelable("article", m_article); out.putParcelable("article", m_article);
out.putParcelable("feed", m_feed);
} }
@Override @Override
@ -192,6 +213,13 @@ public class ArticlePager extends Fragment {
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
if (m_articles.size() == 0 || !m_feed.equals(GlobalState.getInstance().m_activeFeed)) {
refresh(false);
GlobalState.getInstance().m_activeFeed = m_feed;
} else {
m_adapter.notifyDataSetChanged();
}
m_activity.initMenu(); m_activity.initMenu();
} }

View File

@ -61,6 +61,16 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (article != null) {
Article original = GlobalState.getInstance().m_loadedArticles.findById(article.id);
ArticlePager ap = new ArticlePager(original != null ? original : article, feed);
ft.replace(R.id.feeds_fragment, ap, FRAG_ARTICLE);
ap.setSearchQuery(intent.getStringExtra("searchQuery"));
setTitle(intent.getStringExtra("feedTitle"));
} else {
if (feed != null) { if (feed != null) {
HeadlinesFragment hf = new HeadlinesFragment(feed); HeadlinesFragment hf = new HeadlinesFragment(feed);
ft.replace(R.id.feeds_fragment, hf, FRAG_HEADLINES); ft.replace(R.id.feeds_fragment, hf, FRAG_HEADLINES);
@ -74,16 +84,6 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe
setTitle(cat.title); setTitle(cat.title);
} }
if (article != null) {
Article original = GlobalState.getInstance().m_loadedArticles.findById(article.id);
ArticlePager ap = new ArticlePager(original != null ? original : article);
ft.replace(R.id.feeds_fragment, ap, FRAG_ARTICLE);
ap.setSearchQuery(intent.getStringExtra("searchQuery"));
setTitle(intent.getStringExtra("feedTitle"));
} }
ft.commit(); ft.commit();
@ -254,6 +254,7 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe
intent.putExtra("apiLevel", m_apiLevel); intent.putExtra("apiLevel", m_apiLevel);
intent.putExtra("feedTitle", hf.getFeed().title); intent.putExtra("feedTitle", hf.getFeed().title);
intent.putExtra("feed", hf.getFeed());
intent.putExtra("article", article); intent.putExtra("article", article);
intent.putExtra("searchQuery", hf.getSearchQuery()); intent.putExtra("searchQuery", hf.getSearchQuery());

View File

@ -49,7 +49,7 @@ public class HeadlinesActivity extends OnlineActivity implements HeadlinesEventL
String searchQuery = i.getStringExtra("searchQuery"); String searchQuery = i.getStringExtra("searchQuery");
HeadlinesFragment hf = new HeadlinesFragment(feed, article); HeadlinesFragment hf = new HeadlinesFragment(feed, article);
ArticlePager af = new ArticlePager(hf.getArticleById(article.id)); ArticlePager af = new ArticlePager(hf.getArticleById(article.id), feed);
hf.setSearchQuery(searchQuery); hf.setSearchQuery(searchQuery);
af.setSearchQuery(searchQuery); af.setSearchQuery(searchQuery);
@ -171,7 +171,7 @@ public class HeadlinesActivity extends OnlineActivity implements HeadlinesEventL
hf.setActiveArticle(article); hf.setActiveArticle(article);
ArticlePager af = new ArticlePager(article); ArticlePager af = new ArticlePager(article, hf.getFeed());
FragmentTransaction ft = getSupportFragmentManager() FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction(); .beginTransaction();

View File

@ -11,6 +11,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
import org.fox.ttrss.ApiRequest.ApiError;
import org.fox.ttrss.types.Article; import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.ArticleList; import org.fox.ttrss.types.ArticleList;
import org.fox.ttrss.types.Attachment; import org.fox.ttrss.types.Attachment;
@ -365,9 +366,18 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext(), m_activity) { HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext(), m_activity) {
protected void onPostExecute(JsonElement result) { protected void onPostExecute(JsonElement result) {
super.onPostExecute(result); super.onPostExecute(result);
if (result != null) {
m_refreshInProgress = false; m_refreshInProgress = false;
m_adapter.notifyDataSetChanged(); m_adapter.notifyDataSetChanged();
m_listener.onHeadlinesLoaded(fappend); m_listener.onHeadlinesLoaded(fappend);
} else {
if (m_lastError == ApiError.LOGIN_FAILED) {
m_activity.login();
} else {
setLoadingStatus(getErrorMessage(), false);
}
}
} }
}; };

View File

@ -1203,6 +1203,10 @@ public class OnlineActivity extends CommonActivity {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} else {
setLoadingStatus(getErrorMessage(), false);
loginFailure();
return;
} }
Log.d(TAG, "Received API level: " + m_apiLevel); Log.d(TAG, "Received API level: " + m_apiLevel);