2011-12-09 20:49:55 +00:00
|
|
|
package org.fox.ttrss;
|
|
|
|
|
2012-06-19 10:18:00 +00:00
|
|
|
import org.fox.ttrss.types.Article;
|
2012-09-16 15:25:47 +00:00
|
|
|
import org.fox.ttrss.types.ArticleList;
|
2012-06-19 10:18:00 +00:00
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.os.Bundle;
|
2012-06-19 14:24:22 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.FragmentManager;
|
|
|
|
import android.support.v4.app.FragmentStatePagerAdapter;
|
2011-12-09 20:49:55 +00:00
|
|
|
import android.support.v4.view.ViewPager;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
public class ArticlePager extends Fragment {
|
|
|
|
|
2012-06-20 09:12:46 +00:00
|
|
|
private final String TAG = "ArticlePager";
|
2011-12-09 20:49:55 +00:00
|
|
|
private PagerAdapter m_adapter;
|
2012-09-16 09:25:28 +00:00
|
|
|
private HeadlinesEventListener m_onlineServices;
|
2011-12-09 20:49:55 +00:00
|
|
|
private Article m_article;
|
2012-09-16 15:25:47 +00:00
|
|
|
private ArticleList m_articles;
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
private class PagerAdapter extends FragmentStatePagerAdapter {
|
|
|
|
|
2011-12-10 07:22:27 +00:00
|
|
|
public PagerAdapter(FragmentManager fm) {
|
2011-12-09 20:49:55 +00:00
|
|
|
super(fm);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Fragment getItem(int position) {
|
2012-09-16 15:25:47 +00:00
|
|
|
Article article = m_articles.get(position);
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
if (article != null) {
|
|
|
|
ArticleFragment af = new ArticleFragment(article);
|
|
|
|
return af;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
2012-09-16 15:25:47 +00:00
|
|
|
return m_articles.size();
|
2011-12-09 20:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArticlePager() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2012-09-16 15:25:47 +00:00
|
|
|
public ArticlePager(Article article, ArticleList articles) {
|
2011-12-09 20:49:55 +00:00
|
|
|
super();
|
|
|
|
|
|
|
|
m_article = article;
|
2012-09-16 15:25:47 +00:00
|
|
|
m_articles = articles;
|
2011-12-09 20:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.article_pager, container, false);
|
|
|
|
|
2012-09-16 15:25:47 +00:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
m_articles = savedInstanceState.getParcelable("articles");
|
|
|
|
m_article = savedInstanceState.getParcelable("article");
|
|
|
|
}
|
|
|
|
|
2012-06-19 14:24:22 +00:00
|
|
|
m_adapter = new PagerAdapter(getActivity().getSupportFragmentManager());
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
ViewPager pager = (ViewPager) view.findViewById(R.id.article_pager);
|
|
|
|
|
2012-09-16 15:25:47 +00:00
|
|
|
int position = m_articles.indexOf(m_article);
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
pager.setAdapter(m_adapter);
|
|
|
|
pager.setCurrentItem(position);
|
|
|
|
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrollStateChanged(int arg0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrolled(int arg0, float arg1, int arg2) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageSelected(int position) {
|
2012-09-16 15:25:47 +00:00
|
|
|
Article article = m_articles.get(position);
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
if (article != null) {
|
2011-12-10 07:22:27 +00:00
|
|
|
if (article.unread) {
|
|
|
|
article.unread = false;
|
|
|
|
m_onlineServices.saveArticleUnread(article);
|
|
|
|
}
|
2012-09-16 09:25:28 +00:00
|
|
|
m_onlineServices.onArticleSelected(article, false);
|
2012-06-20 09:12:46 +00:00
|
|
|
|
|
|
|
//Log.d(TAG, "Page #" + position + "/" + m_adapter.getCount());
|
|
|
|
|
|
|
|
if (position == m_adapter.getCount() - 5) {
|
2012-09-16 15:25:47 +00:00
|
|
|
// FIXME load more articles somehow
|
|
|
|
//m_hf.refresh(true);
|
2012-06-20 09:12:46 +00:00
|
|
|
m_adapter.notifyDataSetChanged();
|
|
|
|
}
|
2011-12-09 20:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2012-09-16 15:25:47 +00:00
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle out) {
|
|
|
|
super.onSaveInstanceState(out);
|
|
|
|
|
|
|
|
out.putParcelable("articles", m_articles);
|
|
|
|
out.putParcelable("article", m_article);
|
|
|
|
}
|
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
|
2012-09-16 09:25:28 +00:00
|
|
|
m_onlineServices = (HeadlinesEventListener)activity;
|
2012-09-16 16:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2012-09-16 15:25:47 +00:00
|
|
|
|
2012-09-16 16:46:54 +00:00
|
|
|
((OnlineActivity)getActivity()).initMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Article getSelectedArticle() {
|
|
|
|
return m_article;
|
2011-12-09 20:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|