From 1984332fddeebe02db8e569f821391a45f97d904 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 18 Aug 2019 09:25:52 +0800 Subject: Enqueue only one instance of PullNotificationWorker. Fixed bugs related to comment single thread mode. --- .../CommentAndPostRecyclerViewAdapter.java | 188 +++++++++++++++------ .../infinityforreddit/MainActivity.java | 6 +- .../infinityforreddit/PullNotificationWorker.java | 2 + 3 files changed, 138 insertions(+), 58 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index 9816946c..89b2817b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -492,7 +492,11 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter comments, boolean hasMoreComments) { @@ -627,13 +640,25 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter getVisibleComments() { - return mVisibleComments; + if(mIsSingleCommentThreadMode) { + notifyItemChanged(mVisibleComments.size() + 2); + } else { + notifyItemChanged(mVisibleComments.size() + 1); + } } void editComment(String commentContent, int position) { mVisibleComments.get(position).setCommentContent(commentContent); - notifyItemChanged(position + 1); + if(mIsSingleCommentThreadMode) { + notifyItemChanged(position + 2); + } else { + notifyItemChanged(position + 1); + } } void deleteComment(int position) { if(mVisibleComments.get(position).hasReply()) { mVisibleComments.get(position).setAuthor("[deleted]"); mVisibleComments.get(position).setCommentContent("[deleted]"); - notifyItemChanged(position + 1); + if(mIsSingleCommentThreadMode) { + notifyItemChanged(position + 2); + } else { + notifyItemChanged(position + 1); + } } else { mVisibleComments.remove(position); - notifyItemRemoved(position + 1); + if(mIsSingleCommentThreadMode) { + notifyItemRemoved(position + 2); + } else { + notifyItemRemoved(position + 1); + } } } @@ -978,25 +1023,31 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mVisibleComments.get(getAdapterPosition() - 1).getAuthor()); + if(mIsSingleCommentThreadMode) { + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mVisibleComments.get(getAdapterPosition() - 2).getAuthor()); + } else { + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mVisibleComments.get(getAdapterPosition() - 1).getAuthor()); + } mActivity.startActivity(intent); }); shareButton.setOnClickListener(view -> { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); - String extraText = mVisibleComments.get(getAdapterPosition() - 1).getPermalink(); + String extraText = mIsSingleCommentThreadMode ? mVisibleComments.get(getAdapterPosition() - 2).getPermalink() + : mVisibleComments.get(getAdapterPosition() - 1).getPermalink(); intent.putExtra(Intent.EXTRA_TEXT, extraText); mActivity.startActivity(Intent.createChooser(intent, "Share")); }); expandButton.setOnClickListener(view -> { - if(mVisibleComments.get(getAdapterPosition() - 1).isExpanded()) { - collapseChildren(getAdapterPosition() - 1); + int commentPosition = mIsSingleCommentThreadMode ? getAdapterPosition() - 2 : getAdapterPosition() - 1; + if(mVisibleComments.get(commentPosition).isExpanded()) { + collapseChildren(commentPosition); expandButton.setImageResource(R.drawable.ic_expand_more_black_20dp); } else { - expandChildren(getAdapterPosition() - 1); - mVisibleComments.get(getAdapterPosition() - 1).setExpanded(true); + expandChildren(commentPosition); + mVisibleComments.get(commentPosition).setExpanded(true); expandButton.setImageResource(R.drawable.ic_expand_less_black_20dp); } }); @@ -1007,12 +1058,15 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { @@ -1067,43 +1123,45 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { - int parentPosition = getParentPosition(getAdapterPosition() - 1); + int commentPosition = mIsSingleCommentThreadMode ? getAdapterPosition() - 2 : getAdapterPosition() - 1; + int parentPosition = getParentPosition(commentPosition); CommentData parentComment = mVisibleComments.get(parentPosition); - mVisibleComments.get(getAdapterPosition() - 1).setLoadingMoreChildren(true); - mVisibleComments.get(getAdapterPosition() - 1).setLoadMoreChildrenFailed(false); + mVisibleComments.get(commentPosition).setLoadingMoreChildren(true); + mVisibleComments.get(commentPosition).setLoadMoreChildrenFailed(false); placeholderTextView.setText(R.string.loading); FetchComment.fetchMoreComment(mRetrofit, parentComment.getMoreChildrenFullnames(), @@ -1140,8 +1199,8 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter