From 400b8b5274a7b5d19e57bfd0f6eaa89afb1b9192 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 14 Aug 2020 14:14:57 +0800 Subject: Fix regex for spoiler. Support spoiler in CommentsListingRecyclerViewAdapter. --- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 33 ++++------- .../CommentsListingRecyclerViewAdapter.java | 65 ++++++++++++++++++++++ 2 files changed, 76 insertions(+), 22 deletions(-) (limited to 'app/src/main/java/ml') 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 defa4363..c63ba1e2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -13,7 +13,6 @@ import android.net.Uri; 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; @@ -251,7 +250,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter!.+!<"); + Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<"); Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); while (matcher.find()) { markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">"); @@ -262,18 +261,15 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter!.+!<"); + SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString()); + Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<"); 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); ClickableSpan clickableSpan = new ClickableSpan() { private boolean isShowing = false; @Override @@ -294,13 +290,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter!.+!<"); + Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<"); Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); while (matcher.find()) { markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">"); @@ -364,27 +358,24 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter!.+!<"); + Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<"); 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); ClickableSpan clickableSpan = new ClickableSpan() { private boolean isShowing = false; @Override public void updateDrawState(@NonNull TextPaint ds) { if (isShowing) { super.updateDrawState(ds); - ds.setColor(mCommentTextColor); + ds.setColor(markdownColor); } else { ds.bgColor = Color.BLACK; - ds.setColor(mCommentTextColor); + ds.setColor(markdownColor); } ds.setUnderlineText(false); } @@ -395,13 +386,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter![\\S\\s]*?!<"); + Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); + while (matcher.find()) { + markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">"); + } + return super.processMarkdown(markdownStringBuilder.toString()); + } + + @Override + public void afterSetText(@NonNull TextView textView) { + textView.setHighlightColor(Color.TRANSPARENT); + SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString()); + Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<"); + Matcher matcher = spoilerPattern.matcher(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); + int matcherStart = matcher.start(); + int matcherEnd = matcher.end(); + ClickableSpan clickableSpan = new ClickableSpan() { + private boolean isShowing = false; + @Override + public void updateDrawState(@NonNull TextPaint ds) { + if (isShowing) { + super.updateDrawState(ds); + ds.setColor(mCommentColor); + } else { + ds.bgColor = Color.BLACK; + ds.setColor(mCommentColor); + } + ds.setUnderlineText(false); + } + + @Override + public void onClick(@NonNull View view) { + if (!(isShowing && markdownStringBuilder.getSpans(matcherStart, matcherEnd - 4, URLSpan.class).length > 0)) { + isShowing = !isShowing; + view.invalidate(); + } + } + }; + markdownStringBuilder.setSpan(clickableSpan, matcherStart, matcherEnd - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + start = matcherEnd - 4; + } + if (find) { + textView.setText(markdownStringBuilder); + } + } + @Override public void configureTheme(@NonNull MarkwonTheme.Builder builder) { builder.linkColor(customThemeWrapper.getLinkColor()); -- cgit v1.2.3