From bd02f5ef5ddfe7ac564f60305548ae927d33f204 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 30 Sep 2021 22:18:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/ui/dict/DictDialog.kt | 10 ++-------- .../java/io/legado/app/ui/widget/dialog/TextDialog.kt | 10 ++-------- .../main/java/io/legado/app/utils/ViewExtensions.kt | 11 +++++++++++ 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/dict/DictDialog.kt b/app/src/main/java/io/legado/app/ui/dict/DictDialog.kt index 1e64176b9..c4ba80759 100644 --- a/app/src/main/java/io/legado/app/ui/dict/DictDialog.kt +++ b/app/src/main/java/io/legado/app/ui/dict/DictDialog.kt @@ -1,8 +1,6 @@ package io.legado.app.ui.dict -import android.os.Build import android.os.Bundle -import android.text.Html import android.text.method.LinkMovementMethod import android.view.LayoutInflater import android.view.View @@ -12,6 +10,7 @@ import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.databinding.DialogDictBinding import io.legado.app.utils.invisible +import io.legado.app.utils.setHtml import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -53,12 +52,7 @@ class DictDialog() : BaseDialogFragment() { } viewModel.dictHtmlData.observe(viewLifecycleOwner) { binding.rotateLoading.invisible() - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - binding.tvDict.text = Html.fromHtml(it, Html.FROM_HTML_MODE_LEGACY) - } else { - @Suppress("DEPRECATION") - binding.tvDict.text = Html.fromHtml(it) - } + binding.tvDict.setHtml(it) } viewModel.dict(word) diff --git a/app/src/main/java/io/legado/app/ui/widget/dialog/TextDialog.kt b/app/src/main/java/io/legado/app/ui/widget/dialog/TextDialog.kt index 641064990..2e1a4ac7c 100644 --- a/app/src/main/java/io/legado/app/ui/widget/dialog/TextDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/dialog/TextDialog.kt @@ -1,14 +1,13 @@ package io.legado.app.ui.widget.dialog -import android.os.Build import android.os.Bundle -import android.text.Html import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.databinding.DialogTextViewBinding +import io.legado.app.utils.setHtml import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.windowSize import io.noties.markwon.Markwon @@ -70,12 +69,7 @@ class TextDialog() : BaseDialogFragment() { .build() .setMarkdown(binding.textView, content) } - Mode.HTML.name -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - binding.textView.text = Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY) - } else { - @Suppress("DEPRECATION") - binding.textView.text = Html.fromHtml(content) - } + Mode.HTML.name -> binding.textView.setHtml(content) else -> binding.textView.text = content } time = it.getLong("time", 0L) diff --git a/app/src/main/java/io/legado/app/utils/ViewExtensions.kt b/app/src/main/java/io/legado/app/utils/ViewExtensions.kt index cf40f519e..8c2660242 100644 --- a/app/src/main/java/io/legado/app/utils/ViewExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ViewExtensions.kt @@ -5,11 +5,13 @@ import android.content.Context import android.graphics.Bitmap import android.graphics.Canvas import android.os.Build +import android.text.Html import android.view.View import android.view.View.* import android.view.inputmethod.InputMethodManager import android.widget.RadioGroup import android.widget.SeekBar +import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.menu.MenuPopupHelper import androidx.appcompat.widget.PopupMenu @@ -103,6 +105,15 @@ fun RadioGroup.checkByIndex(index: Int) { check(get(index).id) } +fun TextView.setHtml(html: String) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + text = Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT) + } else { + @Suppress("DEPRECATION") + text = Html.fromHtml(html) + } +} + @SuppressLint("RestrictedApi") fun PopupMenu.show(x: Int, y: Int) { kotlin.runCatching {