fix: cleaner spoiler text in comments

This commit is contained in:
rebelonion 2024-03-19 17:09:34 -05:00
parent ca2409ef91
commit a39db5ea93
6 changed files with 6 additions and 65 deletions

View file

@ -92,6 +92,7 @@ import ani.dantotsu.connections.anilist.api.FuzzyDate
import ani.dantotsu.connections.crashlytics.CrashlyticsInterface
import ani.dantotsu.databinding.ItemCountDownBinding
import ani.dantotsu.media.Media
import ani.dantotsu.others.SpoilerPlugin
import ani.dantotsu.parsers.ShowResponse
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
@ -1247,6 +1248,7 @@ fun buildMarkwon(
.usePlugin(StrikethroughPlugin.create())
.usePlugin(TablePlugin.create(activity))
.usePlugin(TaskListPlugin.create(activity))
.usePlugin(SpoilerPlugin())
.usePlugin(HtmlPlugin.create { plugin ->
if (userInputContent) {
plugin.addHandler(

View file

@ -35,7 +35,7 @@ class CharacterDetailsAdapter(private val character: Character, private val acti
binding.characterDesc.isTextSelectable
val markWon = Markwon.builder(activity).usePlugin(SoftBreakAddsNewLinePlugin.create())
.usePlugin(SpoilerPlugin()).build()
markWon.setMarkdown(binding.characterDesc, desc)
markWon.setMarkdown(binding.characterDesc, desc.replace("~!", "||").replace("!~", "||"))
}

View file

@ -59,10 +59,8 @@ class CommentItem(val comment: Comment,
viewBinding.commentRepliesList.layoutManager = LinearLayoutManager(commentsFragment.activity)
viewBinding.commentRepliesList.adapter = adapter
val isUserComment = CommentsAPI.userId == comment.userId
val node = markwon.parse(comment.content)
val spanned = markwon.render(node)
val levelColor = getAvatarColor(comment.totalVotes, backgroundColor)
markwon.setParsedMarkdown(viewBinding.commentText, viewBinding.commentText.setSpoilerText(spanned, markwon))
markwon.setMarkdown(viewBinding.commentText, comment.content)
viewBinding.commentDelete.visibility = if (isUserComment || CommentsAPI.isAdmin || CommentsAPI.isMod) View.VISIBLE else View.GONE
viewBinding.commentBanUser.visibility = if ((CommentsAPI.isAdmin || CommentsAPI.isMod) && !isUserComment) View.VISIBLE else View.GONE
viewBinding.commentReport.visibility = if (!isUserComment) View.VISIBLE else View.GONE

View file

@ -1,59 +0,0 @@
package ani.dantotsu.media.comments
import android.content.Context
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import io.noties.markwon.Markwon
class SpoilerTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {
private val replaceWith = ""
private var originalSpanned: SpannableStringBuilder? = null
fun setSpoilerText(text: Spanned, markwon: Markwon) : Spanned {
val pattern = Regex("\\|\\|(.*?)\\|\\|")
val matcher = pattern.toPattern().matcher(text)
val spannableBuilder = SpannableStringBuilder(text)
//remove the "||" from the text
val originalBuilder = SpannableStringBuilder(text)
originalSpanned = originalBuilder
val map = mutableMapOf<Int, Int>()
while (matcher.find()) {
val start = matcher.start()
val end = matcher.end()
map[start] = end
}
map.forEach { (start, end) ->
val replacement = replaceWith.repeat(end - start)
spannableBuilder.replace(start, end, replacement)
}
val spannableString = SpannableString(spannableBuilder)
map.forEach { (start, end) ->
val clickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
markwon.setParsedMarkdown(this@SpoilerTextView, originalSpanned!!.delete(end - 2, end).delete(start, start + 2))
}
}
spannableString.setSpan(
clickableSpan,
start,
end,
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
movementMethod = LinkMovementMethod.getInstance()
return spannableString
}
}

View file

@ -44,7 +44,7 @@ class SpoilerPlugin : AbstractMarkwonPlugin() {
}
companion object {
private val RE = Pattern.compile("~!.+?!~")
private val RE = Pattern.compile("\\|\\|.+?\\|\\|")
private fun applySpoilerSpans(spannable: Spannable) {
val text = spannable.toString()
val matcher = RE.matcher(text)

View file

@ -148,7 +148,7 @@
tools:ignore="ContentDescription,RtlSymmetry" />
</LinearLayout>
<ani.dantotsu.media.comments.SpoilerTextView
<TextView
android:id="@+id/commentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"