use viewpager-based article swiping for offline mode

This commit is contained in:
Andrew Dolgov 2011-12-10 10:22:27 +03:00
parent 71e5c1f770
commit 3467aa5dc0
7 changed files with 174 additions and 35 deletions

View File

@ -19,7 +19,7 @@ public class ArticlePager extends Fragment {
private class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm, Article article) {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@ -55,12 +55,11 @@ public class ArticlePager extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.article_pager, container, false);
m_adapter = new PagerAdapter(getActivity().getSupportFragmentManager(), m_onlineServices.getSelectedArticle());
m_adapter = new PagerAdapter(getActivity().getSupportFragmentManager());
ViewPager pager = (ViewPager) view.findViewById(R.id.article_pager);
HeadlinesFragment hf = (HeadlinesFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
int position = hf.getArticlePosition(m_article);
int position = m_hf.getArticlePosition(m_article);
pager.setAdapter(m_adapter);
pager.setCurrentItem(position);
@ -79,10 +78,12 @@ public class ArticlePager extends Fragment {
Article article = m_hf.getArticleAtPosition(position);
if (article != null) {
if (article.unread) {
article.unread = false;
m_onlineServices.setSelectedArticle(article);
m_onlineServices.saveArticleUnread(article);
}
m_onlineServices.setSelectedArticle(article);
}
}
});

View File

@ -1070,7 +1070,11 @@ public class MainActivity extends FragmentActivity implements OnlineServices {
m_selectedArticle = null;
initMainMenu();
if (!m_enableCats || m_activeCategory != null)
refreshFeeds();
else
refreshCategories();
}

View File

@ -14,6 +14,7 @@ import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
@ -625,11 +626,12 @@ public class OfflineActivity extends FragmentActivity implements
}
// we don't want to lose selected article in headlines so we refresh them before setting selected id to 0
refreshViews();
m_selectedArticleId = 0;
initMainMenu();
refreshViews();
}
private int getSelectedArticleCount() {
@ -990,7 +992,13 @@ public class OfflineActivity extends FragmentActivity implements
stmt.execute();
stmt.close();
OfflineArticleFragment frag = new OfflineArticleFragment();
Fragment frag;
if (m_smallScreenMode) {
frag = new OfflineArticlePager(articleId);
} else {
frag = new OfflineArticleFragment(articleId);
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.article_fragment, frag);
@ -1020,4 +1028,10 @@ public class OfflineActivity extends FragmentActivity implements
public int getSelectedArticleId() {
return m_selectedArticleId;
}
@Override
public void setSelectedArticleId(int articleId) {
m_selectedArticleId = articleId;
refreshViews();
}
}

View File

