From 172ca2bdc85a1fa6e87ee5c593def8fbc5e6534c Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 22 Dec 2020 15:25:47 -0500 Subject: [PATCH] fix repeating feed, more polishing --- .../asyncs/FeedPostFetchService.java | 8 ++++- .../instagrabber/webservices/FeedService.java | 35 +++++++++++++++---- .../res/layout/layout_profile_details.xml | 13 ++++--- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java index 65cf3e12..1451782e 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java +++ b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java @@ -1,5 +1,6 @@ package awais.instagrabber.asyncs; +import android.os.Handler; import android.util.Log; import java.util.ArrayList; @@ -20,10 +21,13 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService { private static final String TAG = "FeedPostFetchService"; private final FeedService feedService; private String nextCursor; + private final Handler handler; private boolean hasNextPage; + private static final int DELAY_MILLIS = 500; public FeedPostFetchService() { feedService = FeedService.getInstance(); + handler = new Handler(); } @Override @@ -45,7 +49,9 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService { feedModels.addAll(result.getFeedModels()); if (fetchListener != null) { if (feedModels.size() < 15 && hasNextPage) { - feedService.fetch(csrfToken, nextCursor, this); + handler.postDelayed(() -> { + feedService.fetch(csrfToken, nextCursor, this); + }, DELAY_MILLIS); } else { fetchListener.onResult(feedModels); diff --git a/app/src/main/java/awais/instagrabber/webservices/FeedService.java b/app/src/main/java/awais/instagrabber/webservices/FeedService.java index fd491293..584d5e14 100644 --- a/app/src/main/java/awais/instagrabber/webservices/FeedService.java +++ b/app/src/main/java/awais/instagrabber/webservices/FeedService.java @@ -118,18 +118,41 @@ public class FeedService extends BaseService { throws JSONException { final JSONObject root = new JSONObject(body); final boolean moreAvailable = root.optBoolean("more_available"); - final String nextMaxId = root.optString("next_max_id"); + String nextMaxId = root.optString("next_max_id"); + final boolean needNewMaxId = nextMaxId.equals("feed_recs_head_load"); final JSONArray feedItems = root.optJSONArray("items"); final List feedModels = new ArrayList<>(); for (int i = 0; i < feedItems.length(); ++i) { final JSONObject itemJson = feedItems.optJSONObject(i); - if (itemJson == null || itemJson.has("injected") - ) { + if (itemJson == null || itemJson.has("injected")) { continue; } - final FeedModel feedModel = ResponseBodyUtils.parseItem(itemJson); - if (feedModel != null) { - feedModels.add(feedModel); + else if (itemJson.has("end_of_feed_demarcator") && needNewMaxId) { + final JSONArray groups = itemJson.getJSONObject("end_of_feed_demarcator").getJSONObject("group_set").getJSONArray("groups"); + for (int j = 0; j < groups.length(); ++j) { + final JSONObject groupJson = groups.optJSONObject(j); + if (groupJson.getString("id").equals("past_posts")) { + nextMaxId = groupJson.optString("next_max_id"); + final JSONArray miniFeedItems = groupJson.optJSONArray("feed_items"); + for (int k = 0; k < miniFeedItems.length(); ++k) { + final JSONObject miniItemJson = miniFeedItems.optJSONObject(k); + if (miniItemJson == null || miniItemJson.has("injected")) { + continue; + } + final FeedModel feedModel = ResponseBodyUtils.parseItem(miniItemJson); + if (feedModel != null) { + feedModels.add(feedModel); + } + } + } + else continue; + } + } + else { + final FeedModel feedModel = ResponseBodyUtils.parseItem(itemJson); + if (feedModel != null) { + feedModels.add(feedModel); + } } } return new PostsFetchResponse(feedModels, moreAvailable, nextMaxId); diff --git a/app/src/main/res/layout/layout_profile_details.xml b/app/src/main/res/layout/layout_profile_details.xml index 23017654..b7138f82 100644 --- a/app/src/main/res/layout/layout_profile_details.xml +++ b/app/src/main/res/layout/layout_profile_details.xml @@ -37,7 +37,7 @@ tools:visibility="visible" />