Skip to content

Commit 0d4a330

Browse files
authored
Merge pull request #20204 from wordpress-mobile/issue-reader-remove-blogs-duplicates
[Reader] Remove duplicates from the server blogs list only if local and remote lists don't match
2 parents 621ccfb + 08588e4 commit 0d4a330

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.content.Context;
44

5+
import androidx.annotation.NonNull;
6+
57
import com.android.volley.VolleyError;
68
import com.wordpress.rest.RestRequest;
79

@@ -14,6 +16,7 @@
1416
import org.wordpress.android.datasets.ReaderPostTable;
1517
import org.wordpress.android.datasets.ReaderTagTable;
1618
import org.wordpress.android.fluxc.store.AccountStore;
19+
import org.wordpress.android.models.ReaderBlog;
1720
import org.wordpress.android.models.ReaderBlogList;
1821
import org.wordpress.android.models.ReaderTag;
1922
import org.wordpress.android.models.ReaderTagList;
@@ -322,6 +325,11 @@ public void run() {
322325
ReaderBlogList serverBlogs = ReaderBlogList.fromJson(jsonObject);
323326
ReaderBlogList localBlogs = ReaderBlogTable.getFollowedBlogs();
324327

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+
325333
if (!localBlogs.isSameList(serverBlogs)) {
326334
// always update the list of followed blogs if there are *any* changes between
327335
// server and local (including subscription count, description, etc.)
@@ -340,4 +348,24 @@ public void run() {
340348
}
341349
}.start();
342350
}
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);
367+
}
368+
}
369+
}
370+
}
343371
}

0 commit comments

Comments
 (0)