reimplement moving between articles using volume buttons,

articlefragment clicks, etc
This commit is contained in:
Andrew Dolgov 2011-12-06 13:24:57 +03:00
parent d4b6e34b98
commit 797860e517
4 changed files with 121 additions and 67 deletions

View File

@ -6,6 +6,8 @@ import java.util.List;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import org.fox.ttrss.ArticleOps.RelativeArticle;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
@ -187,7 +189,7 @@ public class OfflineActivity extends FragmentActivity {
finish(); finish();
} }
public int getActiveOfflineFeedId() { public int getActiveFeedId() {
return m_activeFeedId; return m_activeFeedId;
} }
@ -735,69 +737,108 @@ public class OfflineActivity extends FragmentActivity {
} }
} }
/* @Override @Override
public boolean dispatchKeyEvent(KeyEvent event) { public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction(); int action = event.getAction();
int keyCode = event.getKeyCode(); int keyCode = event.getKeyCode();
switch (keyCode) { switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN: case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) { if (action == KeyEvent.ACTION_DOWN) {
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
if (hf != null && m_activeFeed != null) { OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
Article base = hf.getArticleById(hf.getActiveArticleId());
Article next = base != null ? getRelativeArticle(base, RelativeArticle.AFTER) : hf.getArticleAtPosition(0); int nextId = getRelativeArticleId(m_selectedArticleId, m_activeFeedId, RelativeArticle.AFTER);
if (next != null) { if (nextId != 0 && ohf != null) {
hf.setActiveArticleId(next.id); if (m_prefs.getBoolean("combined_mode", false)) {
ohf.setActiveArticleId(nextId);
boolean combinedMode = m_prefs.getBoolean("combined_mode", false); SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET unread = 0 " +
"WHERE " + BaseColumns._ID + " = ?");
if (combinedMode || m_selectedArticle == null) { stmt.bindLong(1, nextId);
next.unread = false; stmt.execute();
saveArticleUnread(next); stmt.close();
} else {
openArticle(next, 0); } else {
} openArticle(nextId, 0);
} }
} }
} }
return true; return true;
case KeyEvent.KEYCODE_VOLUME_UP: case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) { if (action == KeyEvent.ACTION_UP) {
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
if (hf != null && m_activeFeed != null) { OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
Article base = hf.getArticleById(hf.getActiveArticleId());
Article prev = base != null ? getRelativeArticle(base, RelativeArticle.BEFORE) : hf.getArticleAtPosition(0); int prevId = getRelativeArticleId(m_selectedArticleId, m_activeFeedId, RelativeArticle.BEFORE);
if (prev != null) { if (prevId != 0 && ohf != null) {
hf.setActiveArticleId(prev.id); if (m_prefs.getBoolean("combined_mode", false)) {
ohf.setActiveArticleId(prevId);
boolean combinedMode = m_prefs.getBoolean("combined_mode", false); SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET unread = 0 " +
"WHERE " + BaseColumns._ID + " = ?");
if (combinedMode || m_selectedArticle == null) { stmt.bindLong(1, prevId);
prev.unread = false; stmt.execute();
saveArticleUnread(prev); stmt.close();
} else {
openArticle(prev, 0); } else {
} openArticle(prevId, 0);
} }
} }
} }
return true; return true;
default: default:
return super.dispatchKeyEvent(event); return super.dispatchKeyEvent(event);
} }
} */ }
public void deselectAllArticles() { public void deselectAllArticles() {
getWritableDb().execSQL("UPDATE articles SET selected = 0 "); getWritableDb().execSQL("UPDATE articles SET selected = 0 ");
} }
public int getRelativeArticleId(int baseId, int feedId, ArticleOps.RelativeArticle mode) {
Cursor c;
/* if (baseId == 0) {
c = getReadableDb().query("articles",
null, "feed_id = ?",
new String[] { String.valueOf(feedId) }, null, null, "updated DESC LIMIT 1");
if (c.moveToFirst()) {
baseId = c.getInt(0);
}
c.close();
return baseId;
} */
if (mode == RelativeArticle.BEFORE) {
c = getReadableDb().query("articles",
null, "updated > (SELECT updated FROM articles WHERE "+BaseColumns._ID+" = ?) AND feed_id = ?",
new String[] { String.valueOf(baseId), String.valueOf(feedId) }, null, null, "updated LIMIT 1");
} else {
c = getReadableDb().query("articles",
null, "updated < (SELECT updated FROM articles WHERE "+BaseColumns._ID+" = ?) AND feed_id = ?",
new String[] { String.valueOf(baseId), String.valueOf(feedId) }, null, null, "updated DESC LIMIT 1");
}
int id = 0;
if (c.moveToFirst()) {
id = c.getInt(0);
}
c.close();
return id;
}
public void viewFeed(int feedId) { public void viewFeed(int feedId) {
m_activeFeedId = feedId; m_activeFeedId = feedId;
@ -828,6 +869,13 @@ public class OfflineActivity extends FragmentActivity {
hf.setActiveArticleId(articleId); hf.setActiveArticleId(articleId);
} }
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET unread = 0 " +
"WHERE " + BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId);
stmt.execute();
stmt.close();
OfflineArticleFragment frag = new OfflineArticleFragment(); OfflineArticleFragment frag = new OfflineArticleFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

