正文搜索百分比 #300

This commit is contained in:
HapeLee
2025-11-13 20:34:24 +08:00
parent 3ecda7d068
commit 1ed159b016
4 changed files with 73 additions and 31 deletions
@@ -211,13 +211,23 @@ class SearchContentActivity :
kotlin.runCatching {
appDb.bookChapterDao.getChapterList(viewModel.bookUrl).forEach { bookChapter ->
ensureActive()
val totalChapters = viewModel.book?.totalChapterNum
?: appDb.bookChapterDao.getChapterCount(viewModel.bookUrl)
val searchResults = if (isLocalBook
|| viewModel.cacheChapterNames.contains(bookChapter.getFileName())
) {
viewModel.searchChapter(query, bookChapter)
viewModel.searchChapter(query, bookChapter).map { result ->
if (totalChapters > 0) {
result.copy(progressPercent = (bookChapter.index + 1).toFloat() / totalChapters * 100f)
} else {
result
}
}
} else {
return@forEach
}
ensureActive()
if (searchResults.isNotEmpty()) {
viewModel.searchResultList.addAll(searchResults)
@@ -2,6 +2,7 @@ package io.legado.app.ui.book.searchContent
import android.content.Context
import android.view.ViewGroup
import androidx.core.view.isVisible
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.databinding.ItemSearchListBinding
@@ -31,6 +32,13 @@ class SearchContentAdapter(context: Context, val callback: Callback) :
tvTitle.text = item.getTitleSpannable(textColorInt)
tvContent.text = item.getContentSpannable(textColorInt, accentColorInt, bgColorInt)
if (item.progressPercent > 0) {
cdCount.isVisible = true
tvCount.text = String.format("%.1f%%", item.progressPercent)
} else {
tvCount.isVisible = false
}
cdRoot.setCardBackgroundColor(
if (isDur) context.themeColor(com.google.android.material.R.attr.colorSurfaceContainer)
else context.themeColor(com.google.android.material.R.attr.colorSurface)
@@ -20,7 +20,8 @@ data class SearchResult(
val chapterIndex: Int = 0,
val pageIndex: Int = 0,
val queryIndexInResult: Int = 0,
val queryIndexInChapter: Int = 0
val queryIndexInChapter: Int = 0,
val progressPercent: Float = 0f
) {
fun getTitleSpannable(accentColor: Int): SpannableString {