From 3acf725e639fe61e253dd8f62413ceeb7f487125 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Thu, 7 Mar 2024 12:58:58 +0100 Subject: [PATCH 1/2] Add pagination to read/following/mine --- .../services/update/ReaderUpdateLogic.java | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 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 7f52800a679c..83d8cdcd5ff7 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 @@ -81,7 +81,7 @@ public void performTasks(EnumSet tasks, Object companion) { fetchInterestTags(); } if (tasks.contains(UpdateTask.FOLLOWED_BLOGS)) { - updateFollowedBlogs(); + updateFollowedBlogs(1, new ReaderBlogList()); } } @@ -297,56 +297,68 @@ public void run() { /*** * request the list of blogs the current user is following */ - private void updateFollowedBlogs() { + private void updateFollowedBlogs(final int page, final ReaderBlogList serverBlogs) { RestRequest.Listener listener = new RestRequest.Listener() { @Override public void onResponse(JSONObject jsonObject) { - handleFollowedBlogsResponse(jsonObject); + handleFollowedBlogsResponse(serverBlogs, jsonObject); } }; RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { AppLog.e(AppLog.T.READER, volleyError); + serverBlogs.clear(); taskCompleted(UpdateTask.FOLLOWED_BLOGS); } }; AppLog.d(AppLog.T.READER, "reader service > updating followed blogs"); // request using ?meta=site,feed to get extra info - WordPress.getRestClientUtilsV1_2().get("read/following/mine?meta=site%2Cfeed", listener, errorListener); + WordPress.getRestClientUtilsV1_2() + .get("read/following/mine?number=100&page=" + page + "&meta=site%2Cfeed", listener, errorListener); } - private void handleFollowedBlogsResponse(final JSONObject jsonObject) { + private void handleFollowedBlogsResponse(final ReaderBlogList serverBlogs, final JSONObject jsonObject) { new Thread() { @Override public void run() { - ReaderBlogList serverBlogs = ReaderBlogList.fromJson(jsonObject); - ReaderBlogList localBlogs = ReaderBlogTable.getFollowedBlogs(); + ReaderBlogList currentPageServerResponse = ReaderBlogList.fromJson(jsonObject); // 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); + removeDuplicateBlogs(currentPageServerResponse); boolean sitesSubscribedChanged = false; final int totalSites = jsonObject == null ? 0 : jsonObject.optInt("total_subscriptions", 0); - - 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.) - ReaderBlogTable.setFollowedBlogs(serverBlogs); - // ...but only update the follow status and alert that followed blogs have - // changed if the server list doesn't have the same blogs as the local list - // (ie: a blog has been followed/unfollowed since local was last updated) - if (!localBlogs.hasSameBlogs(serverBlogs)) { - ReaderPostTable.updateFollowedStatus(); - AppLog.i(AppLog.T.READER, "reader blogs service > followed blogs changed"); - sitesSubscribedChanged = true; + final int page = jsonObject == null ? 1 : jsonObject.optInt("page", 1); + final int numberOfSitesReturned = jsonObject == null ? 0 : jsonObject.optInt("number", 0); + serverBlogs.addAll(currentPageServerResponse); + if (numberOfSitesReturned > 90) { + // 90 appears to be a magic number here, and in a way, it is. + // The server doesn't always return the exact number of requested sites, likely due to deleted or + // suspended sites. In the worst-case scenario, we might make an additional request that returns 0. + updateFollowedBlogs(page + 1, serverBlogs); + } else { + ReaderBlogList localBlogs = ReaderBlogTable.getFollowedBlogs(); + 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.) + ReaderBlogTable.setFollowedBlogs(serverBlogs); + // ...but only update the follow status and alert that followed blogs have + // changed if the server list doesn't have the same blogs as the local list + // (ie: a blog has been followed/unfollowed since local was last updated) + if (!localBlogs.hasSameBlogs(serverBlogs)) { + ReaderPostTable.updateFollowedStatus(); + AppLog.i(AppLog.T.READER, "reader blogs service > followed blogs changed"); + sitesSubscribedChanged = true; + } } + EventBus.getDefault().post(new FollowedBlogsFetched(totalSites, sitesSubscribedChanged)); + serverBlogs.clear(); + taskCompleted(UpdateTask.FOLLOWED_BLOGS); } - EventBus.getDefault().post(new FollowedBlogsFetched(totalSites, sitesSubscribedChanged)); - taskCompleted(UpdateTask.FOLLOWED_BLOGS); } }.start(); } From 3bdb7042946fa21210ea7d6079bbae501559a85a Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Thu, 7 Mar 2024 13:20:22 +0100 Subject: [PATCH 2/2] Log the page requested. --- .../android/ui/reader/services/update/ReaderUpdateLogic.java | 5 +++-- 1 file changed, 3 insertions(+), 2 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 83d8cdcd5ff7..151bdebd5101 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 @@ -313,7 +313,7 @@ public void onErrorResponse(VolleyError volleyError) { } }; - AppLog.d(AppLog.T.READER, "reader service > updating followed blogs"); + AppLog.d(AppLog.T.READER, "reader service > updating followed blogs. Page requested: " + page); // request using ?meta=site,feed to get extra info WordPress.getRestClientUtilsV1_2() .get("read/following/mine?number=100&page=" + page + "&meta=site%2Cfeed", listener, errorListener); @@ -351,7 +351,8 @@ public void run() { // (ie: a blog has been followed/unfollowed since local was last updated) if (!localBlogs.hasSameBlogs(serverBlogs)) { ReaderPostTable.updateFollowedStatus(); - AppLog.i(AppLog.T.READER, "reader blogs service > followed blogs changed"); + AppLog.i(AppLog.T.READER, "reader blogs service > followed blogs changed: " + + totalSites); sitesSubscribedChanged = true; } }