diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-08-12 16:28:50 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-08-12 16:28:50 +0000 |
commit | fac3b83bae3e1603ba01ad6d3ceed365a6faf93c (patch) | |
tree | a1124cd5861e6af729de2fe0424ce23d5b4c6070 /app/src/main | |
parent | a8dd4b351b1382ea73d43ada8c5f2de48d728c2f (diff) | |
download | infinity-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.java | 107 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 |
2 files changed, 104 insertions, 4 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) -> { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e1f8c969..f79108a6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -408,7 +408,6 @@ <string name="settings_enable_nsfw_title">Enable NSFW</string> <string name="settings_blur_nsfw_title">Blur NSFW Images</string> <string name="settings_blur_spoiler_title">Blur Spoiler Images</string> - <string name="settings_layout_no_limits_title">Display Under Navigation bar and Status Bar</string> <string name="settings_about_master_title">About</string> <string name="settings_acknowledgement_master_title">Acknowledgement</string> <string name="settings_credits_master_title">Credits</string> |