Fetch more comments button now works!

Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-07-24 08:38:36 +02:00
parent ec6770a904
commit 350a37d7a7
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
3 changed files with 46 additions and 75 deletions

View File

@ -609,42 +609,20 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
mVisibleComments.get(commentPosition).setLoadMoreChildrenFailed(false); mVisibleComments.get(commentPosition).setLoadMoreChildrenFailed(false);
((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.loading); ((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.loading);
Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; Retrofit retrofit = mRetrofit;
SortType.Type sortType = mCommentRecyclerViewAdapterCallback.getSortType(); SortType.Type sortType = mCommentRecyclerViewAdapterCallback.getSortType();
FetchComment.fetchMoreComment(mExecutor, new Handler(), retrofit, mAccessToken, FetchComment.fetchComments(mExecutor, new Handler(), retrofit, mAccessToken,
parentComment.getMoreChildrenIds(), mPost.getId(), parentComment.getId(), sortType,
mExpandChildren, mPost.getFullName(), sortType, new FetchComment.FetchMoreCommentListener() { mExpandChildren, 1, new FetchComment.FetchCommentListener() {
@Override @Override
public void onFetchMoreCommentSuccess(ArrayList<Comment> topLevelComments, public void onFetchCommentSuccess(ArrayList<Comment> expandedComments, Integer parentId, ArrayList<Integer> children) {
ArrayList<Comment> expandedComments,
ArrayList<Integer> moreChildrenIds) {
if (mVisibleComments.size() > parentPosition if (mVisibleComments.size() > parentPosition
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) { && parentComment.getId() == mVisibleComments.get(parentPosition).getId()) {
if (mVisibleComments.get(parentPosition).isExpanded()) { if (mVisibleComments.get(parentPosition).isExpanded()) {
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)
.setLoadMoreChildrenFailed(false);
int placeholderPosition = findLoadMoreCommentsPlaceholderPosition(parentComment.getFullName(), commentPosition);
if (placeholderPosition != -1) {
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(false);
((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.comment_load_more_comments);
mVisibleComments.addAll(placeholderPosition, expandedComments);
if (mIsSingleCommentThreadMode) {
notifyItemRangeInserted(placeholderPosition + 1, expandedComments.size());
} else {
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
}
}
} else {
mVisibleComments.get(parentPosition).getChildren() mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1); .remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.get(parentPosition).removeMoreChildrenIds(); // mVisibleComments.get(parentPosition).removeMoreChildrenIds();
int placeholderPosition = findLoadMoreCommentsPlaceholderPosition(parentComment.getFullName(), commentPosition); int placeholderPosition = findLoadMoreCommentsPlaceholderPosition(parentComment.getFullName(), commentPosition);
if (placeholderPosition != -1) { if (placeholderPosition != -1) {
@ -662,16 +640,16 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
notifyItemRangeInserted(placeholderPosition, expandedComments.size()); notifyItemRangeInserted(placeholderPosition, expandedComments.size());
} }
} }
}
} else { } else {
if (mVisibleComments.get(parentPosition).hasReply() && moreChildrenIds.isEmpty()) { if (mVisibleComments.get(parentPosition).hasReply()) {
mVisibleComments.get(parentPosition).getChildren() mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1); .remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.get(parentPosition).removeMoreChildrenIds(); mVisibleComments.get(parentPosition).removeMoreChildrenIds();
} }
} }
mVisibleComments.get(parentPosition).addChildren(topLevelComments); mVisibleComments.get(parentPosition).addChildren(expandedComments);
if (mIsSingleCommentThreadMode) { if (mIsSingleCommentThreadMode) {
notifyItemChanged(parentPosition + 1); notifyItemChanged(parentPosition + 1);
} else { } else {
@ -702,7 +680,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
.setLoadingMoreChildren(false); .setLoadingMoreChildren(false);
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1) mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
.setLoadMoreChildrenFailed(false); .setLoadMoreChildrenFailed(false);
mVisibleComments.get(i).addChildren(topLevelComments); mVisibleComments.get(i).addChildren(expandedComments);
if (mIsSingleCommentThreadMode) { if (mIsSingleCommentThreadMode) {
notifyItemChanged(i + 1); notifyItemChanged(i + 1);
} else { } else {
@ -716,7 +694,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
} }
@Override @Override
public void onFetchMoreCommentFailed() { public void onFetchCommentFailed() {
int currentParentPosition = findCommentPosition(parentComment.getFullName(), parentPosition); int currentParentPosition = findCommentPosition(parentComment.getFullName(), parentPosition);
if (currentParentPosition == -1) { if (currentParentPosition == -1) {
// note: returning here is probably a mistake, because // note: returning here is probably a mistake, because

View File

@ -10,8 +10,6 @@ import java.util.concurrent.Executor;
import eu.toldi.infinityforlemmy.SortType; import eu.toldi.infinityforlemmy.SortType;
import eu.toldi.infinityforlemmy.apis.LemmyAPI; import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import eu.toldi.infinityforlemmy.apis.RedditAPI;
import eu.toldi.infinityforlemmy.utils.APIUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
@ -60,29 +58,14 @@ public class FetchComment {
} }
public static void fetchMoreComment(Executor executor, Handler handler, Retrofit retrofit, public static void fetchMoreComment(Executor executor, Handler handler, Retrofit retrofit,
@Nullable String accessToken, @Nullable String accessToken, int article,
ArrayList<Integer> allChildren, int commentId, SortType.Type sortType, boolean expandChildren,
boolean expandChildren, String postFullName, Integer page, FetchMoreCommentListener fetchMoreCommentListener) {
SortType.Type sortType, LemmyAPI api = retrofit.create(LemmyAPI.class);
FetchMoreCommentListener fetchMoreCommentListener) {
if (allChildren == null) {
return;
}
String childrenIds = "";
if (childrenIds.isEmpty()) {
return;
}
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> moreComments; Call<String> moreComments;
if (accessToken == null) {
moreComments = api.moreChildren(postFullName, childrenIds, sortType); moreComments = api.getComments("All", sortType.value, 8, page, 25, null, null, article, commentId, false, accessToken);
} else {
moreComments = api.moreChildrenOauth(postFullName, childrenIds,
sortType, APIUtils.getOAuthHeader(accessToken));
}
moreComments.enqueue(new Callback<String>() { moreComments.enqueue(new Callback<String>() {
@Override @Override

View File

@ -48,6 +48,9 @@ public class ParseComment {
topLevelComments.add(singleComment); topLevelComments.add(singleComment);
} }
} }
Comment parentComment = orderedComments.get(0);
if (parentComment.getDepth() == 0)
parentComment = null;
for (int i = orderedComments.size() - 1; i >= 0; i--) { for (int i = orderedComments.size() - 1; i >= 0; i--) {
Comment c = orderedComments.get(i); Comment c = orderedComments.get(i);
@ -67,6 +70,14 @@ public class ParseComment {
expandChildren(newComments, expandedNewComments, expandChildren); expandChildren(newComments, expandedNewComments, expandChildren);
if (topLevelComments.isEmpty() && !parsedComments.isEmpty() && parentComment != null) {
for (int i = orderedComments.size() - 1; i >= 0; i--) {
Comment c = orderedComments.get(i);
if (c.getDepth() > parentComment.getDepth())
expandedNewComments.add(c);
}
}
ArrayList<Comment> commentData; ArrayList<Comment> commentData;
if (expandChildren) { if (expandChildren) {
commentData = expandedNewComments; commentData = expandedNewComments;
@ -86,8 +97,7 @@ public class ParseComment {
ParseCommentListener parseCommentListener) { ParseCommentListener parseCommentListener) {
executor.execute(() -> { executor.execute(() -> {
try { try {
JSONArray childrenArray = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY) JSONArray childrenArray = new JSONObject(response).getJSONArray("comments");
.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.THINGS_KEY);
ArrayList<Comment> newComments = new ArrayList<>(); ArrayList<Comment> newComments = new ArrayList<>();
ArrayList<Comment> expandedNewComments = new ArrayList<>(); ArrayList<Comment> expandedNewComments = new ArrayList<>();
@ -221,7 +231,7 @@ public class ParseComment {
} }
} }
private static int getChildCount(Comment comment) { public static int getChildCount(Comment comment) {
if (comment.getChildren() == null) { if (comment.getChildren() == null) {
return 0; return 0;
} }