Browse Source

fix repeating feed, more polishing

renovate/org.robolectric-robolectric-4.x
Austin Huang 4 years ago
parent
commit
172ca2bdc8
No known key found for this signature in database GPG Key ID: 84C23AA04587A91F
  1. 6
      app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java
  2. 29
      app/src/main/java/awais/instagrabber/webservices/FeedService.java
  3. 13
      app/src/main/res/layout/layout_profile_details.xml

6
app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java

@ -1,5 +1,6 @@
package awais.instagrabber.asyncs; package awais.instagrabber.asyncs;
import android.os.Handler;
import android.util.Log; import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
@ -20,10 +21,13 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService {
private static final String TAG = "FeedPostFetchService"; private static final String TAG = "FeedPostFetchService";
private final FeedService feedService; private final FeedService feedService;
private String nextCursor; private String nextCursor;
private final Handler handler;
private boolean hasNextPage; private boolean hasNextPage;
private static final int DELAY_MILLIS = 500;
public FeedPostFetchService() { public FeedPostFetchService() {
feedService = FeedService.getInstance(); feedService = FeedService.getInstance();
handler = new Handler();
} }
@Override @Override
@ -45,7 +49,9 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService {
feedModels.addAll(result.getFeedModels()); feedModels.addAll(result.getFeedModels());
if (fetchListener != null) { if (fetchListener != null) {
if (feedModels.size() < 15 && hasNextPage) { if (feedModels.size() < 15 && hasNextPage) {
handler.postDelayed(() -> {
feedService.fetch(csrfToken, nextCursor, this); feedService.fetch(csrfToken, nextCursor, this);
}, DELAY_MILLIS);
} }
else { else {
fetchListener.onResult(feedModels); fetchListener.onResult(feedModels);

29
app/src/main/java/awais/instagrabber/webservices/FeedService.java

@ -118,20 +118,43 @@ public class FeedService extends BaseService {
throws JSONException { throws JSONException {
final JSONObject root = new JSONObject(body); final JSONObject root = new JSONObject(body);
final boolean moreAvailable = root.optBoolean("more_available"); 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 JSONArray feedItems = root.optJSONArray("items");
final List<FeedModel> feedModels = new ArrayList<>(); final List<FeedModel> feedModels = new ArrayList<>();
for (int i = 0; i < feedItems.length(); ++i) { for (int i = 0; i < feedItems.length(); ++i) {
final JSONObject itemJson = feedItems.optJSONObject(i); final JSONObject itemJson = feedItems.optJSONObject(i);
if (itemJson == null || itemJson.has("injected")
) {
if (itemJson == null || itemJson.has("injected")) {
continue; continue;
} }
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); final FeedModel feedModel = ResponseBodyUtils.parseItem(itemJson);
if (feedModel != null) { if (feedModel != null) {
feedModels.add(feedModel); feedModels.add(feedModel);
} }
} }
}
return new PostsFetchResponse(feedModels, moreAvailable, nextMaxId); return new PostsFetchResponse(feedModels, moreAvailable, nextMaxId);
} }
} }

13
app/src/main/res/layout/layout_profile_details.xml

@ -37,7 +37,7 @@
tools:visibility="visible" /> tools:visibility="visible" />
<com.google.android.material.chip.Chip <com.google.android.material.chip.Chip
android:id="@+id/mainStatus"
android:id="@+id/mainFollowing"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="@dimen/profile_chip_size" android:layout_height="@dimen/profile_chip_size"
android:layout_marginStart="4dp" android:layout_marginStart="4dp"
@ -47,8 +47,8 @@
app:chipBackgroundColor="@null" app:chipBackgroundColor="@null"
app:layout_constraintBottom_toTopOf="@id/mainFollowers" app:layout_constraintBottom_toTopOf="@id/mainFollowers"
app:layout_constraintStart_toEndOf="@id/mainPostCount" 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" /> tools:visibility="visible" />
<com.google.android.material.chip.Chip <com.google.android.material.chip.Chip
@ -68,7 +68,7 @@
tools:visibility="visible" /> tools:visibility="visible" />
<com.google.android.material.chip.Chip <com.google.android.material.chip.Chip
android:id="@+id/mainFollowing"
android:id="@+id/mainStatus"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="@dimen/profile_chip_size" android:layout_height="@dimen/profile_chip_size"
android:layout_marginStart="4dp" android:layout_marginStart="4dp"
@ -79,15 +79,14 @@
app:layout_constraintBottom_toTopOf="@id/fav_chip" app:layout_constraintBottom_toTopOf="@id/fav_chip"
app:layout_constraintStart_toEndOf="@id/mainFollowers" app:layout_constraintStart_toEndOf="@id/mainFollowers"
app:layout_constraintTop_toBottomOf="@id/mainPostCount" 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" /> tools:visibility="visible" />
<com.google.android.material.chip.Chip <com.google.android.material.chip.Chip
android:id="@+id/fav_chip" android:id="@+id/fav_chip"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="@dimen/profile_chip_size" android:layout_height="@dimen/profile_chip_size"
android:layout_marginStart="8dp"
android:layout_marginStart="4dp"
android:text="@string/add_to_favorites" android:text="@string/add_to_favorites"
android:visibility="gone" android:visibility="gone"
app:chipBackgroundColor="@null" app:chipBackgroundColor="@null"

Loading…
Cancel
Save