diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-08-14 06:14:57 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-08-14 06:14:57 +0000 |
commit | 400b8b5274a7b5d19e57bfd0f6eaa89afb1b9192 (patch) | |
tree | a6218e1b15e801b11f7e4d3d3f8e61d42b993ac8 | |
parent | 362932460f787ef9af958e764fcf5bde12449b07 (diff) | |
download | infinity-for-reddit-400b8b5274a7b5d19e57bfd0f6eaa89afb1b9192.tar infinity-for-reddit-400b8b5274a7b5d19e57bfd0f6eaa89afb1b9192.tar.gz infinity-for-reddit-400b8b5274a7b5d19e57bfd0f6eaa89afb1b9192.tar.bz2 infinity-for-reddit-400b8b5274a7b5d19e57bfd0f6eaa89afb1b9192.tar.lz infinity-for-reddit-400b8b5274a7b5d19e57bfd0f6eaa89afb1b9192.tar.xz infinity-for-reddit-400b8b5274a7b5d19e57bfd0f6eaa89afb1b9192.tar.zst infinity-for-reddit-400b8b5274a7b5d19e57bfd0f6eaa89afb1b9192.zip |
Fix regex for spoiler. Support spoiler in CommentsListingRecyclerViewAdapter.
2 files changed, 76 insertions, 22 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 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<Recy @Override public String processMarkdown(@NonNull String markdown) { StringBuilder markdownStringBuilder = new StringBuilder(markdown); - Pattern spoilerPattern = Pattern.compile(">!.+!<"); + 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<Recy @Override public void afterSetText(@NonNull TextView textView) { textView.setHighlightColor(Color.TRANSPARENT); - StringBuilder markdownStringBuilder = new StringBuilder(textView.getText().toString()); - Pattern spoilerPattern = Pattern.compile(">!.+!<"); + 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<Recy view.invalidate(); } }; - spannableCopy.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - - spannable = spannableCopy; + markdownStringBuilder.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); start = matcher.end() - 4; } if (find) { - textView.setText(spannable); + textView.setText(markdownStringBuilder); } } @@ -353,7 +347,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy @Override public String processMarkdown(@NonNull String markdown) { StringBuilder markdownStringBuilder = new StringBuilder(markdown); - Pattern spoilerPattern = Pattern.compile(">!.+!<"); + 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<Recy @Override public void afterSetText(@NonNull TextView textView) { SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString()); - Pattern spoilerPattern = Pattern.compile(">!.+!<"); + 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<Recy view.invalidate(); } }; - spannableCopy.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - - spannable = spannableCopy; + markdownStringBuilder.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); start = matcher.end() - 4; } if (find) { - textView.setText(spannable); + textView.setText(markdownStringBuilder); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java index 349fb1b7..1db1f402 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java @@ -4,9 +4,15 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; +import android.graphics.Color; import android.net.Uri; import android.os.Bundle; +import android.text.SpannableStringBuilder; +import android.text.Spanned; +import android.text.TextPaint; +import android.text.style.ClickableSpan; import android.text.style.SuperscriptSpan; +import android.text.style.URLSpan; import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.View; @@ -27,6 +33,8 @@ import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.RecyclerView; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import butterknife.BindView; import butterknife.ButterKnife; @@ -102,8 +110,65 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment super(DIFF_CALLBACK); mContext = context; mOauthRetrofit = oauthRetrofit; + mCommentColor = customThemeWrapper.getCommentColor(); mMarkwon = Markwon.builder(mContext) .usePlugin(new AbstractMarkwonPlugin() { + @NonNull + @Override + public String processMarkdown(@NonNull String markdown) { + StringBuilder markdownStringBuilder = new StringBuilder(markdown); + Pattern spoilerPattern = Pattern.compile(">![\\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()); |