fix link parsing

This commit is contained in:
Matthieu 2021-11-28 19:02:25 +01:00
parent 58b668e09a
commit af63fc8990
1 changed files with 31 additions and 28 deletions

View File

@ -57,10 +57,10 @@ fun parseHTMLText(
context: Context, context: Context,
lifecycleScope: LifecycleCoroutineScope, lifecycleScope: LifecycleCoroutineScope,
) : Spanned { ) : Spanned {
//Convert text to spannable // Convert text to spannable
val content = fromHtml(text) val content = fromHtml(text)
//Retrive all links that should be made clickable // Retrieve all links that should be made clickable
val builder = SpannableStringBuilder(content) val builder = SpannableStringBuilder(content)
val urlSpans = content.getSpans(0, content.length, URLSpan::class.java) val urlSpans = content.getSpans(0, content.length, URLSpan::class.java)
@ -71,52 +71,55 @@ fun parseHTMLText(
val text = builder.subSequence(start, end) val text = builder.subSequence(start, end)
var customSpan: ClickableSpan? = null var customSpan: ClickableSpan? = null
//Handle hashtags // Handle hashtags
if (text[0] == '#') { if (text[0] == '#') {
val tag = text.subSequence(1, text.length).toString() val tag = text.subSequence(1, text.length).toString()
customSpan = object : ClickableSpanNoUnderline() { customSpan = object : ClickableSpanNoUnderline() {
override fun onClick(widget: View) { override fun onClick(widget: View) {
openTag(context, tag) openTag(context, tag)
} }
} }
builder.removeSpan(span)
} }
//Handle mentions // Handle mentions
if(text[0] == '@' && !mentions.isNullOrEmpty()) { else if(text[0] == '@') {
val accountUsername = text.subSequence(1, text.length).toString() if (!mentions.isNullOrEmpty()){
var id: String? = null val accountUsername = text.subSequence(1, text.length).toString()
var id: String? = null
//Go through all mentions stored in the status // Go through all mentions stored in the status
for (mention in mentions) { for (mention in mentions) {
if (mention.username.equals(accountUsername, ignoreCase = true) if (mention.username.equals(accountUsername, ignoreCase = true)
) { ) {
id = mention.id id = mention.id
//Mentions can be of users in other domains //Mentions can be of users in other domains
if (mention.url.contains(getDomain(span.url))) { if (mention.url.contains(getDomain(span.url))) {
break break
}
} }
} }
}
//Check that we found a user for the given mention // Check that we found a user for the given mention
if (id != null) { if (id != null) {
val accountId: String = id val accountId: String = id
customSpan = object : ClickableSpanNoUnderline() { customSpan = object : ClickableSpanNoUnderline() {
override fun onClick(widget: View) { override fun onClick(widget: View) {
Log.e("MENTION", "CLICKED")
//Retrieve the account for the given profile // Retrieve the account for the given profile
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setToCurrentUser() val api: PixelfedAPI = apiHolder.api ?: apiHolder.setToCurrentUser()
openAccountFromId(accountId, api, context) openAccountFromId(accountId, api, context)
}
} }
} }
} }
} }
builder.removeSpan(span)
} }
builder.removeSpan(span)
builder.setSpan(customSpan, start, end, flags) builder.setSpan(customSpan, start, end, flags)
// Add zero-width space after links in end of line to fix its too large hitbox. // Add zero-width space after links in end of line to fix its too large hitbox.