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);
((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.loading);
Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit;
Retrofit retrofit = mRetrofit;
SortType.Type sortType = mCommentRecyclerViewAdapterCallback.getSortType();
FetchComment.fetchMoreComment(mExecutor, new Handler(), retrofit, mAccessToken,
parentComment.getMoreChildrenIds(),
mExpandChildren, mPost.getFullName(), sortType, new FetchComment.FetchMoreCommentListener() {
FetchComment.fetchComments(mExecutor, new Handler(), retrofit, mAccessToken,
mPost.getId(), parentComment.getId(), sortType,
mExpandChildren, 1, new FetchComment.FetchCommentListener() {
@Override
public void onFetchMoreCommentSuccess(ArrayList<Comment> topLevelComments,
ArrayList<Comment> expandedComments,
ArrayList<Integer> moreChildrenIds) {
public void onFetchCommentSuccess(ArrayList<Comment> expandedComments, Integer parentId, ArrayList<Integer> children) {
if (mVisibleComments.size() > parentPosition
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) {
&& parentComment.getId() == mVisibleComments.get(parentPosition).getId()) {
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()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.get(parentPosition).removeMoreChildrenIds();
// mVisibleComments.get(parentPosition).removeMoreChildrenIds();
int placeholderPosition = findLoadMoreCommentsPlaceholderPosition(parentComment.getFullName(), commentPosition);
if (placeholderPosition != -1) {
@ -662,16 +640,16 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
}
}
}
} else {
if (mVisibleComments.get(parentPosition).hasReply() && moreChildrenIds.isEmpty()) {
if (mVisibleComments.get(parentPosition).hasReply()) {
mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.get(parentPosition).removeMoreChildrenIds();
}
}
mVisibleComments.get(parentPosition).addChildren(topLevelComments);
mVisibleComments.get(parentPosition).addChildren(expandedComments);
if (mIsSingleCommentThreadMode) {
notifyItemChanged(parentPosition + 1);
} else {
@ -702,7 +680,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
.setLoadingMoreChildren(false);
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
.setLoadMoreChildrenFailed(false);
mVisibleComments.get(i).addChildren(topLevelComments);
mVisibleComments.get(i).addChildren(expandedComments);
if (mIsSingleCommentThreadMode) {
notifyItemChanged(i + 1);
} else {
@ -716,7 +694,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
}
@Override
public void onFetchMoreCommentFailed() {
public void onFetchCommentFailed() {
int currentParentPosition = findCommentPosition(parentComment.getFullName(), parentPosition);
if (currentParentPosition == -1) {
// 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.apis.LemmyAPI;
import eu.toldi.infinityforlemmy.apis.RedditAPI;
import eu.toldi.infinityforlemmy.utils.APIUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@ -60,29 +58,14 @@ public class FetchComment {
}
public static void fetchMoreComment(Executor executor, Handler handler, Retrofit retrofit,
@Nullable String accessToken,
ArrayList<Integer> allChildren,
boolean expandChildren, String postFullName,
SortType.Type sortType,
FetchMoreCommentListener fetchMoreCommentListener) {
if (allChildren == null) {
return;
}
String childrenIds = "";
if (childrenIds.isEmpty()) {
return;
}
RedditAPI api = retrofit.create(RedditAPI.class);
@Nullable String accessToken, int article,
int commentId, SortType.Type sortType, boolean expandChildren,
Integer page, FetchMoreCommentListener fetchMoreCommentListener) {
LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<String> moreComments;
if (accessToken == null) {
moreComments = api.moreChildren(postFullName, childrenIds, sortType);
} else {
moreComments = api.moreChildrenOauth(postFullName, childrenIds,
sortType, APIUtils.getOAuthHeader(accessToken));
}
moreComments = api.getComments("All", sortType.value, 8, page, 25, null, null, article, commentId, false, accessToken);
moreComments.enqueue(new Callback<String>() {
@Override

View File

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