use link icon instead of emoji when showing hidden urls (#4031)

Looks way better imho. Also closes #4028.

Before vs After:


![Screenshot_20230924_163714](https://github.com/tuskyapp/Tusky/assets/10157047/5b0b745a-4574-4e37-988e-b04997ac55f1)


![Screenshot_20230924_162657](https://github.com/tuskyapp/Tusky/assets/10157047/58a3482f-6afb-4b10-9891-f7a86af7f2bf)
This commit is contained in:
UlrichKu 2023-09-26 21:46:05 +02:00 committed by GitHub
commit 2dc27bca2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 62 additions and 67 deletions

View file

@ -34,6 +34,7 @@ import android.view.MotionEvent.ACTION_UP
import android.view.View
import android.widget.TextView
import androidx.annotation.VisibleForTesting
import androidx.appcompat.content.res.AppCompatResources
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.net.toUri
@ -43,6 +44,7 @@ import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.entity.HashTag
import com.keylesspalace.tusky.entity.Status.Mention
import com.keylesspalace.tusky.interfaces.LinkListener
import java.lang.ref.WeakReference
import java.net.URI
import java.net.URISyntaxException
@ -65,7 +67,7 @@ fun getDomain(urlString: String?): String {
* @param listener to notify about particular spans that are clicked
*/
fun setClickableText(view: TextView, content: CharSequence, mentions: List<Mention>, tags: List<HashTag>?, listener: LinkListener) {
val spannableContent = markupHiddenUrls(view.context, content)
val spannableContent = markupHiddenUrls(view, content)
view.text = spannableContent.apply {
getSpans(0, spannableContent.length, URLSpan::class.java).forEach {
@ -76,7 +78,7 @@ fun setClickableText(view: TextView, content: CharSequence, mentions: List<Menti
}
@VisibleForTesting
fun markupHiddenUrls(context: Context, content: CharSequence): SpannableStringBuilder {
fun markupHiddenUrls(view: TextView, content: CharSequence): SpannableStringBuilder {
val spannableContent = SpannableStringBuilder(content)
val originalSpans = spannableContent.getSpans(0, content.length, URLSpan::class.java)
val obscuredLinkSpans = originalSpans.filter {
@ -99,8 +101,22 @@ fun markupHiddenUrls(context: Context, content: CharSequence): SpannableStringBu
val start = spannableContent.getSpanStart(span)
val end = spannableContent.getSpanEnd(span)
val originalText = spannableContent.subSequence(start, end)
val replacementText = context.getString(R.string.url_domain_notifier, originalText, getDomain(span.url))
val replacementText = view.context.getString(R.string.url_domain_notifier, originalText, getDomain(span.url))
spannableContent.replace(start, end, replacementText) // this also updates the span locations
val linkDrawable = AppCompatResources.getDrawable(view.context, R.drawable.ic_link)!!
// ImageSpan does not always align the icon correctly in the line, let's use our custom emoji span for this
val linkDrawableSpan = EmojiSpan(WeakReference(view))
linkDrawableSpan.imageDrawable = linkDrawable
val placeholderIndex = replacementText.indexOf("🔗")
spannableContent.setSpan(
linkDrawableSpan,
start + placeholderIndex,
start + placeholderIndex + "🔗".length,
0
)
}
return spannableContent