@@ -81,7 +81,7 @@ public void performTasks(EnumSet<UpdateTask> tasks, Object companion) {
8181 fetchInterestTags ();
8282 }
8383 if (tasks .contains (UpdateTask .FOLLOWED_BLOGS )) {
84- updateFollowedBlogs ();
84+ updateFollowedBlogs (1 , new ReaderBlogList () );
8585 }
8686 }
8787
@@ -297,56 +297,69 @@ public void run() {
297297 /***
298298 * request the list of blogs the current user is following
299299 */
300- private void updateFollowedBlogs () {
300+ private void updateFollowedBlogs (final int page , final ReaderBlogList serverBlogs ) {
301301 RestRequest .Listener listener = new RestRequest .Listener () {
302302 @ Override
303303 public void onResponse (JSONObject jsonObject ) {
304- handleFollowedBlogsResponse (jsonObject );
304+ handleFollowedBlogsResponse (serverBlogs , jsonObject );
305305 }
306306 };
307307 RestRequest .ErrorListener errorListener = new RestRequest .ErrorListener () {
308308 @ Override
309309 public void onErrorResponse (VolleyError volleyError ) {
310310 AppLog .e (AppLog .T .READER , volleyError );
311+ serverBlogs .clear ();
311312 taskCompleted (UpdateTask .FOLLOWED_BLOGS );
312313 }
313314 };
314315
315- AppLog .d (AppLog .T .READER , "reader service > updating followed blogs" );
316+ AppLog .d (AppLog .T .READER , "reader service > updating followed blogs. Page requested: " + page );
316317 // request using ?meta=site,feed to get extra info
317- WordPress .getRestClientUtilsV1_2 ().get ("read/following/mine?meta=site%2Cfeed" , listener , errorListener );
318+ WordPress .getRestClientUtilsV1_2 ()
319+ .get ("read/following/mine?number=100&page=" + page + "&meta=site%2Cfeed" , listener , errorListener );
318320 }
319321
320- private void handleFollowedBlogsResponse (final JSONObject jsonObject ) {
322+ private void handleFollowedBlogsResponse (final ReaderBlogList serverBlogs , final JSONObject jsonObject ) {
321323 new Thread () {
322324 @ Override
323325 public void run () {
324- ReaderBlogList serverBlogs = ReaderBlogList .fromJson (jsonObject );
325- ReaderBlogList localBlogs = ReaderBlogTable .getFollowedBlogs ();
326+ ReaderBlogList currentPageServerResponse = ReaderBlogList .fromJson (jsonObject );
326327
327328 // This is required because under rare circumstances the server can return duplicates.
328329 // We could have modified the function isSameList to eliminate the length check,
329330 // but it's better to keep it separate since we aim to remove this check as soon as possible.
330- removeDuplicateBlogs (serverBlogs );
331+ removeDuplicateBlogs (currentPageServerResponse );
331332
332333 boolean sitesSubscribedChanged = false ;
333334 final int totalSites = jsonObject == null ? 0 : jsonObject .optInt ("total_subscriptions" , 0 );
334-
335- if (!localBlogs .isSameList (serverBlogs )) {
336- // always update the list of followed blogs if there are *any* changes between
337- // server and local (including subscription count, description, etc.)
338- ReaderBlogTable .setFollowedBlogs (serverBlogs );
339- // ...but only update the follow status and alert that followed blogs have
340- // changed if the server list doesn't have the same blogs as the local list
341- // (ie: a blog has been followed/unfollowed since local was last updated)
342- if (!localBlogs .hasSameBlogs (serverBlogs )) {
343- ReaderPostTable .updateFollowedStatus ();
344- AppLog .i (AppLog .T .READER , "reader blogs service > followed blogs changed" );
345- sitesSubscribedChanged = true ;
335+ final int page = jsonObject == null ? 1 : jsonObject .optInt ("page" , 1 );
336+ final int numberOfSitesReturned = jsonObject == null ? 0 : jsonObject .optInt ("number" , 0 );
337+ serverBlogs .addAll (currentPageServerResponse );
338+ if (numberOfSitesReturned > 90 ) {
339+ // 90 appears to be a magic number here, and in a way, it is.
340+ // The server doesn't always return the exact number of requested sites, likely due to deleted or
341+ // suspended sites. In the worst-case scenario, we might make an additional request that returns 0.
342+ updateFollowedBlogs (page + 1 , serverBlogs );
343+ } else {
344+ ReaderBlogList localBlogs = ReaderBlogTable .getFollowedBlogs ();
345+ if (!localBlogs .isSameList (serverBlogs )) {
346+ // always update the list of followed blogs if there are *any* changes between
347+ // server and local (including subscription count, description, etc.)
348+ ReaderBlogTable .setFollowedBlogs (serverBlogs );
349+ // ...but only update the follow status and alert that followed blogs have
350+ // changed if the server list doesn't have the same blogs as the local list
351+ // (ie: a blog has been followed/unfollowed since local was last updated)
352+ if (!localBlogs .hasSameBlogs (serverBlogs )) {
353+ ReaderPostTable .updateFollowedStatus ();
354+ AppLog .i (AppLog .T .READER , "reader blogs service > followed blogs changed: "
355+ + totalSites );
356+ sitesSubscribedChanged = true ;
357+ }
346358 }
359+ EventBus .getDefault ().post (new FollowedBlogsFetched (totalSites , sitesSubscribedChanged ));
360+ serverBlogs .clear ();
361+ taskCompleted (UpdateTask .FOLLOWED_BLOGS );
347362 }
348- EventBus .getDefault ().post (new FollowedBlogsFetched (totalSites , sitesSubscribedChanged ));
349- taskCompleted (UpdateTask .FOLLOWED_BLOGS );
350363 }
351364 }.start ();
352365 }
0 commit comments