properly restore state on orientation change

This commit is contained in:
Andrew Dolgov 2011-11-25 13:32:32 +03:00
parent ccb6b1cb5f
commit 2d9ee319ab
4 changed files with 93 additions and 45 deletions

View File

@ -1,8 +1,11 @@
package org.fox.ttrss; package org.fox.ttrss;
import java.util.List; import java.util.List;
import android.os.Parcel;
import android.os.Parcelable;
public class Article {
public class Article implements Parcelable {
int id; int id;
boolean unread; boolean unread;
boolean marked; boolean marked;
@ -14,5 +17,38 @@ public class Article {
int feed_id; int feed_id;
List<String> tags; List<String> tags;
String content; 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();
}
} }

View File

@ -22,15 +22,13 @@ public class ArticleFragment extends Fragment {
protected SharedPreferences m_prefs; protected SharedPreferences m_prefs;
private String m_sessionId;
private Article m_article; private Article m_article;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) { if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId"); m_article = savedInstanceState.getParcelable("article");
//m_articleId = savedInstanceState.getInt("articleId");
} }
View view = inflater.inflate(R.layout.article_fragment, container, false); View view = inflater.inflate(R.layout.article_fragment, container, false);
@ -94,18 +92,13 @@ public class ArticleFragment extends Fragment {
public void onSaveInstanceState (Bundle out) { public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out); super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId); out.putParcelable("article", m_article);
//out.putInt("articleId", m_articleId);
} }
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
m_sessionId = ((MainActivity)activity).getSessionId();
m_article = ((MainActivity)activity).getSelectedArticle(); m_article = ((MainActivity)activity).getSelectedArticle();
//m_prefs = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
} }
} }

View File

@ -16,6 +16,8 @@ import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -42,26 +44,49 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
private final String TAG = this.getClass().getSimpleName(); private final String TAG = this.getClass().getSimpleName();
protected SharedPreferences m_prefs; protected SharedPreferences m_prefs;
//private String m_sessionId;
private Feed m_feed; private Feed m_feed;
//private int m_activeArticleId;
private int m_selectedArticleId; private int m_selectedArticleId;
private ArticleListAdapter m_adapter; private ArticleListAdapter m_adapter;
private List<Article> m_articles = new ArrayList<Article>(); private ArticleList m_articles = new ArticleList();
private OnArticleSelectedListener m_articleSelectedListener; private OnArticleSelectedListener m_articleSelectedListener;
public interface OnArticleSelectedListener { public interface OnArticleSelectedListener {
public void onArticleSelected(Article article); public void onArticleSelected(Article article);
} }
private class ArticleList extends ArrayList<Article> 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 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) { if (savedInstanceState != null) {
//m_sessionId = savedInstanceState.getString("sessionId");
m_feed = savedInstanceState.getParcelable("feed"); 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); 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); Log.d(TAG, "onCreateView, feed=" + m_feed);
if (m_feed != null) if (m_feed != null && (m_articles == null || m_articles.size() == 0))
refresh(); refresh();
else else
view.findViewById(R.id.loading_container).setVisibility(View.GONE); 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("limit", String.valueOf(30));
put("offset", String.valueOf(0)); put("offset", String.valueOf(0));
put("view_mode", "adaptive"); put("view_mode", "adaptive");
//put("view_mode", "all_articles");
} }
}; };
@ -144,10 +168,8 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
super.onSaveInstanceState(out); super.onSaveInstanceState(out);
out.putParcelable("feed", m_feed); out.putParcelable("feed", m_feed);
out.putParcelable("articles", m_articles);
//out.putString("sessionId", m_sessionId); out.putInt("selectedArticleId", m_selectedArticleId);
//out.putInt("feedId", m_feedId);
//out.putInt("activeArticleId", m_activeArticleId);
} }
private class HeadlinesRequest extends ApiRequest { private class HeadlinesRequest extends ApiRequest {
@ -246,17 +268,6 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
private ArrayList<Article> m_selectedArticles = new ArrayList<Article>(); private ArrayList<Article> m_selectedArticles = new ArrayList<Article>();
/* 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<Article> items) { public ArticleListAdapter(Context context, int textViewResourceId, ArrayList<Article> items) {
super(context, textViewResourceId, items); super(context, textViewResourceId, items);
this.items = items; this.items = items;

View File

@ -91,6 +91,7 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
m_sessionId = savedInstanceState.getString("sessionId"); m_sessionId = savedInstanceState.getString("sessionId");
m_unreadOnly = savedInstanceState.getBoolean("unreadOnly"); m_unreadOnly = savedInstanceState.getBoolean("unreadOnly");
m_activeFeed = savedInstanceState.getParcelable("activeFeed"); m_activeFeed = savedInstanceState.getParcelable("activeFeed");
m_selectedArticle = savedInstanceState.getParcelable("selectedArticle");
} }
setContentView(R.layout.main); setContentView(R.layout.main);
@ -99,10 +100,12 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
LinearLayout layout = (LinearLayout)findViewById(R.id.main); LinearLayout layout = (LinearLayout)findViewById(R.id.main);
layout.setLayoutTransition(transitioner); layout.setLayoutTransition(transitioner);
if (m_selectedArticle == null)
findViewById(R.id.article_fragment).setVisibility(View.GONE); findViewById(R.id.article_fragment).setVisibility(View.GONE);
else
findViewById(R.id.feeds_fragment).setVisibility(View.GONE);
if (m_sessionId != null) { if (m_sessionId != null) {
// restarting, TODO set update timers here?
loginSuccess(); loginSuccess();
} else { } else {
login(); login();
@ -131,7 +134,7 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
out.putString("sessionId", m_sessionId); out.putString("sessionId", m_sessionId);
out.putBoolean("unreadOnly", m_unreadOnly); out.putBoolean("unreadOnly", m_unreadOnly);
out.putParcelable("activeFeed", m_activeFeed); out.putParcelable("activeFeed", m_activeFeed);
out.putParcelable("selectedArticle", m_selectedArticle);
} }
@Override @Override
@ -233,8 +236,7 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
findViewById(R.id.article_fragment).setVisibility(View.GONE); findViewById(R.id.article_fragment).setVisibility(View.GONE);
findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE); findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE);
if (m_menu != null) initMainMenu();
m_menu.findItem(R.id.close_article).setVisible(false);
m_selectedArticle = null; m_selectedArticle = null;
} }
@ -250,6 +252,14 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
m_menu.findItem(R.id.update_feeds).setEnabled(true); m_menu.findItem(R.id.update_feeds).setEnabled(true);
m_menu.findItem(R.id.show_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 { } else {
m_menu.findItem(R.id.login).setVisible(true); 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) { public void openArticle(Article article) {
m_selectedArticle = article; m_selectedArticle = article;
initMainMenu();
ArticleFragment frag = new ArticleFragment(); ArticleFragment frag = new ArticleFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction(); FragmentTransaction ft = getFragmentManager().beginTransaction();
@ -370,10 +382,6 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
findViewById(R.id.feeds_fragment).setVisibility(View.GONE); findViewById(R.id.feeds_fragment).setVisibility(View.GONE);
findViewById(R.id.article_fragment).setVisibility(View.VISIBLE); 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 @Override