unify interfaces used by headlines fragment

This commit is contained in:
Andrew Dolgov 2011-11-27 16:11:39 +03:00
parent 592175ae7b
commit de1db3b5ff
3 changed files with 11 additions and 14 deletions

View File

@ -5,6 +5,7 @@ public interface ArticleOps {
public void saveArticleUnread(final Article article); public void saveArticleUnread(final Article article);
public void saveArticleMarked(final Article article); public void saveArticleMarked(final Article article);
public void saveArticlePublished(final Article article); public void saveArticlePublished(final Article article);
public void onArticleOpened(Article article);
public void updateHeadlines(); public void updateHeadlines();
} }

View File

@ -46,28 +46,25 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
private SharedPreferences m_prefs; private SharedPreferences m_prefs;
private Feed m_feed; private Feed m_feed;
private int m_selectedArticleId; private int m_activeArticleId;
private ArticleListAdapter m_adapter; private ArticleListAdapter m_adapter;
private ArticleList m_articles = new ArticleList(); private ArticleList m_articles = new ArticleList();
private ArticleList m_selectedArticles = new ArticleList(); private ArticleList m_selectedArticles = new ArticleList();
private OnArticleSelectedListener m_articleSelectedListener;
private ArticleOps m_articleOps; private ArticleOps m_articleOps;
public interface OnArticleSelectedListener { public ArticleList getSelectedArticles() {
public void onArticleSelected(Article article); return m_selectedArticles;
} }
@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_feed = savedInstanceState.getParcelable("feed"); m_feed = savedInstanceState.getParcelable("feed");
m_articles = savedInstanceState.getParcelable("articles"); m_articles = savedInstanceState.getParcelable("articles");
m_selectedArticleId = savedInstanceState.getInt("selectedArticleId"); m_activeArticleId = savedInstanceState.getInt("activeArticleId");
m_selectedArticles = savedInstanceState.getParcelable("selectedArticles"); m_selectedArticles = savedInstanceState.getParcelable("selectedArticles");
} }
@ -104,7 +101,6 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
super.onAttach(activity); super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_feed = ((MainActivity)activity).getActiveFeed(); m_feed = ((MainActivity)activity).getActiveFeed();
m_articleSelectedListener = (OnArticleSelectedListener) activity;
m_articleOps = (ArticleOps) activity; m_articleOps = (ArticleOps) activity;
} }
@ -114,14 +110,14 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
if (list != null) { if (list != null) {
Article article = (Article)list.getItemAtPosition(position); Article article = (Article)list.getItemAtPosition(position);
m_articleSelectedListener.onArticleSelected(article); m_articleOps.onArticleOpened(article);
if (article.unread) { if (article.unread) {
article.unread = false; article.unread = false;
m_articleOps.saveArticleUnread(article); m_articleOps.saveArticleUnread(article);
} }
m_selectedArticleId = article.id; m_activeArticleId = article.id;
m_adapter.notifyDataSetChanged(); m_adapter.notifyDataSetChanged();
} }
} }
@ -172,7 +168,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
out.putParcelable("feed", m_feed); out.putParcelable("feed", m_feed);
out.putParcelable("articles", m_articles); out.putParcelable("articles", m_articles);
out.putInt("selectedArticleId", m_selectedArticleId); out.putInt("activeArticleId", m_activeArticleId);
out.putParcelable("selectedArticles", m_selectedArticles); out.putParcelable("selectedArticles", m_selectedArticles);
} }
@ -279,7 +275,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
public int getItemViewType(int position) { public int getItemViewType(int position) {
Article a = items.get(position); Article a = items.get(position);
if (a.id == m_selectedArticleId) { if (a.id == m_activeArticleId) {
return VIEW_SELECTED; return VIEW_SELECTED;
} else if (a.unread) { } else if (a.unread) {
return VIEW_UNREAD; return VIEW_UNREAD;

View File

@ -26,7 +26,7 @@ import android.widget.TextView;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
public class MainActivity extends FragmentActivity implements FeedsFragment.OnFeedSelectedListener, HeadlinesFragment.OnArticleSelectedListener, ArticleOps { public class MainActivity extends FragmentActivity implements FeedsFragment.OnFeedSelectedListener, ArticleOps {
private final String TAG = this.getClass().getSimpleName(); private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs; private SharedPreferences m_prefs;
@ -649,7 +649,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
} }
@Override @Override
public void onArticleSelected(Article article) { public void onArticleOpened(Article article) {
openArticle(article); openArticle(article);
} }