View File

@ -7,7 +7,6 @@ import org.fox.ttrss.ArticleOps.RelativeArticle;
import android.app.Activity; import android.app.Activity;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -26,7 +25,6 @@ import android.webkit.WebView;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.google.ads.AdRequest;
import com.google.ads.AdView; import com.google.ads.AdView;
public class OfflineArticleFragment extends Fragment implements OnClickListener { public class OfflineArticleFragment extends Fragment implements OnClickListener {
@ -35,6 +33,8 @@ public class OfflineArticleFragment extends Fragment implements OnClickListener
private SharedPreferences m_prefs; private SharedPreferences m_prefs;
private int m_articleId; private int m_articleId;
private int m_nextArticleId;
private int m_prevArticleId;
private GestureDetector m_gestureDetector; private GestureDetector m_gestureDetector;
private View.OnTouchListener m_gestureListener; private View.OnTouchListener m_gestureListener;
private Cursor m_cursor; private Cursor m_cursor;
@ -44,6 +44,8 @@ public class OfflineArticleFragment extends Fragment implements OnClickListener
if (savedInstanceState != null) { if (savedInstanceState != null) {
m_articleId = savedInstanceState.getInt("articleId"); 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); View view = inflater.inflate(R.layout.article_fragment, container, false);
@ -163,21 +165,21 @@ public class OfflineArticleFragment extends Fragment implements OnClickListener
ImageView next = (ImageView)view.findViewById(R.id.next_article); ImageView next = (ImageView)view.findViewById(R.id.next_article);
if (next != null) { if (next != null) {
// if (m_nextArticle != null) { if (m_nextArticleId != 0) {
// next.setOnClickListener(this); next.setOnClickListener(this);
// } else { } else {
next.setImageResource(R.drawable.ic_next_article_disabled); next.setImageResource(R.drawable.ic_next_article_disabled);
// } }
} }
ImageView prev = (ImageView)view.findViewById(R.id.prev_article); ImageView prev = (ImageView)view.findViewById(R.id.prev_article);
if (prev != null) { if (prev != null) {
// if (m_prevArticle != null) { if (m_prevArticleId != 0) {
// prev.setOnClickListener(this); prev.setOnClickListener(this);
// } else { } else {
prev.setImageResource(R.drawable.ic_prev_article_disabled); prev.setImageResource(R.drawable.ic_prev_article_disabled);
// } }
} }
} }
@ -197,6 +199,9 @@ public class OfflineArticleFragment extends Fragment implements OnClickListener
super.onSaveInstanceState(out); super.onSaveInstanceState(out);
out.putInt("articleId", m_articleId); out.putInt("articleId", m_articleId);
out.putInt("prevArticleId", m_prevArticleId);
out.putInt("nextArticleId", m_nextArticleId);
} }
@Override @Override
@ -205,21 +210,21 @@ public class OfflineArticleFragment extends Fragment implements OnClickListener
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_articleId = ((OfflineActivity)activity).getSelectedArticleId(); OfflineActivity oa = (OfflineActivity)activity;
/* m_articleOps = (ArticleOps)activity;
m_article = m_articleOps.getSelectedArticle();
m_prevArticle = m_articleOps.getRelativeArticle(m_article, RelativeArticle.BEFORE); m_articleId = oa.getSelectedArticleId();
m_nextArticle = m_articleOps.getRelativeArticle(m_article, RelativeArticle.AFTER); */
m_prevArticleId = oa.getRelativeArticleId(m_articleId, oa.getActiveFeedId(), RelativeArticle.BEFORE);
m_nextArticleId = oa.getRelativeArticleId(m_articleId, oa.getActiveFeedId(), RelativeArticle.AFTER);
} }
@Override @Override
public void onClick(View v) { public void onClick(View v) {
/* if (v.getId() == R.id.next_article) { if (v.getId() == R.id.next_article) {
m_articleOps.openArticle(m_nextArticle, 0); ((OfflineActivity)getActivity()).openArticle(m_nextArticleId, 0);
} else if (v.getId() == R.id.prev_article) { } else if (v.getId() == R.id.prev_article) {
m_articleOps.openArticle(m_prevArticle, R.anim.slide_right); ((OfflineActivity)getActivity()).openArticle(m_prevArticleId, R.anim.slide_right);
} */ }
} }
// http://blog.blackmoonit.com/2010/07/gesture-detection-swipe-detection_4292.html // http://blog.blackmoonit.com/2010/07/gesture-detection-swipe-detection_4292.html
@ -238,14 +243,14 @@ public class OfflineArticleFragment extends Fragment implements OnClickListener
if (dX>0) { if (dX>0) {
//Log.d(TAG, "Right swipe"); //Log.d(TAG, "Right swipe");
//if (m_prevArticle != null) if (m_prevArticleId != 0)
// m_articleOps.openArticle(m_prevArticle, R.anim.slide_right); ((OfflineActivity)getActivity()).openArticle(m_prevArticleId, 0);
} else { } else {
//Log.d(TAG, "Left swipe"); //Log.d(TAG, "Left swipe");
//if (m_nextArticle != null) if (m_nextArticleId != 0)
// m_articleOps.openArticle(m_nextArticle, 0); ((OfflineActivity)getActivity()).openArticle(m_nextArticleId, 0);
} }
return true; return true;

