From 5f7db73f89b2b35d83d24b4974243d2054990569 Mon Sep 17 00:00:00 2001 From: ojiikun Date: Tue, 14 Jan 2014 11:10:48 +0000 Subject: [PATCH] Mark-read idempotency for social feeds. (#460) --- .../src/com/newsblur/activity/Reading.java | 3 +- .../com/newsblur/database/FeedProvider.java | 19 +++++++++-- .../src/com/newsblur/util/AppConstants.java | 2 +- .../src/com/newsblur/util/FeedUtils.java | 32 ++++++++++++------- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/clients/android/NewsBlur/src/com/newsblur/activity/Reading.java b/clients/android/NewsBlur/src/com/newsblur/activity/Reading.java index bbe609652..be6edc866 100644 --- a/clients/android/NewsBlur/src/com/newsblur/activity/Reading.java +++ b/clients/android/NewsBlur/src/com/newsblur/activity/Reading.java @@ -151,8 +151,9 @@ public abstract class Reading extends NbFragmentActivity implements OnPageChange if (this.pager == null) { // if this is the first time we've found a cursor, we know the onCreate chain is done - setupPager(); this.startingUnreadCount = getUnreadCount(); + // set up the pager after the unread count, so the first mark-read doesn't happen too quickly + setupPager(); } try { diff --git a/clients/android/NewsBlur/src/com/newsblur/database/FeedProvider.java b/clients/android/NewsBlur/src/com/newsblur/database/FeedProvider.java index 02365099b..6c2caff8e 100644 --- a/clients/android/NewsBlur/src/com/newsblur/database/FeedProvider.java +++ b/clients/android/NewsBlur/src/com/newsblur/database/FeedProvider.java @@ -347,6 +347,18 @@ public class FeedProvider extends ContentProvider { public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) { return mdb.query(table, columns, selection, selectionArgs, groupBy, having, orderBy); } + public void execSQL(String sql) { + if (AppConstants.VERBOSE_LOG) { + Log.d(LoggingDatabase.class.getName(), "execSQL: " + sql); + } + mdb.execSQL(sql); + } + public int update(String table, ContentValues values, String whereClause, String[] whereArgs) { + return mdb.update(table, values, whereClause, whereArgs); + } + public long insertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, int conflictAlgorithm) { + return mdb.insertWithOnConflict(table, nullColumnHack, initialValues, conflictAlgorithm); + } } @Override @@ -603,7 +615,8 @@ public class FeedProvider extends ContentProvider { @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { - final SQLiteDatabase db = databaseHelper.getWritableDatabase(); + final SQLiteDatabase rwdb = databaseHelper.getWritableDatabase(); + final LoggingDatabase db = new LoggingDatabase(rwdb); switch (uriMatcher.match(uri)) { case ALL_FEEDS: @@ -622,10 +635,10 @@ public class FeedProvider extends ContentProvider { // In order to run a raw SQL query whereby we make decrement the column we need to a dynamic reference - something the usual content provider can't easily handle. Hence this circuitous hack. case FEED_COUNT: db.execSQL("UPDATE " + DatabaseConstants.FEED_TABLE + " SET " + selectionArgs[0] + " = " + selectionArgs[0] + " - 1 WHERE " + DatabaseConstants.FEED_ID + " = " + selectionArgs[1]); - return 0; + return 1; case SOCIALFEED_COUNT: db.execSQL("UPDATE " + DatabaseConstants.SOCIALFEED_TABLE + " SET " + selectionArgs[0] + " = " + selectionArgs[0] + " - 1 WHERE " + DatabaseConstants.SOCIAL_FEED_ID + " = " + selectionArgs[1]); - return 0; + return 1; case STARRED_STORIES_COUNT: int rows = db.update(DatabaseConstants.STARRED_STORY_COUNT_TABLE, values, null, null); if (rows == 0 ) { diff --git a/clients/android/NewsBlur/src/com/newsblur/util/AppConstants.java b/clients/android/NewsBlur/src/com/newsblur/util/AppConstants.java index 313b30bdd..91fe6e352 100644 --- a/clients/android/NewsBlur/src/com/newsblur/util/AppConstants.java +++ b/clients/android/NewsBlur/src/com/newsblur/util/AppConstants.java @@ -5,7 +5,7 @@ public class AppConstants { // Enables high-volume logging that may be useful for debugging. This should // never be enabled for releases, as it not only slows down the app considerably, // it will log sensitive info such as passwords! - public static final boolean VERBOSE_LOG = false; + public static final boolean VERBOSE_LOG = true; public static final int STATE_ALL = 0; public static final int STATE_SOME = 1; diff --git a/clients/android/NewsBlur/src/com/newsblur/util/FeedUtils.java b/clients/android/NewsBlur/src/com/newsblur/util/FeedUtils.java index 03900d15b..056d51b23 100644 --- a/clients/android/NewsBlur/src/com/newsblur/util/FeedUtils.java +++ b/clients/android/NewsBlur/src/com/newsblur/util/FeedUtils.java @@ -320,20 +320,28 @@ public class FeedUtils { } 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()); - } + HashSet socialIds = new HashSet(); + if (!TextUtils.isEmpty(story.socialUserId)) { + socialIds.add(story.socialUserId); + } + if (story.friendUserIds != null) { + for (String id : story.friendUserIds) { + socialIds.add(id); + } + } + for (String id : socialIds) { + String[] socialSelectionArgs; + if (story.getIntelligenceTotal() > 0) { + socialSelectionArgs = new String[] { DatabaseConstants.SOCIAL_FEED_POSITIVE_COUNT, id } ; + } else if (story.getIntelligenceTotal() == 0) { + socialSelectionArgs = new String[] { DatabaseConstants.SOCIAL_FEED_NEUTRAL_COUNT, id } ; + } else { + socialSelectionArgs = new String[] { DatabaseConstants.SOCIAL_FEED_NEGATIVE_COUNT, id } ; + } + 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();