@ -39,19 +39,23 @@ public class OfflineArticleFragment extends Fragment {
private SharedPreferences m_prefs;
private int m_articleId;
private int m_nextArticleId;
private int m_prevArticleId;
private Cursor m_cursor;
private OfflineServices m_offlineServices;
public OfflineArticleFragment() {
super();
}
public OfflineArticleFragment(int articleId) {
super();
m_articleId = articleId;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_articleId = savedInstanceState.getInt("articleId");
m_prevArticleId = savedInstanceState.getInt("prevArticleId");
m_nextArticleId = savedInstanceState.getInt("nextArticleId");
}
View view = inflater.inflate(R.layout.article_fragment, container, false);
@ -204,9 +208,6 @@ public class OfflineArticleFragment extends Fragment {
super.onSaveInstanceState(out);
out.putInt("articleId", m_articleId);
out.putInt("prevArticleId", m_prevArticleId);
out.putInt("nextArticleId", m_nextArticleId);
}
@Override
@ -216,11 +217,6 @@ public class OfflineArticleFragment extends Fragment {
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_offlineServices = (OfflineServices)activity;
m_articleId = m_offlineServices.getSelectedArticleId();
m_prevArticleId = m_offlineServices.getRelativeArticleId(m_articleId, m_offlineServices.getActiveFeedId(), RelativeArticle.BEFORE);
m_nextArticleId = m_offlineServices.getRelativeArticleId(m_articleId, m_offlineServices.getActiveFeedId(), RelativeArticle.AFTER);
}

View File

@ -0,0 +1,108 @@
package org.fox.ttrss;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OfflineArticlePager extends Fragment {
private PagerAdapter m_adapter;
private OfflineServices m_offlineServices;
private OfflineHeadlinesFragment m_hf;
private int m_articleId;
private class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
int articleId = m_hf.getArticleIdAtPosition(position);
if (articleId != 0) {
return new OfflineArticleFragment(articleId);
}
return null;
}
@Override
public int getCount() {
return m_hf.getArticleCount();
}
}
public OfflineArticlePager() {
super();
}
public OfflineArticlePager(int articleId) {
super();
m_articleId = articleId;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.article_pager, container, false);
m_adapter = new PagerAdapter(getActivity().getSupportFragmentManager());
ViewPager pager = (ViewPager) view.findViewById(R.id.article_pager);
int position = m_hf.getArticleIdPosition(m_articleId);
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) {
int articleId = m_hf.getArticleIdAtPosition(position);
if (articleId != 0) {
m_offlineServices.setSelectedArticleId(articleId);
SQLiteStatement stmt = m_offlineServices.getWritableDb().compileStatement(
"UPDATE articles SET unread = 0 " + "WHERE " + BaseColumns._ID
+ " = ?");
stmt.bindLong(1, articleId);
stmt.execute();
stmt.close();
}
}
});
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_hf = (OfflineHeadlinesFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
m_offlineServices = (OfflineServices)activity;
}
}

View File

@ -107,6 +107,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
if (m_cursor != null) {
m_adapter.changeCursor(m_cursor);
setActiveArticleId(m_offlineServices.getSelectedArticleId());
m_adapter.notifyDataSetChanged();
}
}
@ -229,7 +230,9 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
public int getItemViewType(int position) {
Cursor c = (Cursor) getItem(position);
if (c.getLong(0) == m_activeArticleId) {
//Log.d(TAG, "@gIVT " + position + " " + c.getInt(0) + " vs " + m_activeArticleId);
if (c.getInt(0) == m_activeArticleId) {
return VIEW_SELECTED;
} else if (c.getInt(c.getColumnIndex("unread")) == 1) {
return VIEW_UNREAD;
@ -394,16 +397,15 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
m_adapter.notifyDataSetChanged();
}
public void setActiveArticleId(int id) {
m_activeArticleId = id;
m_adapter.notifyDataSetChanged();
public void setActiveArticleId(int articleId) {
m_activeArticleId = articleId;
// m_adapter.notifyDataSetChanged();
/* ListView list = (ListView)getView().findViewById(R.id.headlines);
ListView list = (ListView)getView().findViewById(R.id.headlines);
if (list != null) {
int position = m_adapter.getPosition(getArticleById(id));
list.setSelection(position);
} */
list.setSelection(getArticleIdPosition(articleId));
}
}
public Cursor getArticleAtPosition(int position) {
@ -411,18 +413,31 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
}
public int getArticleIdAtPosition(int position) {
Cursor c = getArticleAtPosition(position);
/*Cursor c = getArticleAtPosition(position);
if (c != null) {
int id = c.getInt(0);
return id;
}
} */
return 0;
return (int) m_adapter.getItemId(position);
}
public int getActiveArticleId() {
return m_activeArticleId;
}
public int getArticleIdPosition(int articleId) {
for (int i = 0; i < m_adapter.getCount(); i++) {
if (articleId == m_adapter.getItemId(i))
return i;
}
return 0;
}
public int getArticleCount() {
return m_adapter.getCount();
}
}

View File

@ -13,4 +13,5 @@ public interface OfflineServices {
public int getSelectedArticleId();
public void initMainMenu();
public boolean isSmallScreen();
public void setSelectedArticleId(int articleId);
}