Merge pull request #206 from lance0428/master

Fix issue of 'marking read' or 'marking previous read' from feed story list not updating feed counts correctly
This commit is contained in:
Daniel 2013-04-22 10:07:54 -07:00
commit 5a6c5236cc
6 changed files with 89 additions and 69 deletions

View file

@ -5,7 +5,6 @@ import java.util.HashSet;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.database.Cursor;
@ -16,7 +15,6 @@ import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
@ -26,7 +24,6 @@ import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.newsblur.R;
import com.newsblur.database.DatabaseConstants;
import com.newsblur.database.FeedProvider;
import com.newsblur.domain.Story;
import com.newsblur.domain.UserDetails;
@ -210,39 +207,9 @@ public abstract class Reading extends NbFragmentActivity implements OnPageChange
}
protected void addStoryToMarkAsRead(Story story) {
if (story.read != 1 && !storiesToMarkAsRead.contains(story.id)) {
if (story.read != Story.READ && !storiesToMarkAsRead.contains(story.id)) {
storiesToMarkAsRead.add(story.id);
String[] selectionArgs;
ContentValues emptyValues = new ContentValues();
emptyValues.put(DatabaseConstants.FEED_ID, story.feedId);
if (story.getIntelligenceTotal() > 0) {
selectionArgs = new String[] { DatabaseConstants.FEED_POSITIVE_COUNT, story.feedId } ;
} else if (story.getIntelligenceTotal() == 0) {
selectionArgs = new String[] { DatabaseConstants.FEED_NEUTRAL_COUNT, story.feedId } ;
} else {
selectionArgs = new String[] { DatabaseConstants.FEED_NEGATIVE_COUNT, story.feedId } ;
}
operations.add(ContentProviderOperation.newUpdate(FeedProvider.FEED_COUNT_URI).withValues(emptyValues).withSelection("", selectionArgs).build());
if (!TextUtils.isEmpty(story.socialUserId)) {
String[] socialSelectionArgs;
if (story.getIntelligenceTotal() > 0) {
socialSelectionArgs = new String[] { DatabaseConstants.SOCIAL_FEED_POSITIVE_COUNT, story.socialUserId } ;
} else if (story.getIntelligenceTotal() == 0) {
socialSelectionArgs = new String[] { DatabaseConstants.SOCIAL_FEED_NEUTRAL_COUNT, story.socialUserId } ;
} else {
socialSelectionArgs = new String[] { DatabaseConstants.SOCIAL_FEED_NEGATIVE_COUNT, story.socialUserId } ;
}
operations.add(ContentProviderOperation.newUpdate(FeedProvider.SOCIALCOUNT_URI).withValues(emptyValues).withSelection("", socialSelectionArgs).build());
}
Uri storyUri = FeedProvider.STORY_URI.buildUpon().appendPath(story.id).build();
ContentValues values = new ContentValues();
values.put(DatabaseConstants.STORY_READ, true);
operations.add(ContentProviderOperation.newUpdate(storyUri).withValues(values).build());
FeedUtils.appendStoryReadOperations(story, operations);
}
}

View file

@ -61,7 +61,7 @@ public class FeedItemsAdapter extends SimpleCursorAdapter {
}
// 1 is read
if (Story.fromCursor(cursor).read == 0) {
if (Story.fromCursor(cursor).read == Story.UNREAD) {
((TextView) v.findViewById(R.id.row_item_author)).setTextColor(storyAuthorUnread);
((TextView) v.findViewById(R.id.row_item_date)).setTextColor(storyDateUnread);

View file

@ -70,7 +70,7 @@ public class MultipleFeedItemsAdapter extends SimpleCursorAdapter {
}
// 1 is read
if (Story.fromCursor(cursor).read == 0) {
if (Story.fromCursor(cursor).read == Story.UNREAD) {
((TextView) v.findViewById(R.id.row_item_author)).setTextColor(storyAuthorUnread);
((TextView) v.findViewById(R.id.row_item_date)).setTextColor(storyDateUnread);
((TextView) v.findViewById(R.id.row_item_feedtitle)).setTextColor(storyFeedUnread);

View file

@ -18,6 +18,9 @@ public class Story implements Serializable {
private static final long serialVersionUID = 7629596752129163308L;
public static final int UNREAD = 0;
public static final int READ = 1;
public String id;
@SerializedName("story_permalink")

View file

@ -2,16 +2,20 @@ package com.newsblur.fragment;
import java.util.ArrayList;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
@ -36,6 +40,7 @@ import com.newsblur.database.FeedProvider;
import com.newsblur.domain.Feed;
import com.newsblur.domain.Story;
import com.newsblur.network.MarkStoryAsReadTask;
import com.newsblur.util.FeedUtils;
import com.newsblur.util.NetworkUtils;
import com.newsblur.view.FeedItemViewBinder;
@ -179,46 +184,49 @@ public class FeedItemListFragment extends ItemListFragment implements LoaderMana
final AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
if (item.getItemId() == R.id.menu_mark_story_as_read) {
final Story story = adapter.getStory(menuInfo.position);
ArrayList<String> storyIdsToMarkRead = new ArrayList<String>();
storyIdsToMarkRead.add(story.id);
new MarkStoryAsReadTask(getActivity(), storyIdsToMarkRead, feedId) {
@Override
protected void onPostExecute(Void result) {
// TODO this isn't sufficient. We also need to update counts
ContentValues values = new ContentValues();
values.put(DatabaseConstants.STORY_READ, true);
contentResolver.update(FeedProvider.STORY_URI.buildUpon().appendPath(story.id).build(), values, null, null);
refreshStories();
}
}.execute();
if(story.read == Story.UNREAD) {
ArrayList<Story> storiesToMarkAsRead = new ArrayList<Story>();
storiesToMarkAsRead.add(story);
markStoriesRead(storiesToMarkAsRead);
}
} else if (item.getItemId() == R.id.menu_mark_previous_stories_as_read) {
ArrayList<Story> previousStories = adapter.getPreviousStories(menuInfo.position);
final ArrayList<String> storyIdsToMarkRead = new ArrayList<String>();
final ArrayList<Story> previousStories = adapter.getPreviousStories(menuInfo.position);
ArrayList<Story> storiesToMarkAsRead = new ArrayList<Story>();
for(Story story: previousStories) {
if(story.read == 0) {
storyIdsToMarkRead.add(story.id);
if(story.read == Story.UNREAD) {
storiesToMarkAsRead.add(story);
}
}
new MarkStoryAsReadTask(getActivity(), storyIdsToMarkRead, feedId) {
@Override
protected void onPostExecute(Void result) {
// TODO this isn't sufficient. We also need to update counts
ContentValues values = new ContentValues();
values.put(DatabaseConstants.STORY_READ, true);
for(String storyId: storyIdsToMarkRead) {
contentResolver.update(FeedProvider.STORY_URI.buildUpon().appendPath(storyId).build(), values, null, null);
}
refreshStories();
}
}.execute();
markStoriesRead(storiesToMarkAsRead);
}
return super.onContextItemSelected(item);
}
private void markStoriesRead(final ArrayList<Story> storiesToMarkRead) {
ArrayList<String> storyIdsToMarkRead = new ArrayList<String>();
for(Story story: storiesToMarkRead) {
storyIdsToMarkRead.add(story.id);
}
new MarkStoryAsReadTask(getActivity(), storyIdsToMarkRead, feedId) {
@Override
protected void onPostExecute(Void result) {
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
for(Story story: storiesToMarkRead) {
FeedUtils.appendStoryReadOperations(story, operations);
}
try {
contentResolver.applyBatch(FeedProvider.AUTHORITY, operations);
} catch (Exception e) {
Log.e(TAG, "Failed to update feed read status in local DB for " + storiesToMarkRead.size() + " stories");
e.printStackTrace();
}
refreshStories();
}
}.execute();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {

View file

@ -1,11 +1,19 @@
package com.newsblur.util;
import java.util.ArrayList;
import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.newsblur.R;
import com.newsblur.database.DatabaseConstants;
import com.newsblur.database.FeedProvider;
import com.newsblur.domain.Story;
import com.newsblur.network.APIManager;
@ -33,4 +41,38 @@ public class FeedUtils {
Log.w(FeedUtils.class.getName(), "Couldn't save story, no selection found.");
}
}
public static void appendStoryReadOperations(Story story, ArrayList<ContentProviderOperation> operations) {
String[] selectionArgs;
ContentValues emptyValues = new ContentValues();
emptyValues.put(DatabaseConstants.FEED_ID, story.feedId);
if (story.getIntelligenceTotal() > 0) {
selectionArgs = new String[] { DatabaseConstants.FEED_POSITIVE_COUNT, story.feedId } ;
} else if (story.getIntelligenceTotal() == 0) {
selectionArgs = new String[] { DatabaseConstants.FEED_NEUTRAL_COUNT, story.feedId } ;
} else {
selectionArgs = new String[] { DatabaseConstants.FEED_NEGATIVE_COUNT, story.feedId } ;
}
operations.add(ContentProviderOperation.newUpdate(FeedProvider.FEED_COUNT_URI).withValues(emptyValues).withSelection("", selectionArgs).build());
if (!TextUtils.isEmpty(story.socialUserId)) {
String[] socialSelectionArgs;
if (story.getIntelligenceTotal() > 0) {
socialSelectionArgs = new String[] { DatabaseConstants.SOCIAL_FEED_POSITIVE_COUNT, story.socialUserId } ;
} else if (story.getIntelligenceTotal() == 0) {
socialSelectionArgs = new String[] { DatabaseConstants.SOCIAL_FEED_NEUTRAL_COUNT, story.socialUserId } ;
} else {
socialSelectionArgs = new String[] { DatabaseConstants.SOCIAL_FEED_NEGATIVE_COUNT, story.socialUserId } ;
}
operations.add(ContentProviderOperation.newUpdate(FeedProvider.SOCIALCOUNT_URI).withValues(emptyValues).withSelection("", socialSelectionArgs).build());
}
Uri storyUri = FeedProvider.STORY_URI.buildUpon().appendPath(story.id).build();
ContentValues values = new ContentValues();
values.put(DatabaseConstants.STORY_READ, true);
operations.add(ContentProviderOperation.newUpdate(storyUri).withValues(values).build());
}
}