Skip to content

Commit

Permalink
fix: Show content warning changes when viewing status edits
Browse files Browse the repository at this point in the history
Previous code only ran the status content through the diff engine, so
changes to the content warning / spoiler text were ignored.

Fixes #1144
  • Loading branch information
nikclayton committed Dec 2, 2024
1 parent 7882f7d commit c33300b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.google.android.material.color.MaterialColors
import org.xml.sax.XMLReader

class ViewEditsAdapter(
context: Context,
private val edits: List<StatusEdit>,
private val animateEmojis: Boolean,
private val useBlurhash: Boolean,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class ViewEditsFragment :
binding.initialProgressBar.hide()

binding.recyclerView.adapter = ViewEditsAdapter(
context = requireContext(),
edits = uiState.edits,
animateEmojis = animateEmojis,
useBlurhash = useBlurhash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,30 @@ 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("<div>${sortedEdits[0].spoilerText}</div>")
var previousSpoilerText = loader.load("<div>${sortedEdits[1].spoilerText}</div>")

// The XML processor expects `br` to be closed
var currentContent =
loader.load(sortedEdits[0].content.replace("<br>", "<br/>"))
var previousContent =
loader.load(sortedEdits[1].content.replace("<br>", "<br/>"))

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("<div/>"),
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("<br>", "<br/>"),
Expand Down

0 comments on commit c33300b

Please sign in to comment.