From 2d9ee319abba7ae9d60848ede2e00a9c58849c99 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 25 Nov 2011 13:32:32 +0300 Subject: [PATCH] properly restore state on orientation change --- src/org/fox/ttrss/Article.java | 40 ++++++++++++++++- src/org/fox/ttrss/ArticleFragment.java | 11 +---- src/org/fox/ttrss/HeadlinesFragment.java | 57 ++++++++++++++---------- src/org/fox/ttrss/MainActivity.java | 30 ++++++++----- 4 files changed, 93 insertions(+), 45 deletions(-) diff --git a/src/org/fox/ttrss/Article.java b/src/org/fox/ttrss/Article.java index c4989d7a..e9766556 100644 --- a/src/org/fox/ttrss/Article.java +++ b/src/org/fox/ttrss/Article.java @@ -1,8 +1,11 @@ package org.fox.ttrss; import java.util.List; +import android.os.Parcel; +import android.os.Parcelable; -public class Article { + +public class Article implements Parcelable { int id; boolean unread; boolean marked; @@ -14,5 +17,38 @@ public class Article { int feed_id; List tags; String content; - boolean _selected; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel out, int flags) { + out.writeInt(id); + out.writeInt(unread ? 1 : 0); + out.writeInt(marked ? 1 : 0); + out.writeInt(published ? 1 : 0); + out.writeInt(updated); + out.writeInt(is_updated ? 1 : 0); + out.writeString(title); + out.writeString(link); + out.writeInt(feed_id); + out.writeStringList(tags); + out.writeString(content); + } + + public void readFromParcel(Parcel in) { + id = in.readInt(); + unread = in.readInt() == 1; + marked = in.readInt() == 1; + published = in.readInt() == 1; + updated = in.readInt(); + is_updated = in.readInt() == 1; + title = in.readString(); + link = in.readString(); + feed_id = in.readInt(); + in.readStringList(tags); + content = in.readString(); + } } diff --git a/src/org/fox/ttrss/ArticleFragment.java b/src/org/fox/ttrss/ArticleFragment.java index 511932bc..86bb4e8c 100644 --- a/src/org/fox/ttrss/ArticleFragment.java +++ b/src/org/fox/ttrss/ArticleFragment.java @@ -22,15 +22,13 @@ public class ArticleFragment extends Fragment { protected SharedPreferences m_prefs; - private String m_sessionId; private Article m_article; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (savedInstanceState != null) { - m_sessionId = savedInstanceState.getString("sessionId"); - //m_articleId = savedInstanceState.getInt("articleId"); + m_article = savedInstanceState.getParcelable("article"); } View view = inflater.inflate(R.layout.article_fragment, container, false); @@ -94,18 +92,13 @@ public class ArticleFragment extends Fragment { public void onSaveInstanceState (Bundle out) { super.onSaveInstanceState(out); - out.putString("sessionId", m_sessionId); - //out.putInt("articleId", m_articleId); + out.putParcelable("article", m_article); } @Override public void onAttach(Activity activity) { super.onAttach(activity); - m_sessionId = ((MainActivity)activity).getSessionId(); m_article = ((MainActivity)activity).getSelectedArticle(); - - //m_prefs = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext()); - } } diff --git a/src/org/fox/ttrss/HeadlinesFragment.java b/src/org/fox/ttrss/HeadlinesFragment.java index fa5db0e8..611941cb 100644 --- a/src/org/fox/ttrss/HeadlinesFragment.java +++ b/src/org/fox/ttrss/HeadlinesFragment.java @@ -16,6 +16,8 @@ import android.app.Fragment; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; +import android.os.Parcel; +import android.os.Parcelable; import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; @@ -42,26 +44,49 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { private final String TAG = this.getClass().getSimpleName(); protected SharedPreferences m_prefs; - //private String m_sessionId; private Feed m_feed; - //private int m_activeArticleId; private int m_selectedArticleId; private ArticleListAdapter m_adapter; - private List
m_articles = new ArrayList
(); + private ArticleList m_articles = new ArticleList(); private OnArticleSelectedListener m_articleSelectedListener; public interface OnArticleSelectedListener { public void onArticleSelected(Article article); } + + private class ArticleList extends ArrayList
implements Parcelable { + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel out, int flags) { + out.writeInt(this.size()); + for (Article article : this) { + out.writeParcelable(article, flags); + } + } + + public void readFromParcel(Parcel in) { + int length = in.readInt(); + + for (int i = 0; i < length; i++) { + Article article = in.readParcelable(Article.class.getClassLoader()); + this.add(article); + } + + } + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (savedInstanceState != null) { - //m_sessionId = savedInstanceState.getString("sessionId"); - m_feed = savedInstanceState.getParcelable("feed"); - //m_activeArticleId = savedInstanceState.getInt("activeArticleId"); + m_articles = savedInstanceState.getParcelable("articles"); + m_selectedArticleId = savedInstanceState.getInt("selectedArticleId"); } View view = inflater.inflate(R.layout.headlines_fragment, container, false); @@ -73,7 +98,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { Log.d(TAG, "onCreateView, feed=" + m_feed); - if (m_feed != null) + if (m_feed != null && (m_articles == null || m_articles.size() == 0)) refresh(); else view.findViewById(R.id.loading_container).setVisibility(View.GONE); @@ -132,7 +157,6 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { put("limit", String.valueOf(30)); put("offset", String.valueOf(0)); put("view_mode", "adaptive"); - //put("view_mode", "all_articles"); } }; @@ -144,10 +168,8 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { super.onSaveInstanceState(out); out.putParcelable("feed", m_feed); - - //out.putString("sessionId", m_sessionId); - //out.putInt("feedId", m_feedId); - //out.putInt("activeArticleId", m_activeArticleId); + out.putParcelable("articles", m_articles); + out.putInt("selectedArticleId", m_selectedArticleId); } private class HeadlinesRequest extends ApiRequest { @@ -246,17 +268,6 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { private ArrayList
m_selectedArticles = new ArrayList
(); - /* private class ArticleCheckListener implements OnCheckedChangeListener { - - @Override - public void onCheckedChanged(CompoundButton buttonView, - boolean isChecked) { - - Log.d(TAG, "onCheckedChanged: " + buttonView + "/" + getContext()); - } - - } */ - public ArticleListAdapter(Context context, int textViewResourceId, ArrayList
items) { super(context, textViewResourceId, items); this.items = items; diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java index 0b844317..6c583974 100644 --- a/src/org/fox/ttrss/MainActivity.java +++ b/src/org/fox/ttrss/MainActivity.java @@ -91,6 +91,7 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect m_sessionId = savedInstanceState.getString("sessionId"); m_unreadOnly = savedInstanceState.getBoolean("unreadOnly"); m_activeFeed = savedInstanceState.getParcelable("activeFeed"); + m_selectedArticle = savedInstanceState.getParcelable("selectedArticle"); } setContentView(R.layout.main); @@ -99,10 +100,12 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect LinearLayout layout = (LinearLayout)findViewById(R.id.main); layout.setLayoutTransition(transitioner); - findViewById(R.id.article_fragment).setVisibility(View.GONE); + if (m_selectedArticle == null) + findViewById(R.id.article_fragment).setVisibility(View.GONE); + else + findViewById(R.id.feeds_fragment).setVisibility(View.GONE); if (m_sessionId != null) { - // restarting, TODO set update timers here? loginSuccess(); } else { login(); @@ -131,7 +134,7 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect out.putString("sessionId", m_sessionId); out.putBoolean("unreadOnly", m_unreadOnly); out.putParcelable("activeFeed", m_activeFeed); - + out.putParcelable("selectedArticle", m_selectedArticle); } @Override @@ -232,9 +235,8 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect public void closeArticle() { findViewById(R.id.article_fragment).setVisibility(View.GONE); findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE); - - if (m_menu != null) - m_menu.findItem(R.id.close_article).setVisible(false); + + initMainMenu(); m_selectedArticle = null; } @@ -249,6 +251,14 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect m_menu.findItem(R.id.update_feeds).setEnabled(true); m_menu.findItem(R.id.show_feeds).setEnabled(true); + + if (m_selectedArticle != null) { + m_menu.findItem(R.id.close_article).setVisible(true); + m_menu.findItem(R.id.share_article).setVisible(true); + } else { + m_menu.findItem(R.id.close_article).setVisible(false); + m_menu.findItem(R.id.share_article).setVisible(false); + } } else { m_menu.findItem(R.id.login).setVisible(true); @@ -361,6 +371,8 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect public void openArticle(Article article) { m_selectedArticle = article; + initMainMenu(); + ArticleFragment frag = new ArticleFragment(); FragmentTransaction ft = getFragmentManager().beginTransaction(); @@ -369,11 +381,7 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect findViewById(R.id.feeds_fragment).setVisibility(View.GONE); findViewById(R.id.article_fragment).setVisibility(View.VISIBLE); - - if (m_menu != null) { - m_menu.findItem(R.id.close_article).setVisible(true); - m_menu.findItem(R.id.share_article).setVisible(true); - } + } @Override