From cf461906df9bf142917908a18bc6e905d209d4cf Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Mon, 2 Dec 2024 14:53:57 +0100 Subject: [PATCH] fix: Show content warning changes when viewing status edits (#1148) Previous code only ran the status content through the diff engine, so changes to the content warning / spoiler text were ignored. Fixes #1144 --- .../viewthread/edits/ViewEditsAdapter.kt | 17 +++++++++++------ .../viewthread/edits/ViewEditsFragment.kt | 1 + .../viewthread/edits/ViewEditsViewModel.kt | 14 +++++++++++--- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsAdapter.kt b/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsAdapter.kt index 55c3c5bcc..1d7de3b44 100644 --- a/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsAdapter.kt +++ b/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsAdapter.kt @@ -41,6 +41,7 @@ import com.google.android.material.color.MaterialColors import org.xml.sax.XMLReader class ViewEditsAdapter( + context: Context, private val edits: List, private val animateEmojis: Boolean, private val useBlurhash: Boolean, @@ -55,6 +56,8 @@ class ViewEditsAdapter( /** Size of medium text in this theme, in px */ private var mediumTextSizePx: Float = 0f + private val pachliTagHandler = PachliTagHandler(context) + override fun onCreateViewHolder( parent: ViewGroup, viewType: Int, @@ -108,16 +111,18 @@ class ViewEditsAdapter( } else { binding.statusEditContentWarningDescription.show() binding.statusEditContentWarningSeparator.show() - binding.statusEditContentWarningDescription.text = edit.spoilerText.emojify( - edit.emojis, - binding.statusEditContentWarningDescription, - animateEmojis, - ) + binding.statusEditContentWarningDescription.text = edit.spoilerText + .parseAsMastodonHtml(pachliTagHandler) + .emojify( + edit.emojis, + binding.statusEditContentWarningDescription, + animateEmojis, + ) } val emojifiedText = edit .content - .parseAsMastodonHtml(PachliTagHandler(context)) + .parseAsMastodonHtml(pachliTagHandler) .emojify(edit.emojis, binding.statusEditContent, animateEmojis) setClickableText(binding.statusEditContent, emojifiedText, emptyList(), emptyList(), listener) diff --git a/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsFragment.kt b/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsFragment.kt index 67b5644ac..1d9434de3 100644 --- a/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsFragment.kt +++ b/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsFragment.kt @@ -138,6 +138,7 @@ class ViewEditsFragment : binding.initialProgressBar.hide() binding.recyclerView.adapter = ViewEditsAdapter( + context = requireContext(), edits = uiState.edits, animateEmojis = animateEmojis, useBlurhash = useBlurhash, diff --git a/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsViewModel.kt b/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsViewModel.kt index 45338ca52..cbc842de6 100644 --- a/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsViewModel.kt +++ b/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsViewModel.kt @@ -105,9 +105,13 @@ class ViewEditsViewModel @Inject constructor(private val api: MastodonApi) : Vie ) val processor = OptimisticXMLProcessor() processor.setCoalesce(true) - val output = HtmlDiffOutput() + val spoilerDiff = HtmlDiffOutput() + val contentDiff = HtmlDiffOutput() try { + var currentSpoilerText = loader.load("
${sortedEdits[0].spoilerText}
") + var previousSpoilerText = loader.load("
${sortedEdits[1].spoilerText}
") + // The XML processor expects `br` to be closed var currentContent = loader.load(sortedEdits[0].content.replace("
", "
")) @@ -115,12 +119,16 @@ class ViewEditsViewModel @Inject constructor(private val api: MastodonApi) : Vie loader.load(sortedEdits[1].content.replace("
", "
")) for (i in 1 until sortedEdits.size) { - processor.diff(previousContent, currentContent, output) + processor.diff(previousSpoilerText, currentSpoilerText, spoilerDiff) + processor.diff(previousContent, currentContent, contentDiff) sortedEdits[i - 1] = sortedEdits[i - 1].copy( - content = output.xml.toString(), + spoilerText = spoilerDiff.xml.toString().removePrefix("
"), + content = contentDiff.xml.toString(), ) if (i < sortedEdits.size - 1) { + currentSpoilerText = previousSpoilerText + previousSpoilerText = loader.load(sortedEdits[i + 1].spoilerText) currentContent = previousContent previousContent = loader.load( sortedEdits[i + 1].content.replace("
", "
"),