From 04d1079ad717914c2cfae78f5a6a1d51f692f8f9 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 14 Aug 2020 10:24:10 +0800 Subject: [PATCH] Fix spoiler's background in comments cannot be removed in CommentAndPostRecyclerViewAdapter. Fix blockquote overriding spoiler. --- .../CommentAndPostRecyclerViewAdapter.java | 58 ++++++++++++++----- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 60b00af6..a530d2b6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -14,8 +14,10 @@ import android.os.Bundle; import android.text.Html; import android.text.Spannable; import android.text.SpannableString; +import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextPaint; +import android.text.method.LinkMovementMethod; import android.text.style.BackgroundColorSpan; import android.text.style.ClickableSpan; import android.text.style.SuperscriptSpan; @@ -253,7 +255,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter!.+!<"); Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); while (matcher.find()) { - markdownStringBuilder.setCharAt(matcher.start(), '<'); + markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">"); } return super.processMarkdown(markdownStringBuilder.toString()); } @@ -262,11 +264,13 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter!.+!<"); Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); Spannable spannable = new SpannableString(markdownStringBuilder); int start = 0; + boolean find = false; while (matcher.find(start)) { + find = true; markdownStringBuilder.delete(matcher.end() - 2, matcher.end()); markdownStringBuilder.delete(matcher.start(), matcher.start() + 2); @@ -290,7 +294,9 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter!.+!<"); Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); while (matcher.find()) { - markdownStringBuilder.setCharAt(matcher.start(), '<'); + markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">"); } return super.processMarkdown(markdownStringBuilder.toString()); } @Override public void afterSetText(@NonNull TextView textView) { - textView.setHighlightColor(Color.TRANSPARENT); - StringBuilder markdownStringBuilder = new StringBuilder(textView.getText().toString()); - Pattern spoilerPattern = Pattern.compile("!.+!<"); Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); Spannable spannable = new SpannableString(markdownStringBuilder); int start = 0; + boolean find = false; while (matcher.find(start)) { + find = true; markdownStringBuilder.delete(matcher.end() - 2, matcher.end()); markdownStringBuilder.delete(matcher.start(), matcher.start() + 2); Spannable spannableCopy = new SpannableString(markdownStringBuilder); - BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.BLACK); ClickableSpan clickableSpan = new ClickableSpan() { + private boolean isShowing = false; @Override public void updateDrawState(@NonNull TextPaint ds) { + if (isShowing) { + super.updateDrawState(ds); + ds.setColor(mCommentTextColor); + } else { + ds.bgColor = Color.BLACK; + ds.setColor(mCommentTextColor); + } ds.setUnderlineText(false); } @Override public void onClick(@NonNull View view) { - spannableCopy.removeSpan(backgroundColorSpan); - spannableCopy.removeSpan(this); + isShowing = !isShowing; + view.invalidate(); } }; spannableCopy.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - spannableCopy.setSpan(backgroundColorSpan, matcher.start(), matcher.end() - 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannable = spannableCopy; start = matcher.end() - 4; } - textView.setText(spannable); + if (find) { + textView.setText(spannable); + } } @Override @@ -3353,6 +3368,8 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter hideToolbar(); @@ -3360,20 +3377,29 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter expandComments(); - commentMarkdownView.setOnClickListener(expandCommentsOnClickListener); - itemView.setOnClickListener(expandCommentsOnClickListener); + commentMarkdownView.setOnClickListener(view -> { + if (commentMarkdownView.getSelectionStart() == -1 && commentMarkdownView.getSelectionEnd() == -1) { + expandComments(); + } + }); + itemView.setOnClickListener(view -> expandComments()); } else { if (mCommentToolbarHideOnClick) { + commentMarkdownView.setOnClickListener(view -> { + if (commentMarkdownView.getSelectionStart() == -1 && commentMarkdownView.getSelectionEnd() == -1) { + hideToolbar(); + } + }); View.OnClickListener hideToolbarOnClickListener = view -> hideToolbar(); linearLayout.setOnClickListener(hideToolbarOnClickListener); - commentMarkdownView.setOnClickListener(hideToolbarOnClickListener); commentTimeTextView.setOnClickListener(hideToolbarOnClickListener); } View.OnLongClickListener expandsCommentsOnLongClickListener = view -> expandComments(); commentMarkdownView.setOnLongClickListener(expandsCommentsOnLongClickListener); itemView.setOnLongClickListener(expandsCommentsOnLongClickListener); } + commentMarkdownView.setMovementMethod(LinkMovementMethod.getInstance()); + commentMarkdownView.setHighlightColor(Color.TRANSPARENT); } private boolean expandComments() {