1
0
mirror of https://github.com/tuskyapp/Tusky synced 2024-12-22 15:04:25 +01:00

Preserve the case of hashtags from content when possible (#4802)

https://social.tchncs.de/@KazuShuSora/113621293051641672
This commit is contained in:
Levi Bard 2024-12-11 17:53:32 +01:00 committed by GitHub
parent 328aca7031
commit c8d8891a07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -105,16 +105,38 @@ fun setClickableText(
trailingHashtagView?.visible(showHashtagBar)
if (showHashtagBar) {
trailingHashtagView?.apply {
text = SpannableStringBuilder().apply {
tags?.forEachIndexed { index, tag ->
val text = "#${tag.name}"
append(text, getCustomSpanForTag(text, tags, URLSpan(tag.url), listener), 0)
if (index != tags.lastIndex) {
append(" ")
}
}
}
trailingHashtagView?.apply { text = buildTrailingHashtagText(tags, trailingHashtags, listener) }
}
}
/**
* Build a spanned string containing trailing and out-of-band hashtags for the trailing hashtag view
* @param tagsFromServer The list of hashtags from the server
* @param trailingHashtagsFromContent The list of trailing hashtags scraped from the post content
* @param listener to notify about particular spans that are clicked
*/
private fun buildTrailingHashtagText(tagsFromServer: List<HashTag>?, trailingHashtagsFromContent: List<HashTag>, listener: LinkListener): SpannableStringBuilder {
return SpannableStringBuilder().apply {
// we apply the tags scraped from the content first to preserve the casing
// (tags from the server are often downcased)
val additionalTags = tagsFromServer?.let {
it.filter { serverTag -> trailingHashtagsFromContent.none { serverTag.name.equals(it.name, ignoreCase = true) } }
} ?: emptyList()
appendTags(trailingHashtagsFromContent.plus(additionalTags), listener)
}
}
/**
* Append space-separated url spans for a list of hashtags
* @param tags The tags to append
* @param listener to notify about particular spans that are clicked
*/
private fun SpannableStringBuilder.appendTags(tags: List<HashTag>, listener: LinkListener) {
tags.forEachIndexed { index, tag ->
val text = "#${tag.name}"
append(text, getCustomSpanForTag(text, tags, URLSpan(tag.url), listener), 0)
if (index != tags.lastIndex) {
append(" ")
}
}
}