aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2020-08-12 16:28:50 +0000
committerAlex Ning <chineseperson5@gmail.com>2020-08-12 16:28:50 +0000
commitfac3b83bae3e1603ba01ad6d3ceed365a6faf93c (patch)
treea1124cd5861e6af729de2fe0424ce23d5b4c6070 /app/src/main/java
parenta8dd4b351b1382ea73d43ada8c5f2de48d728c2f (diff)
downloadinfinity-for-reddit-fac3b83bae3e1603ba01ad6d3ceed365a6faf93c.tar
infinity-for-reddit-fac3b83bae3e1603ba01ad6d3ceed365a6faf93c.tar.gz
infinity-for-reddit-fac3b83bae3e1603ba01ad6d3ceed365a6faf93c.tar.bz2
infinity-for-reddit-fac3b83bae3e1603ba01ad6d3ceed365a6faf93c.tar.lz
infinity-for-reddit-fac3b83bae3e1603ba01ad6d3ceed365a6faf93c.tar.xz
infinity-for-reddit-fac3b83bae3e1603ba01ad6d3ceed365a6faf93c.tar.zst
infinity-for-reddit-fac3b83bae3e1603ba01ad6d3ceed365a6faf93c.zip
Use a hack to make spoiler text hidden at first. Click the spoiler area to reveal the spoiler text.
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java107
1 files changed, 104 insertions, 3 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 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<Recy
int linkColor = customThemeWrapper.getLinkColor();
mPostDetailMarkwon = 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("<!.+!<");
+ Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
+ Spannable spannable = new SpannableString(markdownStringBuilder);
+ int start = 0;
+ while (matcher.find(start)) {
+ 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() {
+ @Override
+ public void updateDrawState(@NonNull TextPaint ds) {
+ ds.setUnderlineText(false);
+ }
+
+ @Override
+ public void onClick(@NonNull View view) {
+ spannableCopy.removeSpan(backgroundColorSpan);
+ spannableCopy.removeSpan(this);
+ }
+ };
+ 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);
+ }
+
@Override
public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) {
textView.setTextColor(markdownColor);
@@ -275,15 +329,62 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
.usePlugin(StrikethroughPlugin.create())
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(SimpleExtPlugin.create(plugin ->
- 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("<!.+!<");
+ Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
+ Spannable spannable = new SpannableString(markdownStringBuilder);
+ int start = 0;
+ while (matcher.find(start)) {
+ 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() {
+ @Override
+ public void updateDrawState(@NonNull TextPaint ds) {
+ ds.setUnderlineText(false);
+ }
+
+ @Override
+ public void onClick(@NonNull View view) {
+ spannableCopy.removeSpan(backgroundColorSpan);
+ spannableCopy.removeSpan(this);
+ }
+ };
+ 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);
+ }
+
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.linkResolver((view, link) -> {