|
|
@@ -1,7 +1,13 @@
|
|
|
package io.legado.app.ui.book.searchContent
|
|
|
|
|
|
-import android.text.Spanned
|
|
|
-import androidx.core.text.HtmlCompat
|
|
|
+import android.graphics.Typeface
|
|
|
+import android.text.Spannable
|
|
|
+import android.text.SpannableString
|
|
|
+import android.text.SpannableStringBuilder
|
|
|
+import android.text.style.BackgroundColorSpan
|
|
|
+import android.text.style.ForegroundColorSpan
|
|
|
+import android.text.style.StyleSpan
|
|
|
+import android.text.style.UnderlineSpan
|
|
|
import io.legado.app.help.config.AppConfig
|
|
|
|
|
|
data class SearchResult(
|
|
|
@@ -17,44 +23,48 @@ data class SearchResult(
|
|
|
val queryIndexInChapter: Int = 0
|
|
|
) {
|
|
|
|
|
|
- fun getHtmlCompat(textColor: String, accentColor: String): Spanned {
|
|
|
- return if (query.isNotBlank()) {
|
|
|
- val queryIndexInSurrounding = resultText.indexOf(query)
|
|
|
- val leftString = resultText.substring(0, queryIndexInSurrounding)
|
|
|
- val rightString =
|
|
|
- resultText.substring(queryIndexInSurrounding + query.length, resultText.length)
|
|
|
- // 检查是否为墨水屏模式
|
|
|
- val html = if (AppConfig.isEInkMode) {
|
|
|
- // 墨水屏模式:使用下划线
|
|
|
- buildString {
|
|
|
- append("<u>${chapterTitle}</u>")
|
|
|
- append("<br>")
|
|
|
- append(leftString)
|
|
|
- append("<u>${query}</u>")
|
|
|
- append(rightString)
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 普通模式:使用颜色
|
|
|
- buildString {
|
|
|
- append(chapterTitle.colorTextForHtml(accentColor))
|
|
|
- append("<br>")
|
|
|
- append(leftString.colorTextForHtml(textColor))
|
|
|
- append(query.colorTextForHtml(accentColor))
|
|
|
- append(rightString.colorTextForHtml(textColor))
|
|
|
- }
|
|
|
+ fun getTitleSpannable(accentColor: Int): SpannableString {
|
|
|
+ return if (AppConfig.isEInkMode) {
|
|
|
+ SpannableString(chapterTitle).apply {
|
|
|
+ setSpan(UnderlineSpan(), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
}
|
|
|
- HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY)
|
|
|
} else {
|
|
|
- val html = if (AppConfig.isEInkMode) {
|
|
|
- resultText
|
|
|
- } else {
|
|
|
- resultText.colorTextForHtml(textColor)
|
|
|
+ SpannableString(chapterTitle).apply {
|
|
|
+ setSpan(ForegroundColorSpan(accentColor), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
}
|
|
|
- HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun String.colorTextForHtml(textColor: String) =
|
|
|
- "<font color=#${textColor}>$this</font>"
|
|
|
+ fun getContentSpannable(textColor: Int, accentColor: Int, bgColor: Int): SpannableStringBuilder {
|
|
|
+ val spannable = SpannableStringBuilder(resultText)
|
|
|
+
|
|
|
+ if (query.isNotBlank()) {
|
|
|
+ var searchStart = 0
|
|
|
+ while (true) {
|
|
|
+ val start = resultText.indexOf(query, searchStart, ignoreCase = true)
|
|
|
+ if (start == -1) break
|
|
|
+ val end = start + query.length
|
|
|
|
|
|
-}
|
|
|
+ if (AppConfig.isEInkMode) {
|
|
|
+ spannable.setSpan(UnderlineSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
+ } else {
|
|
|
+ // 字体加粗
|
|
|
+ spannable.setSpan(StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
+ // 字体颜色
|
|
|
+ spannable.setSpan(ForegroundColorSpan(accentColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
+ // 背景色
|
|
|
+ spannable.setSpan(BackgroundColorSpan(bgColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
+ }
|
|
|
+
|
|
|
+ searchStart = end
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置整体字体颜色(正文)
|
|
|
+ if (!AppConfig.isEInkMode) {
|
|
|
+ spannable.setSpan(ForegroundColorSpan(textColor), 0, spannable.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
+ }
|
|
|
+
|
|
|
+ return spannable
|
|
|
+ }
|
|
|
+}
|