View File

@ -89,7 +89,7 @@ public class OfflineFeedsFragment extends Fragment implements OnItemClickListene
list.setAdapter(m_adapter); list.setAdapter(m_adapter);
list.setOnItemClickListener(this); list.setOnItemClickListener(this);
list.setEmptyView(view.findViewById(R.id.no_unread_feeds)); list.setEmptyView(view.findViewById(R.id.no_feeds));
registerForContextMenu(list); registerForContextMenu(list);
view.findViewById(R.id.loading_container).setVisibility(View.GONE); view.findViewById(R.id.loading_container).setVisibility(View.GONE);

View File

@ -131,6 +131,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
list.setAdapter(m_adapter); list.setAdapter(m_adapter);
list.setOnItemClickListener(this); list.setOnItemClickListener(this);
list.setEmptyView(view.findViewById(R.id.no_headlines));
registerForContextMenu(list); registerForContextMenu(list);
view.findViewById(R.id.loading_progress).setVisibility(View.GONE); view.findViewById(R.id.loading_progress).setVisibility(View.GONE);
@ -146,7 +147,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
m_feedId = ((OfflineActivity)activity).getActiveOfflineFeedId(); m_feedId = ((OfflineActivity)activity).getActiveFeedId();
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_combinedMode = m_prefs.getBoolean("combined_mode", false); m_combinedMode = m_prefs.getBoolean("combined_mode", false);
} }