Use parseAsHtml() extension.

This commit is contained in:
Isira Seneviratne 2023-01-16 18:30:45 +05:30
parent 2e0c121f58
commit b9c1b00ae5
5 changed files with 11 additions and 28 deletions

View File

@ -7,7 +7,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.core.text.HtmlCompat
import androidx.core.text.parseAsHtml
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
@ -67,10 +67,7 @@ class CommentsAdapter(
}
commentInfos.text = comment.author + TextUtils.SEPARATOR + comment.commentedTime
commentText.text = HtmlCompat.fromHtml(
comment.commentText,
HtmlCompat.FROM_HTML_MODE_LEGACY
)
commentText.text = comment.commentText.parseAsHtml()
ImageHelper.loadImage(comment.thumbnail, commentorImage)
likesTextView.text = comment.likeCount.formatShort()

View File

@ -1,10 +0,0 @@
package com.github.libretube.ui.extensions
import android.text.util.Linkify
import android.widget.TextView
import androidx.core.text.HtmlCompat
fun TextView.setFormattedHtml(text: String) {
Linkify.addLinks(this, Linkify.WEB_URLS)
this.text = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY)
}

View File

@ -31,7 +31,7 @@ import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.net.toUri
import androidx.core.os.ConfigurationCompat
import androidx.core.os.bundleOf
import androidx.core.text.HtmlCompat
import androidx.core.text.parseAsHtml
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
@ -1033,12 +1033,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// detect whether the description is html formatted
if (description.contains("<") && description.contains(">")) {
descTextView.movementMethod = LinkMovementMethod.getInstance()
descTextView.text = HtmlCompat.fromHtml(
description,
HtmlCompat.FROM_HTML_MODE_LEGACY,
null,
HtmlParser(LinkHandler { link -> handleLink(link) })
)
descTextView.text = description
.parseAsHtml(tagHandler = HtmlParser(LinkHandler(this::handleLink)))
} else {
// Links can be present as plain text
descTextView.autoLinkMask = Linkify.WEB_URLS

View File

@ -8,10 +8,11 @@ import android.view.View
import org.xml.sax.Attributes
class LinkHandler(
private val clickCallback: ((String) -> Unit)?
private val clickCallback: (String) -> Unit
) {
private var linkTagStartIndex = -1
private var link: String? = null
fun handleTag(
opening: Boolean,
tag: String?,
@ -38,7 +39,7 @@ class LinkHandler(
output.setSpan(
object : ClickableSpan() {
override fun onClick(widget: View) {
clickCallback?.invoke(link)
clickCallback(link)
}
override fun updateDrawState(ds: TextPaint) {

View File

@ -9,6 +9,7 @@ import android.util.TypedValue
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.text.HtmlCompat
import androidx.core.text.parseAsHtml
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.ui.adapters.IconsSheetAdapter
@ -133,9 +134,7 @@ object ThemeHelper {
fun getStyledAppName(context: Context): Spanned {
val colorPrimary = getThemeColor(context, R.attr.colorPrimaryDark)
val hexColor = String.format("#%06X", (0xFFFFFF and colorPrimary))
return HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
return "Libre<span style='color:$hexColor';>Tube</span>"
.parseAsHtml(HtmlCompat.FROM_HTML_MODE_COMPACT)
}
}