优化搜索

pull/382/head
gedoor 4 years ago
parent a05b70293b
commit 24e8703663
  1. 19
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchListAdapter.kt
  2. 27
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchResult.kt

@ -1,21 +1,12 @@
package io.legado.app.ui.book.searchContent package io.legado.app.ui.book.searchContent
import android.content.Context import android.content.Context
import android.os.Build
import android.text.Html
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import androidx.core.text.HtmlCompat
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.getCompatColor import io.legado.app.utils.getCompatColor
import io.legado.app.utils.visible import io.legado.app.utils.hexString
import kotlinx.android.synthetic.main.item_bookmark.view.*
import kotlinx.android.synthetic.main.item_search_list.view.* import kotlinx.android.synthetic.main.item_search_list.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
@ -23,15 +14,15 @@ class SearchListAdapter(context: Context, val callback: Callback) :
SimpleRecyclerAdapter<SearchResult>(context, R.layout.item_search_list) { SimpleRecyclerAdapter<SearchResult>(context, R.layout.item_search_list) {
val cacheFileNames = hashSetOf<String>() val cacheFileNames = hashSetOf<String>()
val textColor = context.getCompatColor(R.color.primaryText).hexString.substring(2)
val accentColor = context.accentColor.hexString.substring(2)
override fun convert(holder: ItemViewHolder, item: SearchResult, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: SearchResult, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
val isDur = callback.durChapterIndex() == item.chapterIndex val isDur = callback.durChapterIndex() == item.chapterIndex
if (payloads.isEmpty()) { if (payloads.isEmpty()) {
tv_search_result.text = item.parseText(item.presentText) tv_search_result.text = item.getHtmlCompat(textColor, accentColor)
if (isDur){ tv_search_result.paint.isFakeBoldText = isDur
tv_search_result.paint.isFakeBoldText = true
}
} }
} }
} }

@ -2,11 +2,6 @@ package io.legado.app.ui.book.searchContent
import android.text.Spanned import android.text.Spanned
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import io.legado.app.App
import io.legado.app.R
import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.hexString
data class SearchResult( data class SearchResult(
var index: Int = 0, var index: Int = 0,
@ -20,16 +15,20 @@ data class SearchResult(
var newPosition: Int = 0, var newPosition: Int = 0,
var contentPosition: Int =0 var contentPosition: Int =0
) { ) {
val textColor = App.INSTANCE.getCompatColor(R.color.primaryText).hexString
val accentColor = App.INSTANCE.accentColor.hexString
val presentText: String fun getHtmlCompat(textColor: String, accentColor: String): Spanned {
get() { val html = colorPresentText(newPosition, query, text, textColor, accentColor) +
return colorPresentText(newPosition, query, text) +
"<font color=#${accentColor}>($chapterTitle)</font>" "<font color=#${accentColor}>($chapterTitle)</font>"
return HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY)
} }
private fun colorPresentText(position: Int, center: String, targetText: String): String { private fun colorPresentText(
position: Int,
center: String,
targetText: String,
textColor: String,
accentColor: String
): String {
val sub1 = text.substring(0, position) val sub1 = text.substring(0, position)
val sub2 = text.substring(position + center.length, targetText.length) val sub2 = text.substring(position + center.length, targetText.length)
return "<font color=#${textColor}>$sub1</font>" + return "<font color=#${textColor}>$sub1</font>" +
@ -37,10 +36,4 @@ data class SearchResult(
"<font color=#${textColor}>$sub2</font>" "<font color=#${textColor}>$sub2</font>"
} }
fun parseText(targetText: String): Spanned {
return HtmlCompat.fromHtml(targetText, HtmlCompat.FROM_HTML_MODE_LEGACY)
}
} }
Loading…
Cancel
Save