From 4870d2bee64e454faf0f6a43269d0e00aaba52e7 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 14 Sep 2020 10:34:11 +0800 Subject: Fix spoiler background color that cannot hide spoiler. Support spoiler in MessageRecyclerViewAdapter. --- .../Activity/CommentFullMarkdownActivity.java | 3 +- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 8 +- .../CommentsListingRecyclerViewAdapter.java | 3 +- .../Adapter/MessageRecyclerViewAdapter.java | 90 +++++++++++++++++++--- 4 files changed, 89 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java index 1dde280f..0cf15283 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java @@ -114,6 +114,7 @@ public class CommentFullMarkdownActivity extends BaseActivity { String commentMarkdown = getIntent().getStringExtra(EXTRA_COMMENT_MARKDOWN); boolean isNsfw = getIntent().getBooleanExtra(EXTRA_IS_NSFW, false); int markdownColor = mCustomThemeWrapper.getCommentColor(); + int spoilerBackgroundColor = markdownColor | 0xFF000000; int linkColor = mCustomThemeWrapper.getLinkColor(); Markwon markwon = Markwon.builder(this) .usePlugin(new AbstractMarkwonPlugin() { @@ -149,7 +150,7 @@ public class CommentFullMarkdownActivity extends BaseActivity { super.updateDrawState(ds); ds.setColor(markdownColor); } else { - ds.bgColor = markdownColor; + ds.bgColor = spoilerBackgroundColor; ds.setColor(markdownColor); } ds.setUnderlineText(false); 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 fec7839a..9298c0b2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -250,7 +250,9 @@ 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(mSecondaryTextColor); + } else { + ds.bgColor = spoilerBackgroundColor; + ds.setColor(mSecondaryTextColor); + } + ds.setUnderlineText(false); + } + + @Override + public void onClick(@NonNull View view) { + 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 configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { builder.linkResolver((view, link) -> { @@ -115,15 +188,6 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter ((DataViewHolder) holder).itemView.performClick()); + ((DataViewHolder) holder).contentCustomMarkwonView.setOnClickListener(view -> { + if (((DataViewHolder) holder).contentCustomMarkwonView.getSelectionStart() == -1 && ((DataViewHolder) holder).contentCustomMarkwonView.getSelectionEnd() == -1) { + ((DataViewHolder) holder).itemView.performClick(); + } + }); } } } @@ -293,6 +361,8 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter