Commit 1bb82a84 authored by Adrien Dorsaz's avatar Adrien Dorsaz

Implements "Mark all read" option.

parent 84527686
......@@ -7,6 +7,7 @@ import ch.adorsaz.loungeDroid.R;
import ch.adorsaz.loungeDroid.article.ToDisplay;
import ch.adorsaz.loungeDroid.article.Article;
import ch.adorsaz.loungeDroid.servercom.ArticleListGetter;
import ch.adorsaz.loungeDroid.servercom.ArticleMarkAllRead;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
......@@ -140,6 +141,9 @@ public class ArticleListActivity extends ListActivity {
mDisplayChoice = ToDisplay.STARRED;
fetchNews();
break;
case R.id.markAllRead:
markAllRead();
break;
case R.id.menu_settings:
SettingsActivity.setWantToEdit();
......@@ -192,6 +196,23 @@ public class ArticleListActivity extends ListActivity {
getter.execute(mDisplayChoice);
}
private void markAllRead() {
if (mArticleAdapter != null) {
ArticleMarkAllRead allMarker = new ArticleMarkAllRead(this);
List<Integer> articleIdList = new LinkedList<Integer>();
for (int articleIterator = 0; articleIterator < mArticleAdapter
.getCount(); articleIterator++) {
Article oneArticle = mArticleAdapter.getItem(articleIterator);
if (!oneArticle.isRead()) {
articleIdList.add(oneArticle.getId());
}
}
allMarker.execute(articleIdList);
}
}
private void disableToDisplayMenu(Menu menu) {
MenuItem showAllItem = menu.findItem(R.id.showAllMenu);
MenuItem showUnreadItem = menu.findItem(R.id.showUnreadMenu);
......
package ch.adorsaz.loungeDroid.servercom;
import java.net.ConnectException;
import java.util.Arrays;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import ch.adorsaz.loungeDroid.activities.ArticleListActivity;
import ch.adorsaz.loungeDroid.exception.ReadStateUpdateException;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
/**
* ArticleMarkAllRead is an async task to mark all articles read in
* ArticleListActivity.
* */
public class ArticleMarkAllRead extends
AsyncTask<List<Integer>, Object, Boolean> {
private SessionManager mSessionManager = null;
private ArticleListActivity mActivity = null;
private List<Integer> mArticleIdList = null;
/* Some urls needed to get feeds */
private final static String MARKALL_READ_RSSLOUNGE = "/item/markall";
private final static String ITEM_POST_ID = "items[]";
public ArticleMarkAllRead(ArticleListActivity activity) {
mActivity = activity;
}
@Override
protected void onPreExecute() {
mSessionManager = SessionManager.getInstance(mActivity
.getApplicationContext());
}
@Override
protected Boolean doInBackground(List<Integer>... articleId) {
boolean hasWorked = false;
try {
mArticleIdList = articleId[0];
hasWorked = updateArticles();
} catch (ConnectException e) {
// TODO Auto-generated catch block
Log.e(SessionManager.LOG_SERVER,
"There was an error with network connection.");
e.printStackTrace();
hasWorked = false;
} catch (ReadStateUpdateException e) {
// Try again login and request.
SessionManager.deleteSessionCookie();
try {
hasWorked = updateArticles();
} catch (ConnectException e1) {
// TODO Auto-generated catch block
Log.e(SessionManager.LOG_SERVER,
"There was an error with network connection.");
e1.printStackTrace();
hasWorked = false;
} catch (ReadStateUpdateException e1) {
Log.e(SessionManager.LOG_SERVER,
"Error while updating read state twice. Try again later");
e1.printStackTrace();
hasWorked = false;
}
}
return hasWorked;
}
@Override
protected void onPostExecute(Boolean hasWorked) {
if (hasWorked) {
} else {
Toast.makeText(
mActivity,
"Unable to update rss feed. Have you network connection ? Are your settings correct ?",
Toast.LENGTH_LONG).show();
}
}
private boolean updateArticles()
throws ReadStateUpdateException,
ConnectException {
Boolean hasWorked = false;
String httpParams = SessionManager.JSON_GET_RSSLOUNGE;
for (Integer articleId : mArticleIdList) {
httpParams += "&" + Uri.encode(ITEM_POST_ID) + "=" + articleId;
}
JSONObject jsonResponse = mSessionManager.serverRequest(
MARKALL_READ_RSSLOUNGE, httpParams);
if (jsonResponse.has("next")) {
hasWorked = true;
} else {
throw new ReadStateUpdateException();
}
return hasWorked;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment