diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-09-14 02:34:11 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-09-14 02:34:11 +0000 |
commit | 4870d2bee64e454faf0f6a43269d0e00aaba52e7 (patch) | |
tree | b1efb85e7cb6797352d2e627125f0507bfa9db3a /app/src | |
parent | acaece6e3e63fba9f7a38899e3fb3079c9dd12c0 (diff) | |
download | infinity-for-reddit-4870d2bee64e454faf0f6a43269d0e00aaba52e7.tar infinity-for-reddit-4870d2bee64e454faf0f6a43269d0e00aaba52e7.tar.gz infinity-for-reddit-4870d2bee64e454faf0f6a43269d0e00aaba52e7.tar.bz2 infinity-for-reddit-4870d2bee64e454faf0f6a43269d0e00aaba52e7.tar.lz infinity-for-reddit-4870d2bee64e454faf0f6a43269d0e00aaba52e7.tar.xz infinity-for-reddit-4870d2bee64e454faf0f6a43269d0e00aaba52e7.tar.zst infinity-for-reddit-4870d2bee64e454faf0f6a43269d0e00aaba52e7.zip |
Fix spoiler background color that cannot hide spoiler. Support spoiler in MessageRecyclerViewAdapter.
Diffstat (limited to '')
4 files changed, 89 insertions, 15 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java index 1dde280f..0cf15283 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java @@ -114,6 +114,7 @@ public class CommentFullMarkdownActivity extends BaseActivity { String commentMarkdown = getIntent().getStringExtra(EXTRA_COMMENT_MARKDOWN); boolean isNsfw = getIntent().getBooleanExtra(EXTRA_IS_NSFW, false); int markdownColor = mCustomThemeWrapper.getCommentColor(); + int spoilerBackgroundColor = markdownColor | 0xFF000000; int linkColor = mCustomThemeWrapper.getLinkColor(); Markwon markwon = Markwon.builder(this) .usePlugin(new AbstractMarkwonPlugin() { @@ -149,7 +150,7 @@ public class CommentFullMarkdownActivity extends BaseActivity { super.updateDrawState(ds); ds.setColor(markdownColor); } else { - ds.bgColor = markdownColor; + ds.bgColor = spoilerBackgroundColor; ds.setColor(markdownColor); } ds.setUnderlineText(false); 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 fec7839a..9298c0b2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -250,7 +250,9 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy mGlide = glide; mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor(); mCommentTextColor = customThemeWrapper.getCommentColor(); + int commentSpoilerBackgroundColor = mCommentTextColor | 0xFF000000; int markdownColor = customThemeWrapper.getPostContentColor(); + int postSpoilerBackgroundColor = markdownColor | 0xFF000000; int linkColor = customThemeWrapper.getLinkColor(); mPostDetailMarkwon = Markwon.builder(mActivity) .usePlugin(new AbstractMarkwonPlugin() { @@ -286,7 +288,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy super.updateDrawState(ds); ds.setColor(markdownColor); } else { - ds.bgColor = markdownColor; + ds.bgColor = postSpoilerBackgroundColor; ds.setColor(markdownColor); } ds.setUnderlineText(false); @@ -382,7 +384,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy super.updateDrawState(ds); ds.setColor(mCommentTextColor); } else { - ds.bgColor = mCommentTextColor; + ds.bgColor = commentSpoilerBackgroundColor; ds.setColor(mCommentTextColor); } ds.setUnderlineText(false); @@ -3657,7 +3659,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy ((ViewPostDetailActivity) mActivity).changeToSingleThreadMode(); } - itemView.setBackgroundColor(mCommentBackgroundColor); + itemView.setBackgroundColor(mColorAccent); ((TextView) itemView).setTextColor(mColorAccent); }); } 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 ccfc2a88..eb37f28a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java @@ -111,6 +111,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment mContext = context; mOauthRetrofit = oauthRetrofit; mCommentColor = customThemeWrapper.getCommentColor(); + int commentSpoilerBackgroundColor = mCommentColor | 0xFF000000; mMarkwon = Markwon.builder(mContext) .usePlugin(new AbstractMarkwonPlugin() { @NonNull @@ -147,7 +148,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment super.updateDrawState(ds); ds.setColor(mCommentColor); } else { - ds.bgColor = mCommentColor; + ds.bgColor = commentSpoilerBackgroundColor; ds.setColor(mCommentColor); } ds.setUnderlineText(false); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MessageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MessageRecyclerViewAdapter.java index d088a938..58604abd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MessageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MessageRecyclerViewAdapter.java @@ -3,7 +3,13 @@ package ml.docilealligator.infinityforreddit.Adapter; import android.content.Context; import android.content.Intent; import android.content.res.ColorStateList; +import android.graphics.Color; import android.net.Uri; +import android.text.SpannableStringBuilder; +import android.text.Spanned; +import android.text.TextPaint; +import android.text.method.LinkMovementMethod; +import android.text.style.ClickableSpan; import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.view.LayoutInflater; @@ -19,6 +25,8 @@ import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import butterknife.BindView; import butterknife.ButterKnife; @@ -79,8 +87,73 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl mContext = context; mOauthRetrofit = oauthRetrofit; mRetryLoadingMoreCallback = retryLoadingMoreCallback; + + mColorAccent = customThemeWrapper.getColorAccent(); + mMessageBackgroundColor = customThemeWrapper.getCardViewBackgroundColor(); + mUsernameColor = customThemeWrapper.getUsername(); + mPrimaryTextColor = customThemeWrapper.getPrimaryTextColor(); + mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor(); + int spoilerBackgroundColor = mSecondaryTextColor | 0xFF000000; + mUnreadMessageBackgroundColor = customThemeWrapper.getUnreadMessageBackgroundColor(); + mColorPrimaryLightTheme = customThemeWrapper.getColorPrimaryLightTheme(); + mButtonTextColor = customThemeWrapper.getButtonTextColor(); + 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(mSecondaryTextColor); + } else { + ds.bgColor = spoilerBackgroundColor; + ds.setColor(mSecondaryTextColor); + } + ds.setUnderlineText(false); + } + + @Override + public void onClick(@NonNull View view) { + 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 configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { builder.linkResolver((view, link) -> { @@ -115,15 +188,6 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl } else { mMessageType = FetchMessage.MESSAGE_TYPE_INBOX; } - - mColorAccent = customThemeWrapper.getColorAccent(); - mMessageBackgroundColor = customThemeWrapper.getCardViewBackgroundColor(); - mUsernameColor = customThemeWrapper.getUsername(); - mPrimaryTextColor = customThemeWrapper.getPrimaryTextColor(); - mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor(); - mUnreadMessageBackgroundColor = customThemeWrapper.getUnreadMessageBackgroundColor(); - mColorPrimaryLightTheme = customThemeWrapper.getColorPrimaryLightTheme(); - mButtonTextColor = customThemeWrapper.getButtonTextColor(); } @NonNull @@ -205,7 +269,11 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl mContext.startActivity(intent); }); - ((DataViewHolder) holder).contentCustomMarkwonView.setOnClickListener(view -> ((DataViewHolder) holder).itemView.performClick()); + ((DataViewHolder) holder).contentCustomMarkwonView.setOnClickListener(view -> { + if (((DataViewHolder) holder).contentCustomMarkwonView.getSelectionStart() == -1 && ((DataViewHolder) holder).contentCustomMarkwonView.getSelectionEnd() == -1) { + ((DataViewHolder) holder).itemView.performClick(); + } + }); } } } @@ -293,6 +361,8 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl subjectTextView.setTextColor(mPrimaryTextColor); titleTextView.setTextColor(mPrimaryTextColor); contentCustomMarkwonView.setTextColor(mSecondaryTextColor); + + contentCustomMarkwonView.setMovementMethod(LinkMovementMethod.getInstance()); } } |