HapeLee 1 год назад
Родитель
Сommit
1ed159b016

+ 11 - 1
app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt

@@ -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)

+ 8 - 0
app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentAdapter.kt

@@ -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)

+ 2 - 1
app/src/main/java/io/legado/app/ui/book/searchContent/SearchResult.kt

@@ -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 {

+ 51 - 28
app/src/main/res/layout/item_search_list.xml

@@ -1,45 +1,68 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.google.android.material.listitem.ListItemCardView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
-    android:orientation="vertical"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/cd_root"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
+    android:checkable="true"
+    app:cardCornerRadius="16dp"
     android:layout_margin="8dp">
-    
-    <com.google.android.material.listitem.ListItemCardView
-        android:id="@+id/cd_root"
+
+    <androidx.constraintlayout.widget.ConstraintLayout
+        android:orientation="vertical"
+        android:padding="8dp"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:checkable="true"
-        app:cardCornerRadius="16dp">
+        android:layout_height="wrap_content">
 
-        <LinearLayout
-            android:orientation="vertical"
-            android:padding="8dp"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content">
+        <TextView
+            android:id="@+id/tvTitle"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:paddingBottom="2dp"
+            android:textColor="?attr/colorOnSurface"
+            android:textStyle="bold"
+            android:textSize="14sp"
+            app:layout_constraintStart_toStartOf="parent"
+            app:layout_constraintTop_toTopOf="parent"
+            app:layout_constraintEnd_toStartOf="@id/cd_count"/>
+
+        <com.google.android.material.card.MaterialCardView
+            android:id="@+id/cd_count"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:visibility="visible"
+            app:cardCornerRadius="8dp"
+            app:strokeWidth="0dp"
+            app:cardBackgroundColor="?attr/colorSecondaryVariant"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintTop_toTopOf="parent">
 
             <TextView
-                android:id="@+id/tvTitle"
-                android:layout_width="match_parent"
+                android:id="@+id/tv_count"
+                android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
-                android:paddingBottom="2dp"
-                android:textColor="?attr/colorOnSurface"
+                android:textColor="?attr/colorOnSecondary"
+                android:textSize="10sp"
+                android:padding="4dp"
                 android:textStyle="bold"
-                android:textSize="14sp"/>
+                android:includeFontPadding="false"
+                tools:text="5"
+                tools:ignore="SmallSp" />
 
-            <TextView
-                android:id="@+id/tvContent"
-                android:layout_width="match_parent"
-                android:layout_height="wrap_content"
-                android:textColor="?attr/colorOnSurface"
-                android:textSize="13sp"
-                android:lineSpacingExtra="2dp"/>
+        </com.google.android.material.card.MaterialCardView>
 
-        </LinearLayout>
+        <TextView
+            android:id="@+id/tvContent"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:textColor="?attr/colorOnSurface"
+            android:textSize="13sp"
+            android:lineSpacingExtra="2dp"
+            app:layout_constraintTop_toBottomOf="@id/tvTitle"/>
 
-    </com.google.android.material.listitem.ListItemCardView>
+    </androidx.constraintlayout.widget.ConstraintLayout>
 
+</com.google.android.material.listitem.ListItemCardView>
 
 
-</LinearLayout>