Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve the case of hashtags from content when possible #4802

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ")
}
}
}
Expand Down
Loading