Skip to content

Commit

Permalink
Merge pull request #20204 from wordpress-mobile/issue-reader-remove-b…
Browse files Browse the repository at this point in the history
…logs-duplicates

[Reader] Remove duplicates from the server blogs list only if local and remote  lists don't match
  • Loading branch information
daniloercoli authored Feb 19, 2024
2 parents 621ccfb + 08588e4 commit 0d4a330
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.content.Context;

import androidx.annotation.NonNull;

import com.android.volley.VolleyError;
import com.wordpress.rest.RestRequest;

Expand All @@ -14,6 +16,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;
Expand Down Expand Up @@ -322,6 +325,11 @@ public void run() {
ReaderBlogList serverBlogs = ReaderBlogList.fromJson(jsonObject);
ReaderBlogList localBlogs = ReaderBlogTable.getFollowedBlogs();

// 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.)
Expand All @@ -340,4 +348,24 @@ public void run() {
}
}.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);
}
}
}
}
}

0 comments on commit 0d4a330

Please sign in to comment.