From b9c1b00ae53714d4a532adf5b62377887e7b5df2 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 16 Jan 2023 18:30:45 +0530 Subject: [PATCH] Use parseAsHtml() extension. --- .../github/libretube/ui/adapters/CommentsAdapter.kt | 7 ++----- .../github/libretube/ui/extensions/SetFormattedHtml.kt | 10 ---------- .../github/libretube/ui/fragments/PlayerFragment.kt | 10 +++------- .../main/java/com/github/libretube/util/LinkHandler.kt | 5 +++-- .../main/java/com/github/libretube/util/ThemeHelper.kt | 7 +++---- 5 files changed, 11 insertions(+), 28 deletions(-) delete mode 100644 app/src/main/java/com/github/libretube/ui/extensions/SetFormattedHtml.kt diff --git a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt index ec33f5cf5..aa72c25f0 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt @@ -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() diff --git a/app/src/main/java/com/github/libretube/ui/extensions/SetFormattedHtml.kt b/app/src/main/java/com/github/libretube/ui/extensions/SetFormattedHtml.kt deleted file mode 100644 index bd9b0ee7d..000000000 --- a/app/src/main/java/com/github/libretube/ui/extensions/SetFormattedHtml.kt +++ /dev/null @@ -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) -} diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 1e2e3b0d0..8d4d938db 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -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 diff --git a/app/src/main/java/com/github/libretube/util/LinkHandler.kt b/app/src/main/java/com/github/libretube/util/LinkHandler.kt index ebe2af6d8..f7e424568 100644 --- a/app/src/main/java/com/github/libretube/util/LinkHandler.kt +++ b/app/src/main/java/com/github/libretube/util/LinkHandler.kt @@ -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) { diff --git a/app/src/main/java/com/github/libretube/util/ThemeHelper.kt b/app/src/main/java/com/github/libretube/util/ThemeHelper.kt index 21ea7c24a..b75544bd8 100644 --- a/app/src/main/java/com/github/libretube/util/ThemeHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ThemeHelper.kt @@ -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( - "LibreTube", - HtmlCompat.FROM_HTML_MODE_COMPACT - ) + return "LibreTube" + .parseAsHtml(HtmlCompat.FROM_HTML_MODE_COMPACT) } }