diff options
author | Sergei Kozelko <KozelkoS@yandex.ru> | 2022-11-11 05:32:58 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-11 05:32:58 +0000 |
commit | 0f1c4d1e11a3b072d2bb1a60c52f858c79c6e0ae (patch) | |
tree | 81a22bceb512583f0411453a97839b1a356a9aeb /app/src/main/java/ml/docilealligator/infinityforreddit | |
parent | 8326e6c4a20eb84ebfbbd23e594aec2a8c8a56e3 (diff) | |
download | infinity-for-reddit-0f1c4d1e11a3b072d2bb1a60c52f858c79c6e0ae.tar infinity-for-reddit-0f1c4d1e11a3b072d2bb1a60c52f858c79c6e0ae.tar.gz infinity-for-reddit-0f1c4d1e11a3b072d2bb1a60c52f858c79c6e0ae.tar.bz2 infinity-for-reddit-0f1c4d1e11a3b072d2bb1a60c52f858c79c6e0ae.tar.lz infinity-for-reddit-0f1c4d1e11a3b072d2bb1a60c52f858c79c6e0ae.tar.xz infinity-for-reddit-0f1c4d1e11a3b072d2bb1a60c52f858c79c6e0ae.tar.zst infinity-for-reddit-0f1c4d1e11a3b072d2bb1a60c52f858c79c6e0ae.zip |
Switch loading more comments to /morechildren endpoint (#1186)
* Load more comments from /morechildren endpoint
Previous implementation requested comments from /api/info which returned the comments themselves but did not include any information about their children. Also /api/info does not allow to specify sort type. On the other hand /morechildren supports sort type and it will be added in a later commit.
I am not proud of this implementation, but I had to fight with both Reddit api response and existing code. The problem with api response is that it is a flat list of comments, not a tree structure. So the tree has to be rebuilt on our end. And the problem with existing code is that it merges "more children" node into its parent but then adds a placeholder anyways.
The code relies on the fact that parent comment will be located before any of its children in the response. The code sequentially processes comments, tries to find their parents and either adds them to the tree or puts in a "top-level" array which will be handled by outside code.
One possible problem is the removal of `depth` argument from parsing as I couldn't find a way to fit it in the new logic. However I also didn't experience any problems with it during my testing and the response seems to always contain depth key. Moreover current depth handling logic in ParseComment#parseCommentRecursion is broken because it does not increment depth when making a recursive call.
* Store moreChildren ids instead of fullnames
/morechildren endpoint accepts ids instead of fullnames so there is no point in converting ids to fullnames and back
* Send all comment ids to Reddit so it can select what to display itself
Sending all ids allows Reddit to sort them properly. Since the number of comments can be very bing, it requires using POST request instead of GET.
This commit changes the meaning of Comment#moreChildrenIds field, now it stores only ids of comments that are not yet loaded. This simplifies the code and removes the need for Comment#moreChildrenStartingIndex
* Fetch more comments with current sort type
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
6 files changed, 167 insertions, 83 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index 1271cfc2..29c3f828 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -607,18 +607,17 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi ((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.loading); Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; + String sortType = mCommentRecyclerViewAdapterCallback.getSortType(); FetchComment.fetchMoreComment(mExecutor, new Handler(), retrofit, mAccessToken, - parentComment.getMoreChildrenFullnames(), - parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1, - mExpandChildren, new FetchComment.FetchMoreCommentListener() { + parentComment.getMoreChildrenIds(), + mExpandChildren, mPost.getFullName(), sortType, new FetchComment.FetchMoreCommentListener() { @Override - public void onFetchMoreCommentSuccess(ArrayList<Comment> expandedComments, - int childrenStartingIndex) { + public void onFetchMoreCommentSuccess(ArrayList<Comment> expandedComments, ArrayList<String> moreChildrenIds) { if (mVisibleComments.size() > parentPosition && parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) { if (mVisibleComments.get(parentPosition).isExpanded()) { - if (mVisibleComments.get(parentPosition).getChildren().size() > childrenStartingIndex) { - mVisibleComments.get(parentPosition).setMoreChildrenStartingIndex(childrenStartingIndex); + if (!moreChildrenIds.isEmpty()) { + mVisibleComments.get(parentPosition).setMoreChildrenIds(moreChildrenIds); mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1) .setLoadingMoreChildren(false); mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1) @@ -647,7 +646,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi } else { mVisibleComments.get(parentPosition).getChildren() .remove(mVisibleComments.get(parentPosition).getChildren().size() - 1); - mVisibleComments.get(parentPosition).removeMoreChildrenFullnames(); + mVisibleComments.get(parentPosition).removeMoreChildrenIds(); int placeholderPosition = commentPosition; if (mVisibleComments.get(commentPosition).getFullName().equals(parentComment.getFullName())) { @@ -674,10 +673,10 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi } } } else { - if (mVisibleComments.get(parentPosition).hasReply() && mVisibleComments.get(parentPosition).getChildren().size() <= childrenStartingIndex) { + if (mVisibleComments.get(parentPosition).hasReply() && moreChildrenIds.isEmpty()) { mVisibleComments.get(parentPosition).getChildren() .remove(mVisibleComments.get(parentPosition).getChildren().size() - 1); - mVisibleComments.get(parentPosition).removeMoreChildrenFullnames(); + mVisibleComments.get(parentPosition).removeMoreChildrenIds(); } } @@ -1157,6 +1156,8 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi void retryFetchingComments(); void retryFetchingMoreComments(); + + String getSortType(); } public class CommentViewHolder extends RecyclerView.ViewHolder { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java index 5638863a..705e8490 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -10,6 +10,7 @@ import retrofit2.Call; import retrofit2.Response; import retrofit2.http.Body; import retrofit2.http.DELETE; +import retrofit2.http.Field; import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; @@ -500,4 +501,12 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/site_admin") Call<String> postSiteAdmin(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); + + @FormUrlEncoded + @POST("/api/morechildren.json?raw_json=1&api_type=json") + Call<String> moreChildren(@Field("link_id") String linkId, @Field("children") String children, @Field("sort") String sort); + + @FormUrlEncoded + @POST("/api/morechildren.json?raw_json=1&api_type=json") + Call<String> moreChildrenOauth(@Field("link_id") String linkId, @Field("children") String children, @Field("sort") String sort, @HeaderMap Map<String, String> headers); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java index 15b1b68d..d6a6f173 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/Comment.java @@ -53,8 +53,7 @@ public class Comment implements Parcelable { private boolean isExpanded; private boolean hasExpandedBefore; private ArrayList<Comment> children; - private ArrayList<String> moreChildrenFullnames; - private int moreChildrenStartingIndex; + private ArrayList<String> moreChildrenIds; private int placeholderType; private boolean isLoadingMoreChildren; private boolean loadMoreChildrenFailed; @@ -91,7 +90,6 @@ public class Comment implements Parcelable { this.saved = saved; this.isExpanded = false; this.hasExpandedBefore = false; - moreChildrenStartingIndex = 0; placeholderType = NOT_PLACEHOLDER; } @@ -141,9 +139,8 @@ public class Comment implements Parcelable { hasExpandedBefore = in.readByte() != 0; children = new ArrayList<>(); in.readTypedList(children, Comment.CREATOR); - moreChildrenFullnames = new ArrayList<>(); - in.readStringList(moreChildrenFullnames); - moreChildrenStartingIndex = in.readInt(); + moreChildrenIds = new ArrayList<>(); + in.readStringList(moreChildrenIds); placeholderType = in.readInt(); isLoadingMoreChildren = in.readByte() != 0; loadMoreChildrenFailed = in.readByte() != 0; @@ -345,28 +342,20 @@ public class Comment implements Parcelable { children.add(position, comment); } - public ArrayList<String> getMoreChildrenFullnames() { - return moreChildrenFullnames; + public ArrayList<String> getMoreChildrenIds() { + return moreChildrenIds; } - public void setMoreChildrenFullnames(ArrayList<String> moreChildrenFullnames) { - this.moreChildrenFullnames = moreChildrenFullnames; + public void setMoreChildrenIds(ArrayList<String> moreChildrenIds) { + this.moreChildrenIds = moreChildrenIds; } - public boolean hasMoreChildrenFullnames() { - return moreChildrenFullnames != null; + public boolean hasMoreChildrenIds() { + return moreChildrenIds != null; } - public void removeMoreChildrenFullnames() { - moreChildrenFullnames.clear(); - } - - public int getMoreChildrenStartingIndex() { - return moreChildrenStartingIndex; - } - - public void setMoreChildrenStartingIndex(int moreChildrenStartingIndex) { - this.moreChildrenStartingIndex = moreChildrenStartingIndex; + public void removeMoreChildrenIds() { + moreChildrenIds.clear(); } public int getPlaceholderType() { @@ -423,8 +412,7 @@ public class Comment implements Parcelable { parcel.writeByte((byte) (isExpanded ? 1 : 0)); parcel.writeByte((byte) (hasExpandedBefore ? 1 : 0)); parcel.writeTypedList(children); - parcel.writeStringList(moreChildrenFullnames); - parcel.writeInt(moreChildrenStartingIndex); + parcel.writeStringList(moreChildrenIds); parcel.writeInt(placeholderType); parcel.writeByte((byte) (isLoadingMoreChildren ? 1 : 0)); parcel.writeByte((byte) (loadMoreChildrenFailed ? 1 : 0)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java index 6162865b..a0d62086 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java @@ -46,9 +46,9 @@ public class FetchComment { expandChildren, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList<Comment> expandedComments, - String parentId, ArrayList<String> moreChildrenFullnames) { + String parentId, ArrayList<String> moreChildrenIds) { fetchCommentListener.onFetchCommentSuccess(expandedComments, parentId, - moreChildrenFullnames); + moreChildrenIds); } @Override @@ -70,33 +70,27 @@ public class FetchComment { public static void fetchMoreComment(Executor executor, Handler handler, Retrofit retrofit, @Nullable String accessToken, - ArrayList<String> allChildren, int startingIndex, - int depth, boolean expandChildren, + ArrayList<String> allChildren, + boolean expandChildren, String postFullName, + String sortType, FetchMoreCommentListener fetchMoreCommentListener) { if (allChildren == null) { return; } - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < 100; i++) { - if (allChildren.size() <= startingIndex + i) { - break; - } - stringBuilder.append(allChildren.get(startingIndex + i)).append(","); - } + String childrenIds = String.join(",", allChildren); - if (stringBuilder.length() == 0) { + if (childrenIds.isEmpty()) { return; } - stringBuilder.deleteCharAt(stringBuilder.length() - 1); - RedditAPI api = retrofit.create(RedditAPI.class); Call<String> moreComments; if (accessToken == null) { - moreComments = api.getInfo(stringBuilder.toString()); + moreComments = api.moreChildren(postFullName, childrenIds, sortType); } else { - moreComments = api.getInfoOauth(stringBuilder.toString(), APIUtils.getOAuthHeader(accessToken)); + moreComments = api.moreChildrenOauth(postFullName, childrenIds, + sortType, APIUtils.getOAuthHeader(accessToken)); } moreComments.enqueue(new Callback<String>() { @@ -104,12 +98,11 @@ public class FetchComment { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { ParseComment.parseMoreComment(executor, handler, response.body(), new ArrayList<>(), - depth, expandChildren, new ParseComment.ParseCommentListener() { + expandChildren, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList<Comment> expandedComments, - String parentId, ArrayList<String> moreChildrenFullnames) { - fetchMoreCommentListener.onFetchMoreCommentSuccess(expandedComments, - startingIndex + 100); + String parentId, ArrayList<String> moreChildrenIds) { + fetchMoreCommentListener.onFetchMoreCommentSuccess(expandedComments, moreChildrenIds); } @Override @@ -136,7 +129,7 @@ public class FetchComment { } public interface FetchMoreCommentListener { - void onFetchMoreCommentSuccess(ArrayList<Comment> expandedComments, int childrenStartingIndex); + void onFetchMoreCommentSuccess(ArrayList<Comment> expandedComments, ArrayList<String> moreChildrenIds); void onFetchMoreCommentFailed(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java index 0a341ff3..62b87a1f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java @@ -6,7 +6,9 @@ import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_UPV import android.os.Handler; import android.text.Html; +import android.util.Log; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import org.json.JSONArray; @@ -14,6 +16,7 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.utils.JSONUtils; @@ -31,10 +34,10 @@ public class ParseComment { childrenArray = childrenArray.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); ArrayList<Comment> expandedNewComments = new ArrayList<>(); - ArrayList<String> moreChildrenFullnames = new ArrayList<>(); + ArrayList<String> moreChildrenIds = new ArrayList<>(); ArrayList<Comment> newComments = new ArrayList<>(); - parseCommentRecursion(childrenArray, newComments, moreChildrenFullnames, 0); + parseCommentRecursion(childrenArray, newComments, moreChildrenIds, 0); expandChildren(newComments, expandedNewComments, expandChildren); if (expandChildren) { @@ -43,7 +46,7 @@ public class ParseComment { commentData.addAll(newComments); } - handler.post(() -> parseCommentListener.onParseCommentSuccess(commentData, parentId, moreChildrenFullnames)); + handler.post(() -> parseCommentListener.onParseCommentSuccess(commentData, parentId, moreChildrenIds)); } catch (JSONException e) { e.printStackTrace(); handler.post(parseCommentListener::onParseCommentFailed); @@ -52,17 +55,75 @@ public class ParseComment { } static void parseMoreComment(Executor executor, Handler handler, String response, - ArrayList<Comment> commentData, int depth, boolean expandChildren, + ArrayList<Comment> commentData, boolean expandChildren, ParseCommentListener parseCommentListener) { executor.execute(() -> { try { - JSONArray childrenArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + JSONArray childrenArray = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY) + .getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.THINGS_KEY); ArrayList<Comment> newComments = new ArrayList<>(); ArrayList<Comment> expandedNewComments = new ArrayList<>(); - ArrayList<String> moreChildrenFullnames = new ArrayList<>(); + ArrayList<String> moreChildrenIds = new ArrayList<>(); + + // api response is a flat list of comments tree + // process it in order and rebuild the tree + for (int i = 0; i < childrenArray.length(); i++) { + JSONObject child = childrenArray.getJSONObject(i); + JSONObject childData = child.getJSONObject(JSONUtils.DATA_KEY); + if (child.getString(JSONUtils.KIND_KEY).equals(JSONUtils.KIND_VALUE_MORE)) { + String parentFullName = childData.getString(JSONUtils.PARENT_ID_KEY); + JSONArray childrenIds = childData.getJSONArray(JSONUtils.CHILDREN_KEY); + + if (childrenIds.length() != 0) { + ArrayList<String> localMoreChildrenIds = new ArrayList<>(childrenIds.length()); + for (int j = 0; j < childrenIds.length(); j++) { + localMoreChildrenIds.add(childrenIds.getString(j)); + } + + Comment parentComment = findCommentByFullName(newComments, parentFullName); + if (parentComment != null) { + parentComment.setHasReply(true); + parentComment.setMoreChildrenIds(localMoreChildrenIds); + parentComment.addChildren(new ArrayList<>()); // ensure children list is not null + } else { + // assume that it is parent of this call + moreChildrenIds.addAll(localMoreChildrenIds); + } + } else { + Comment continueThreadPlaceholder = new Comment( + parentFullName, + childData.getInt(JSONUtils.DEPTH_KEY), + Comment.PLACEHOLDER_CONTINUE_THREAD + ); + + Comment parentComment = findCommentByFullName(newComments, parentFullName); + if (parentComment != null) { + parentComment.setHasReply(true); + parentComment.addChild(continueThreadPlaceholder, parentComment.getChildCount()); + parentComment.setChildCount(parentComment.getChildCount() + 1); + } else { + // assume that it is parent of this call + newComments.add(continueThreadPlaceholder); + } + } + } else { + Comment comment = parseSingleComment(childData, 0); + String parentFullName = comment.getParentId(); + + Comment parentComment = findCommentByFullName(newComments, parentFullName); + if (parentComment != null) { + parentComment.setHasReply(true); + parentComment.addChild(comment, parentComment.getChildCount()); + parentComment.setChildCount(parentComment.getChildCount() + 1); + } else { + // assume that it is parent of this call + newComments.add(comment); + } + } + } - parseCommentRecursion(childrenArray, newComments, moreChildrenFullnames, depth); + updateChildrenCount(newComments); expandChildren(newComments, expandedNewComments, expandChildren); if (expandChildren) { @@ -71,7 +132,7 @@ public class ParseComment { commentData.addAll(newComments); } - handler.post(() -> parseCommentListener.onParseCommentSuccess(commentData, null, moreChildrenFullnames)); + handler.post(() -> parseCommentListener.onParseCommentSuccess(commentData, null, moreChildrenIds)); } catch (JSONException e) { e.printStackTrace(); handler.post(parseCommentListener::onParseCommentFailed); @@ -96,7 +157,7 @@ public class ParseComment { } private static void parseCommentRecursion(JSONArray comments, ArrayList<Comment> newCommentData, - ArrayList<String> moreChildrenFullnames, int depth) throws JSONException { + ArrayList<String> moreChildrenIds, int depth) throws JSONException { int actualCommentLength; if (comments.length() == 0) { @@ -105,17 +166,17 @@ public class ParseComment { JSONObject more = comments.getJSONObject(comments.length() - 1).getJSONObject(JSONUtils.DATA_KEY); - //Maybe moreChildrenFullnames contain only commentsJSONArray and no more info + //Maybe moreChildrenIds contain only commentsJSONArray and no more info if (more.has(JSONUtils.COUNT_KEY)) { JSONArray childrenArray = more.getJSONArray(JSONUtils.CHILDREN_KEY); for (int i = 0; i < childrenArray.length(); i++) { - moreChildrenFullnames.add("t1_" + childrenArray.getString(i)); + moreChildrenIds.add(childrenArray.getString(i)); } actualCommentLength = comments.length() - 1; - if (moreChildrenFullnames.isEmpty() && comments.getJSONObject(comments.length() - 1).getString(JSONUtils.KIND_KEY).equals(JSONUtils.KIND_VALUE_MORE)) { + if (moreChildrenIds.isEmpty() && comments.getJSONObject(comments.length() - 1).getString(JSONUtils.KIND_KEY).equals(JSONUtils.KIND_VALUE_MORE)) { newCommentData.add(new Comment(more.getString(JSONUtils.PARENT_ID_KEY), more.getInt(JSONUtils.DEPTH_KEY), Comment.PLACEHOLDER_CONTINUE_THREAD)); return; } @@ -131,10 +192,10 @@ public class ParseComment { JSONArray childrenArray = data.getJSONObject(JSONUtils.REPLIES_KEY) .getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); ArrayList<Comment> children = new ArrayList<>(); - ArrayList<String> nextMoreChildrenFullnames = new ArrayList<>(); - parseCommentRecursion(childrenArray, children, nextMoreChildrenFullnames, singleComment.getDepth()); + ArrayList<String> nextMoreChildrenIds = new ArrayList<>(); + parseCommentRecursion(childrenArray, children, nextMoreChildrenIds, singleComment.getDepth()); singleComment.addChildren(children); - singleComment.setMoreChildrenFullnames(nextMoreChildrenFullnames); + singleComment.setMoreChildrenIds(nextMoreChildrenIds); singleComment.setChildCount(getChildCount(singleComment)); } @@ -165,7 +226,7 @@ public class ParseComment { } else { c.setExpanded(true); } - if (c.hasMoreChildrenFullnames() && c.getMoreChildrenFullnames().size() > c.getMoreChildrenStartingIndex()) { + if (c.hasMoreChildrenIds() && !c.getMoreChildrenIds().isEmpty()) { //Add a load more placeholder Comment placeholder = new Comment(c.getFullName(), c.getDepth() + 1, Comment.PLACEHOLDER_LOAD_MORE_COMMENTS); visibleComments.add(placeholder); @@ -277,9 +338,35 @@ public class ParseComment { return null; } + @Nullable + private static Comment findCommentByFullName(@NonNull List<Comment> comments, @NonNull String fullName) { + for (Comment comment: comments) { + if (comment.getFullName().equals(fullName) && + comment.getPlaceholderType() == Comment.NOT_PLACEHOLDER) { + return comment; + } + if (comment.getChildren() != null) { + Comment result = findCommentByFullName(comment.getChildren(), fullName); + if (result != null) { + return result; + } + } + } + return null; + } + + private static void updateChildrenCount(@NonNull List<Comment> comments) { + for (Comment comment: comments) { + comment.setChildCount(getChildCount(comment)); + if (comment.getChildren() != null) { + updateChildrenCount(comment.getChildren()); + } + } + } + public interface ParseCommentListener { void onParseCommentSuccess(ArrayList<Comment> expandedComments, String parentId, - ArrayList<String> moreChildrenFullnames); + ArrayList<String> moreChildrenIds); void onParseCommentFailed(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index 87f42698..1f1e2b6f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -202,8 +202,6 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic @State ArrayList<String> children; @State - int mChildrenStartingIndex = 0; - @State boolean loadMoreChildrenSuccess = true; @State boolean hasMoreChildren; @@ -621,6 +619,11 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic fetchMoreComments(); } + + @Override + public String getSortType() { + return sortType; + } }); if (mCommentsRecyclerView != null) { mRecyclerView.setAdapter(mPostAdapter); @@ -810,7 +813,6 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic public void changeSortType(SortType sortType) { mFetchPostInfoLinearLayout.setVisibility(View.GONE); mGlide.clear(mFetchPostInfoImageView); - mChildrenStartingIndex = 0; if (children != null) { children.clear(); } @@ -1328,6 +1330,11 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic fetchMoreComments(); } + + @Override + public String getSortType() { + return sortType; + } }); if (mCommentsRecyclerView != null) { mRecyclerView.setAdapter(mPostAdapter); @@ -1343,8 +1350,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic ParseComment.parseComment(mExecutor, new Handler(), response.body(), new ArrayList<>(), mExpandChildren, new ParseComment.ParseCommentListener() { @Override - public void onParseCommentSuccess(ArrayList<Comment> expandedComments, String parentId, ArrayList<String> moreChildrenFullnames) { - ViewPostDetailFragment.this.children = moreChildrenFullnames; + public void onParseCommentSuccess(ArrayList<Comment> expandedComments, String parentId, ArrayList<String> moreChildrenIds) { + ViewPostDetailFragment.this.children = moreChildrenIds; hasMoreChildren = children.size() != 0; mCommentsAdapter.addComments(expandedComments, hasMoreChildren); @@ -1567,13 +1574,13 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic isLoadingMoreChildren = true; Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; - FetchComment.fetchMoreComment(mExecutor, new Handler(), retrofit, mAccessToken, children, mChildrenStartingIndex, - 0, mExpandChildren, new FetchComment.FetchMoreCommentListener() { + FetchComment.fetchMoreComment(mExecutor, new Handler(), retrofit, mAccessToken, children, + mExpandChildren, mPost.getFullName(), sortType, new FetchComment.FetchMoreCommentListener() { @Override - public void onFetchMoreCommentSuccess(ArrayList<Comment> expandedComments, int childrenStartingIndex) { - hasMoreChildren = childrenStartingIndex < children.size(); + public void onFetchMoreCommentSuccess(ArrayList<Comment> expandedComments, ArrayList<String> moreChildrenIds) { + children = moreChildrenIds; + hasMoreChildren = !children.isEmpty(); mCommentsAdapter.addComments(expandedComments, hasMoreChildren); - mChildrenStartingIndex = childrenStartingIndex; isLoadingMoreChildren = false; loadMoreChildrenSuccess = true; } @@ -1590,7 +1597,6 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic public void refresh(boolean fetchPost, boolean fetchComments) { if (mPostAdapter != null && !isRefreshing) { isRefreshing = true; - mChildrenStartingIndex = 0; mFetchPostInfoLinearLayout.setVisibility(View.GONE); mGlide.clear(mFetchPostInfoImageView); |