22
33import android .content .Context ;
44
5+ import androidx .annotation .NonNull ;
6+
57import com .android .volley .VolleyError ;
68import com .wordpress .rest .RestRequest ;
79
@@ -322,13 +324,12 @@ private void handleFollowedBlogsResponse(final JSONObject jsonObject) {
322324 public void run () {
323325 ReaderBlogList serverBlogs = ReaderBlogList .fromJson (jsonObject );
324326 ReaderBlogList localBlogs = ReaderBlogTable .getFollowedBlogs ();
325- // Remove duplicates from the server blogs list only if local and remote lists don't match.
326- if (serverBlogs .size () != localBlogs .size ()) {
327- // This is required because under rare circumstances the server can return duplicates.
328- // We could have modified the function isSameList to eliminate the length check,
329- // but it's better to keep it separate since we aim to remove this check as soon as possible.
330- removeDuplicateFromServerResponse (serverBlogs );
331- }
327+
328+ // This is required because under rare circumstances the server can return duplicates.
329+ // We could have modified the function isSameList to eliminate the length check,
330+ // but it's better to keep it separate since we aim to remove this check as soon as possible.
331+ removeDuplicateBlogs (serverBlogs );
332+
332333 if (!localBlogs .isSameList (serverBlogs )) {
333334 // always update the list of followed blogs if there are *any* changes between
334335 // server and local (including subscription count, description, etc.)
@@ -345,20 +346,26 @@ public void run() {
345346
346347 taskCompleted (UpdateTask .FOLLOWED_BLOGS );
347348 }
348- /* This method remove duplicate ReaderBlog from list. */
349- private void removeDuplicateFromServerResponse (ReaderBlogList serverBlogs ) {
350- for (int i = 0 ; i < serverBlogs .size (); i ++) {
351- ReaderBlog outer = serverBlogs .get (i );
352- for (int j = serverBlogs .size () - 1 ; j > i ; j --) {
353- ReaderBlog inner = serverBlogs .get (j );
354- if (outer .blogId == inner .blogId ) {
355- // If the 'id' property is the same,
356- // remove the later object to avoid duplicates
357- serverBlogs .remove (j );
358- }
359- }
349+ }.start ();
350+ }
351+
352+ /**
353+ * Remove duplicates from the input list.
354+ * Note that this method modifies the input list.
355+ *
356+ * @param blogList The list of blogs to remove duplicates from.
357+ */
358+ private void removeDuplicateBlogs (@ NonNull ReaderBlogList blogList ) {
359+ for (int i = 0 ; i < blogList .size (); i ++) {
360+ ReaderBlog outer = blogList .get (i );
361+ for (int j = blogList .size () - 1 ; j > i ; j --) {
362+ ReaderBlog inner = blogList .get (j );
363+ if (outer .blogId == inner .blogId ) {
364+ // If the 'id' property is the same,
365+ // remove the later object to avoid duplicates
366+ blogList .remove (j );
360367 }
361368 }
362- }. start ();
369+ }
363370 }
364371}
0 commit comments