From fac3b83bae3e1603ba01ad6d3ceed365a6faf93c Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 13 Aug 2020 00:28:50 +0800 Subject: Use a hack to make spoiler text hidden at first. Click the spoiler area to reveal the spoiler text. --- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 107 ++++++++++++++++++++- 1 file changed, 104 insertions(+), 3 deletions(-) (limited to 'app/src/main/java') 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 028e674e..60b00af6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -5,6 +5,7 @@ import android.content.SharedPreferences; import android.content.res.ColorStateList; import android.content.res.Configuration; import android.content.res.Resources; +import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; @@ -12,7 +13,11 @@ import android.net.Uri; import android.os.Bundle; import android.text.Html; import android.text.Spannable; +import android.text.SpannableString; import android.text.Spanned; +import android.text.TextPaint; +import android.text.style.BackgroundColorSpan; +import android.text.style.ClickableSpan; import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.view.LayoutInflater; @@ -56,6 +61,8 @@ import org.commonmark.ext.gfm.tables.TableBlock; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import butterknife.BindView; import butterknife.ButterKnife; @@ -239,6 +246,53 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter!.+!<"); + Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); + while (matcher.find()) { + markdownStringBuilder.setCharAt(matcher.start(), '<'); + } + 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(" - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) + plugin.addExtension(1, '^', (configuration, props) -> { + return new SuperscriptSpan(); + }) ) ) .usePlugin(TableEntryPlugin.create(mActivity)) .build(); mCommentMarkwon = Markwon.builder(mActivity) .usePlugin(new AbstractMarkwonPlugin() { + @NonNull + @Override + public String processMarkdown(@NonNull String markdown) { + StringBuilder markdownStringBuilder = new StringBuilder(markdown); + Pattern spoilerPattern = Pattern.compile(">!.+!<"); + Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); + while (matcher.find()) { + markdownStringBuilder.setCharAt(matcher.start(), '<'); + } + 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(" { -- cgit v1.2.3