From 8092c07ea01597bb6eb17e344cf3c7cbfc74768d Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 18 Jun 2019 17:41:45 +0800 Subject: Fixed sending child comments. --- .../infinityforreddit/CommentActivity.java | 1 + .../infinityforreddit/CommentData.java | 7 ++++++ .../CommentRecyclerViewAdapter.java | 27 ++++++++++++++-------- .../infinityforreddit/ViewPostDetailActivity.java | 3 ++- 4 files changed, 28 insertions(+), 10 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java index eef1d810..e05d4d17 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java @@ -98,6 +98,7 @@ public class CommentActivity extends AppCompatActivity { public void sendCommentSuccess(CommentData commentData) { Intent returnIntent = new Intent(); returnIntent.putExtra(EXTRA_COMMENT_DATA_KEY, commentData); + returnIntent.putExtra(EXTRA_PARENT_FULLNAME_KEY, parentFullname); if(isReplying) { returnIntent.putExtra(EXTRA_PARENT_POSITION_KEY, parentPosition); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java index 6ade3dd8..ac4b5823 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java @@ -171,6 +171,13 @@ class CommentData implements Parcelable { } } + public void addChild(CommentData comment) { + if(children == null) { + children = new ArrayList<>(); + } + children.add(0, comment); + } + public ArrayList getMoreChildrenIds() { return moreChildrenIds; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java index 260bb5bf..dc2e49f0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java @@ -334,16 +334,25 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter childComments = mCommentData.get(parentPosition).getChildren(); - if(childComments == null) { - childComments = new ArrayList<>(); + void addChildComment(CommentData comment, String parentFullname, int parentPosition) { + if(parentFullname.equals(mVisibleComments.get(parentPosition).getFullName())) { + for(int i = 0; i < mVisibleComments.size(); i++) { + if(parentFullname.equals(mVisibleComments.get(i).getFullName())) { + parentPosition = i; + break; + } + } + } + + mVisibleComments.get(parentPosition).addChild(comment); + mVisibleComments.get(parentPosition).setHasReply(true); + if(!mVisibleComments.get(parentPosition).isExpanded()) { + expandChildren(parentPosition); + notifyItemChanged(parentPosition); + } else { + mVisibleComments.add(parentPosition + 1, comment); + notifyItemInserted(parentPosition + 1); } - childComments.add(0, comment); - mCommentData.get(parentPosition).addChildren(childComments); - mCommentData.get(parentPosition).setHasReply(true); - notifyItemChanged(parentPosition); } void clearData() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 95a9ba0e..58c4d571 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -694,8 +694,9 @@ public class ViewPostDetailActivity extends AppCompatActivity { if(comment.getDepth() == 0) { mAdapter.addComment(comment); } else { + String parentFullname = data.getExtras().getString(CommentActivity.EXTRA_PARENT_FULLNAME_KEY); int parentPosition = data.getExtras().getInt(CommentActivity.EXTRA_PARENT_POSITION_KEY); - mAdapter.addChildComment(comment, parentPosition); + mAdapter.addChildComment(comment, parentFullname, parentPosition); } } else { Toast.makeText(this, R.string.send_comment_failed, Toast.LENGTH_SHORT).show(); -- cgit v1.2.3