fix repeating feed, more polishing

This commit is contained in:
Austin Huang 2020-12-22 15:25:47 -05:00
parent 9ca9cca33d
commit 172ca2bdc8
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
3 changed files with 42 additions and 14 deletions

View File

@ -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);

View File

@ -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<FeedModel> 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);

View File

@ -37,7 +37,7 @@
tools:visibility="visible" />
<com.google.android.material.chip.Chip
android:id="@+id/mainStatus"
android:id="@+id/mainFollowing"
android:layout_width="wrap_content"
android:layout_height="@dimen/profile_chip_size"
android:layout_marginStart="4dp"
@ -47,8 +47,8 @@
app:chipBackgroundColor="@null"
app:layout_constraintBottom_toTopOf="@id/mainFollowers"
app:layout_constraintStart_toEndOf="@id/mainPostCount"
app:layout_constraintTop_toTopOf="@id/mainPostCount"
tools:text="omg what do u expect"
app:rippleColor="@color/grey_400"
tools:text="10 Following"
tools:visibility="visible" />
<com.google.android.material.chip.Chip
@ -68,7 +68,7 @@
tools:visibility="visible" />
<com.google.android.material.chip.Chip
android:id="@+id/mainFollowing"
android:id="@+id/mainStatus"
android:layout_width="wrap_content"
android:layout_height="@dimen/profile_chip_size"
android:layout_marginStart="4dp"
@ -79,15 +79,14 @@
app:layout_constraintBottom_toTopOf="@id/fav_chip"
app:layout_constraintStart_toEndOf="@id/mainFollowers"
app:layout_constraintTop_toBottomOf="@id/mainPostCount"
app:rippleColor="@color/grey_400"
tools:text="10 Following"
tools:text="omg what do u expect"
tools:visibility="visible" />
<com.google.android.material.chip.Chip
android:id="@+id/fav_chip"
android:layout_width="wrap_content"
android:layout_height="@dimen/profile_chip_size"
android:layout_marginStart="8dp"
android:layout_marginStart="4dp"
android:text="@string/add_to_favorites"
android:visibility="gone"
app:chipBackgroundColor="@null"