From 62591c6a3387a8242726f57a3917a81e14f6f5b7 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Mon, 19 Feb 2024 12:21:33 +0100 Subject: [PATCH 1/2] Remove duplicates from the server blogs list if local and remote doesn't match. Remove duplicates from the server blogs list if local and remote doesn't match. This is required because under obscure circumstances the server can return duplicates. We could have modified the function isSameList to eliminate the length check, but it's better to keep it separate since we aim to remove this check as soon as possible. --- .../services/update/ReaderUpdateLogic.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java index a9af93b20e15..d7d6febd20dd 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java @@ -14,6 +14,7 @@ import org.wordpress.android.datasets.ReaderPostTable; import org.wordpress.android.datasets.ReaderTagTable; import org.wordpress.android.fluxc.store.AccountStore; +import org.wordpress.android.models.ReaderBlog; import org.wordpress.android.models.ReaderBlogList; import org.wordpress.android.models.ReaderTag; import org.wordpress.android.models.ReaderTagList; @@ -321,7 +322,13 @@ private void handleFollowedBlogsResponse(final JSONObject jsonObject) { public void run() { ReaderBlogList serverBlogs = ReaderBlogList.fromJson(jsonObject); ReaderBlogList localBlogs = ReaderBlogTable.getFollowedBlogs(); - + // Remove duplicates from the server blogs list only if local and remote lists don't match. + if (serverBlogs.size() != localBlogs.size()) { + // This is required because under rare circumstances the server can return duplicates. + // We could have modified the function isSameList to eliminate the length check, + // but it's better to keep it separate since we aim to remove this check as soon as possible. + removeDuplicateFromServerResponse(serverBlogs); + } if (!localBlogs.isSameList(serverBlogs)) { // always update the list of followed blogs if there are *any* changes between // server and local (including subscription count, description, etc.) @@ -338,6 +345,20 @@ public void run() { taskCompleted(UpdateTask.FOLLOWED_BLOGS); } + /* This method remove duplicate ReaderBlog from list. */ + private void removeDuplicateFromServerResponse(ReaderBlogList serverBlogs) { + for (int i = 0; i < serverBlogs.size(); i++) { + ReaderBlog outer = serverBlogs.get(i); + for (int j = serverBlogs.size() - 1; j > i; j--) { + ReaderBlog inner = serverBlogs.get(j); + if (outer.blogId == inner.blogId) { + // If the 'id' property is the same, + // remove the later object to avoid duplicates + serverBlogs.remove(j); + } + } + } + } }.start(); } } From 08588e4554f855249f38d3b85dd7c5687d467e7c Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Mon, 19 Feb 2024 16:07:15 +0100 Subject: [PATCH 2/2] Rename 'removeDuplicateBlogs' to something more generic and move it outside of the Thread object --- .../services/update/ReaderUpdateLogic.java | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java index d7d6febd20dd..cde09194380f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java @@ -2,6 +2,8 @@ import android.content.Context; +import androidx.annotation.NonNull; + import com.android.volley.VolleyError; import com.wordpress.rest.RestRequest; @@ -322,13 +324,12 @@ private void handleFollowedBlogsResponse(final JSONObject jsonObject) { public void run() { ReaderBlogList serverBlogs = ReaderBlogList.fromJson(jsonObject); ReaderBlogList localBlogs = ReaderBlogTable.getFollowedBlogs(); - // Remove duplicates from the server blogs list only if local and remote lists don't match. - if (serverBlogs.size() != localBlogs.size()) { - // This is required because under rare circumstances the server can return duplicates. - // We could have modified the function isSameList to eliminate the length check, - // but it's better to keep it separate since we aim to remove this check as soon as possible. - removeDuplicateFromServerResponse(serverBlogs); - } + + // This is required because under rare circumstances the server can return duplicates. + // We could have modified the function isSameList to eliminate the length check, + // but it's better to keep it separate since we aim to remove this check as soon as possible. + removeDuplicateBlogs(serverBlogs); + if (!localBlogs.isSameList(serverBlogs)) { // always update the list of followed blogs if there are *any* changes between // server and local (including subscription count, description, etc.) @@ -345,20 +346,26 @@ public void run() { taskCompleted(UpdateTask.FOLLOWED_BLOGS); } - /* This method remove duplicate ReaderBlog from list. */ - private void removeDuplicateFromServerResponse(ReaderBlogList serverBlogs) { - for (int i = 0; i < serverBlogs.size(); i++) { - ReaderBlog outer = serverBlogs.get(i); - for (int j = serverBlogs.size() - 1; j > i; j--) { - ReaderBlog inner = serverBlogs.get(j); - if (outer.blogId == inner.blogId) { - // If the 'id' property is the same, - // remove the later object to avoid duplicates - serverBlogs.remove(j); - } - } + }.start(); + } + + /** + * Remove duplicates from the input list. + * Note that this method modifies the input list. + * + * @param blogList The list of blogs to remove duplicates from. + */ + private void removeDuplicateBlogs(@NonNull ReaderBlogList blogList) { + for (int i = 0; i < blogList.size(); i++) { + ReaderBlog outer = blogList.get(i); + for (int j = blogList.size() - 1; j > i; j--) { + ReaderBlog inner = blogList.get(j); + if (outer.blogId == inner.blogId) { + // If the 'id' property is the same, + // remove the later object to avoid duplicates + blogList.remove(j); } } - }.start(); + } } }