From 84698a5133ae9244e5bd03dde88b9cacb7adc860 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 13 Feb 2015 14:31:15 +0300 Subject: [PATCH] feedsactivity: restore headline selection state when going back from headlinesactivity --- .../java/org/fox/ttrss/FeedsActivity.java | 9 ++++-- .../java/org/fox/ttrss/HeadlinesActivity.java | 23 +++++++++++++- .../java/org/fox/ttrss/HeadlinesFragment.java | 31 ++++++++++++++----- .../java/org/fox/ttrss/types/ArticleList.java | 26 +++++++--------- 4 files changed, 63 insertions(+), 26 deletions(-) mode change 100644 => 100755 org.fox.ttrss/src/main/java/org/fox/ttrss/types/ArticleList.java diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/FeedsActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/FeedsActivity.java index 3dbc47f3..50678d81 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/FeedsActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/FeedsActivity.java @@ -447,6 +447,7 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe intent.putExtra("searchQuery", hf.getSearchQuery()); //intent.putParcelableArrayListExtra("articles", hf.getArticles()); intent.putExtra("articles", (Parcelable)hf.getAllArticles()); + intent.putExtra("selectedArticles", (Parcelable)hf.getSelectedArticles()); /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { startActivityForResult(intent, HEADLINES_REQUEST, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()); @@ -498,13 +499,15 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe //ArrayList
tmp = data.getParcelableArrayListExtra("articles"); Article article = data.getParcelableExtra("activeArticle"); - ArticleList tmp = data.getParcelableExtra("articles"); + ArticleList articles = data.getParcelableExtra("articles"); + ArticleList selectedArticles = data.getParcelableExtra("selectedArticles"); - if (tmp != null) { + if (articles != null) { HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); if (hf != null) { - hf.setArticles(tmp); + hf.setArticles(articles); + hf.setSelectedArticles(selectedArticles); hf.setActiveArticle(article); } } diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/HeadlinesActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/HeadlinesActivity.java index 3296964e..68826434 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/HeadlinesActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/HeadlinesActivity.java @@ -21,6 +21,7 @@ import org.fox.ttrss.types.Feed; public class HeadlinesActivity extends OnlineActivity implements HeadlinesEventListener { private final String TAG = this.getClass().getSimpleName(); protected ArticleList m_articles = new ArticleList(); + protected ArticleList m_selectedArticles = new ArticleList(); protected SharedPreferences m_prefs; private Article m_activeArticle; @@ -50,6 +51,7 @@ public class HeadlinesActivity extends OnlineActivity implements HeadlinesEventL if (savedInstanceState != null) { m_articles = savedInstanceState.getParcelable("articles"); + m_selectedArticles = savedInstanceState.getParcelable("selectedArticles"); } else { Intent i = getIntent(); @@ -83,12 +85,22 @@ public class HeadlinesActivity extends OnlineActivity implements HeadlinesEventL m_articles.addAll(tmp); } - FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + tmp = i.getParcelableExtra("selectedArticles"); + + if (tmp != null) { + m_selectedArticles.addAll(tmp); + } + + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); final HeadlinesFragment hf = new HeadlinesFragment(); hf.initialize(feed, article, true, m_articles); hf.setSearchQuery(searchQuery); + if (!isPortrait() && !isSmallScreen()) { + hf.setSelectedArticles(m_selectedArticles); + } + ft.replace(R.id.headlines_fragment, hf, FRAG_HEADLINES); ft.replace(R.id.article_fragment, new LoadingFragment(), null); @@ -144,6 +156,7 @@ public class HeadlinesActivity extends OnlineActivity implements HeadlinesEventL super.onSaveInstanceState(out); out.putParcelable("articles", m_articles); + out.putParcelable("selectedArticles", m_selectedArticles); GlobalState.getInstance().save(out); } @@ -304,9 +317,17 @@ public class HeadlinesActivity extends OnlineActivity implements HeadlinesEventL public void onBackPressed() { Intent resultIntent = new Intent(); + HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES); + resultIntent.putExtra("articles", (Parcelable) m_articles); resultIntent.putExtra("activeArticle", m_activeArticle); + if (hf != null && !isPortrait() && !isSmallScreen()) { + resultIntent.putExtra("selectedArticles", (Parcelable) hf.getSelectedArticles()); + } else { + resultIntent.putExtra("selectedArticles", (Parcelable) m_selectedArticles); + } + setResult(Activity.RESULT_OK, resultIntent); super.onBackPressed(); diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/HeadlinesFragment.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/HeadlinesFragment.java index 5dca5828..1916106b 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/HeadlinesFragment.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/HeadlinesFragment.java @@ -73,7 +73,7 @@ import java.util.HashMap; import java.util.TimeZone; public class HeadlinesFragment extends Fragment implements OnItemClickListener, OnScrollListener { - public static enum ArticlesSelection { ALL, NONE, UNREAD } + public static enum ArticlesSelection { ALL, NONE, UNREAD } public static final int FLAVOR_IMG_MIN_WIDTH = 128; public static final int FLAVOR_IMG_MIN_HEIGHT = 128; @@ -685,7 +685,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, private void updateTextCheckedState(HeadlineViewHolder holder, Article item) { String tmp = item.title.length() > 0 ? item.title.substring(0, 1) : "?"; - if (m_selectedArticles.contains(item)) { + if (m_selectedArticles.containsId(item.id)) { holder.textImage.setImageDrawable(m_drawableBuilder.build(" ", 0xff616161)); holder.textChecked.setVisibility(View.VISIBLE); @@ -773,7 +773,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, public void onClick(View view) { Log.d(TAG, "textImage : onclicked"); - if (!m_selectedArticles.contains(article)) { + if (!m_selectedArticles.containsId(article.id)) { m_selectedArticles.add(article); } else { m_selectedArticles.remove(article); @@ -1053,7 +1053,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, if (holder.selectionBoxView != null) { - holder.selectionBoxView.setChecked(m_selectedArticles.contains(article)); + holder.selectionBoxView.setChecked(m_selectedArticles.containsId(article.id)); holder.selectionBoxView.setOnClickListener(new OnClickListener() { @Override @@ -1061,7 +1061,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, CheckBox cb = (CheckBox)view; if (cb.isChecked()) { - if (!m_selectedArticles.contains(article)) + if (!m_selectedArticles.containsId(article.id)) m_selectedArticles.add(article); } else { m_selectedArticles.remove(article); @@ -1149,11 +1149,26 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener, } } } - - m_adapter.notifyDataSetChanged(); + + if (m_adapter != null) { + m_adapter.notifyDataSetChanged(); + } } - public Article getArticleAtPosition(int position) { + public void setSelectedArticles(ArticleList selectedArticles) { + if (selectedArticles != null) { + m_selectedArticles.clear(); + m_selectedArticles.addAll(selectedArticles); + + if (m_adapter != null) { + m_adapter.notifyDataSetChanged(); + } + } + } + + + + public Article getArticleAtPosition(int position) { try { return m_adapter.getItem(position); } catch (IndexOutOfBoundsException e) { diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/types/ArticleList.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/types/ArticleList.java old mode 100644 new mode 100755 index c9b491ee..a6f59643 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/types/ArticleList.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/types/ArticleList.java @@ -1,11 +1,9 @@ package org.fox.ttrss.types; -import java.util.ArrayList; - - import android.os.Parcel; import android.os.Parcelable; -import android.util.Log; + +import java.util.ArrayList; @SuppressWarnings("serial") public class ArticleList extends ArrayList
implements Parcelable { @@ -18,7 +16,15 @@ public class ArticleList extends ArrayList
implements Parcelable { public void writeToParcel(Parcel out, int flags) { out.writeList(this); } - + + public boolean containsId(int id) { + return findById(id) != null; + } + + public boolean contains(Article article) { + return containsId(article.id); + } + public Article findById(int id) { for (Article a : this) { if (a.id == id) @@ -36,15 +42,7 @@ public class ArticleList extends ArrayList
implements Parcelable { public ArticleList(Parcel in) { readFromParcel(in); } - - public boolean containsId(int id) { - for (Article a : this) { - if (a.id == id) - return true; - } - return false; - } - + @SuppressWarnings("rawtypes") public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {