mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-02 14:27:10 +01:00
Fix regex for spoiler. Support spoiler in CommentsListingRecyclerViewAdapter.
This commit is contained in:
parent
362932460f
commit
400b8b5274
@ -13,7 +13,6 @@ import android.net.Uri;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
import android.text.Spannable;
|
import android.text.Spannable;
|
||||||
import android.text.SpannableString;
|
|
||||||
import android.text.SpannableStringBuilder;
|
import android.text.SpannableStringBuilder;
|
||||||
import android.text.Spanned;
|
import android.text.Spanned;
|
||||||
import android.text.TextPaint;
|
import android.text.TextPaint;
|
||||||
@ -251,7 +250,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
|
|||||||
@Override
|
@Override
|
||||||
public String processMarkdown(@NonNull String markdown) {
|
public String processMarkdown(@NonNull String markdown) {
|
||||||
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
|
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
|
||||||
Pattern spoilerPattern = Pattern.compile(">!.+!<");
|
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
|
||||||
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
|
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">");
|
markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">");
|
||||||
@ -262,18 +261,15 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
|
|||||||
@Override
|
@Override
|
||||||
public void afterSetText(@NonNull TextView textView) {
|
public void afterSetText(@NonNull TextView textView) {
|
||||||
textView.setHighlightColor(Color.TRANSPARENT);
|
textView.setHighlightColor(Color.TRANSPARENT);
|
||||||
StringBuilder markdownStringBuilder = new StringBuilder(textView.getText().toString());
|
SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString());
|
||||||
Pattern spoilerPattern = Pattern.compile(">!.+!<");
|
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
|
||||||
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
|
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
|
||||||
Spannable spannable = new SpannableString(markdownStringBuilder);
|
|
||||||
int start = 0;
|
int start = 0;
|
||||||
boolean find = false;
|
boolean find = false;
|
||||||
while (matcher.find(start)) {
|
while (matcher.find(start)) {
|
||||||
find = true;
|
find = true;
|
||||||
markdownStringBuilder.delete(matcher.end() - 2, matcher.end());
|
markdownStringBuilder.delete(matcher.end() - 2, matcher.end());
|
||||||
markdownStringBuilder.delete(matcher.start(), matcher.start() + 2);
|
markdownStringBuilder.delete(matcher.start(), matcher.start() + 2);
|
||||||
|
|
||||||
Spannable spannableCopy = new SpannableString(markdownStringBuilder);
|
|
||||||
ClickableSpan clickableSpan = new ClickableSpan() {
|
ClickableSpan clickableSpan = new ClickableSpan() {
|
||||||
private boolean isShowing = false;
|
private boolean isShowing = false;
|
||||||
@Override
|
@Override
|
||||||
@ -294,13 +290,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
|
|||||||
view.invalidate();
|
view.invalidate();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
spannableCopy.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
markdownStringBuilder.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
|
||||||
spannable = spannableCopy;
|
|
||||||
start = matcher.end() - 4;
|
start = matcher.end() - 4;
|
||||||
}
|
}
|
||||||
if (find) {
|
if (find) {
|
||||||
textView.setText(spannable);
|
textView.setText(markdownStringBuilder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -353,7 +347,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
|
|||||||
@Override
|
@Override
|
||||||
public String processMarkdown(@NonNull String markdown) {
|
public String processMarkdown(@NonNull String markdown) {
|
||||||
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
|
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
|
||||||
Pattern spoilerPattern = Pattern.compile(">!.+!<");
|
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
|
||||||
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
|
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">");
|
markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">");
|
||||||
@ -364,27 +358,24 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
|
|||||||
@Override
|
@Override
|
||||||
public void afterSetText(@NonNull TextView textView) {
|
public void afterSetText(@NonNull TextView textView) {
|
||||||
SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString());
|
SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString());
|
||||||
Pattern spoilerPattern = Pattern.compile(">!.+!<");
|
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
|
||||||
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
|
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
|
||||||
Spannable spannable = new SpannableString(markdownStringBuilder);
|
|
||||||
int start = 0;
|
int start = 0;
|
||||||
boolean find = false;
|
boolean find = false;
|
||||||
while (matcher.find(start)) {
|
while (matcher.find(start)) {
|
||||||
find = true;
|
find = true;
|
||||||
markdownStringBuilder.delete(matcher.end() - 2, matcher.end());
|
markdownStringBuilder.delete(matcher.end() - 2, matcher.end());
|
||||||
markdownStringBuilder.delete(matcher.start(), matcher.start() + 2);
|
markdownStringBuilder.delete(matcher.start(), matcher.start() + 2);
|
||||||
|
|
||||||
Spannable spannableCopy = new SpannableString(markdownStringBuilder);
|
|
||||||
ClickableSpan clickableSpan = new ClickableSpan() {
|
ClickableSpan clickableSpan = new ClickableSpan() {
|
||||||
private boolean isShowing = false;
|
private boolean isShowing = false;
|
||||||
@Override
|
@Override
|
||||||
public void updateDrawState(@NonNull TextPaint ds) {
|
public void updateDrawState(@NonNull TextPaint ds) {
|
||||||
if (isShowing) {
|
if (isShowing) {
|
||||||
super.updateDrawState(ds);
|
super.updateDrawState(ds);
|
||||||
ds.setColor(mCommentTextColor);
|
ds.setColor(markdownColor);
|
||||||
} else {
|
} else {
|
||||||
ds.bgColor = Color.BLACK;
|
ds.bgColor = Color.BLACK;
|
||||||
ds.setColor(mCommentTextColor);
|
ds.setColor(markdownColor);
|
||||||
}
|
}
|
||||||
ds.setUnderlineText(false);
|
ds.setUnderlineText(false);
|
||||||
}
|
}
|
||||||
@ -395,13 +386,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
|
|||||||
view.invalidate();
|
view.invalidate();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
spannableCopy.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
markdownStringBuilder.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
|
||||||
spannable = spannableCopy;
|
|
||||||
start = matcher.end() - 4;
|
start = matcher.end() - 4;
|
||||||
}
|
}
|
||||||
if (find) {
|
if (find) {
|
||||||
textView.setText(spannable);
|
textView.setText(markdownStringBuilder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,9 +4,15 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.ColorStateList;
|
import android.content.res.ColorStateList;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
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.SuperscriptSpan;
|
||||||
|
import android.text.style.URLSpan;
|
||||||
import android.text.util.Linkify;
|
import android.text.util.Linkify;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@ -27,6 +33,8 @@ import androidx.recyclerview.widget.DiffUtil;
|
|||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
@ -102,8 +110,65 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
|
|||||||
super(DIFF_CALLBACK);
|
super(DIFF_CALLBACK);
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mOauthRetrofit = oauthRetrofit;
|
mOauthRetrofit = oauthRetrofit;
|
||||||
|
mCommentColor = customThemeWrapper.getCommentColor();
|
||||||
mMarkwon = Markwon.builder(mContext)
|
mMarkwon = Markwon.builder(mContext)
|
||||||
.usePlugin(new AbstractMarkwonPlugin() {
|
.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
|
@Override
|
||||||
public void configureTheme(@NonNull MarkwonTheme.Builder builder) {
|
public void configureTheme(@NonNull MarkwonTheme.Builder builder) {
|
||||||
builder.linkColor(customThemeWrapper.getLinkColor());
|
builder.linkColor(customThemeWrapper.getLinkColor());
|
||||||
|
Loading…
Reference in New Issue
Block a user