kunfei 4 лет назад
Родитель
Сommit
7f11b4e1c7

+ 1 - 0
app/src/main/java/io/legado/app/ui/book/bookmark/AllBookmarkActivity.kt

@@ -22,6 +22,7 @@ class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookma
     }
 
     private fun initView() {
+        binding.recyclerView.addItemDecoration(BookmarkDecoration(adapter))
         binding.recyclerView.adapter = adapter
     }
 

+ 16 - 0
app/src/main/java/io/legado/app/ui/book/bookmark/BookmarkAdapter.kt

@@ -6,6 +6,7 @@ import io.legado.app.base.adapter.ItemViewHolder
 import io.legado.app.base.adapter.RecyclerAdapter
 import io.legado.app.data.entities.Bookmark
 import io.legado.app.databinding.ItemBookmarkBinding
+import io.legado.app.utils.gone
 
 class BookmarkAdapter(context: Context) : RecyclerAdapter<Bookmark, ItemBookmarkBinding>(context) {
 
@@ -20,7 +21,9 @@ class BookmarkAdapter(context: Context) : RecyclerAdapter<Bookmark, ItemBookmark
         payloads: MutableList<Any>
     ) {
         binding.tvChapterName.text = item.chapterName
+        binding.tvBookText.gone(item.bookText.isEmpty())
         binding.tvBookText.text = item.bookText
+        binding.tvContent.gone(item.content.isEmpty())
         binding.tvContent.text = item.content
     }
 
@@ -28,5 +31,18 @@ class BookmarkAdapter(context: Context) : RecyclerAdapter<Bookmark, ItemBookmark
 
     }
 
+    fun getHeaderText(position: Int): String {
+        return with(getItem(position)) {
+            "${this?.bookName ?: ""}(${this?.bookAuthor ?: ""})"
+        }
+    }
+
+    fun isItemHeader(position: Int): Boolean {
+        if (position == 0) return true
+        val lastItem = getItem(position - 1)
+        val curItem = getItem(position)
+        return !(lastItem?.bookName == curItem?.bookName
+            && lastItem?.bookAuthor == curItem?.bookAuthor)
+    }
 
 }

+ 102 - 0
app/src/main/java/io/legado/app/ui/book/bookmark/BookmarkDecoration.kt

@@ -0,0 +1,102 @@
+package io.legado.app.ui.book.bookmark
+
+import android.graphics.Canvas
+import android.graphics.Paint
+import android.graphics.Rect
+import android.text.TextPaint
+import android.view.View
+import androidx.recyclerview.widget.RecyclerView
+import io.legado.app.lib.theme.accentColor
+import io.legado.app.lib.theme.backgroundColor
+import io.legado.app.utils.dpToPx
+import io.legado.app.utils.spToPx
+import splitties.init.appCtx
+
+class BookmarkDecoration(val adapter: BookmarkAdapter) : RecyclerView.ItemDecoration() {
+
+    private val headerLeft = 16f.dpToPx()
+    private val headerHeight = 32f.dpToPx()
+
+    private val headerPaint = Paint().apply {
+        color = appCtx.backgroundColor
+    }
+    private val textPaint = TextPaint().apply {
+        textSize = 16f.spToPx()
+        color = appCtx.accentColor
+    }
+    private val textRect = Rect()
+
+    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
+        val count = parent.childCount
+        for (i in 0 until count) {
+            val view = parent.getChildAt(i)
+            val position = parent.getChildLayoutPosition(view)
+            val isHeader = adapter.isItemHeader(position)
+            if (isHeader) {
+                c.drawRect(0f, 0f, parent.width.toFloat(), headerHeight, headerPaint)
+                val headerText = adapter.getHeaderText(position)
+                textPaint.getTextBounds(headerText, 0, headerText.length, textRect)
+                c.drawText(
+                    headerText,
+                    headerLeft,
+                    (view.top - headerHeight) + headerHeight / 2 + textRect.height() / 2,
+                    textPaint
+                )
+            }
+        }
+    }
+
+//    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
+//        val position = (parent.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
+//        val view = parent.findViewHolderForAdapterPosition(position)?.itemView ?: return
+//        val isHeader = adapter.isItemHeader(position + 1)
+//        val headerText = adapter.getHeaderText(position)
+//        if (isHeader) {
+//            val bottom = min(headerHeight.toInt(), view.bottom)
+//            c.drawRect(
+//                0f,
+//                view.top.toFloat(),
+//                parent.width.toFloat(),
+//                view.top + headerHeight,
+//                headerPaint
+//            )
+//            textPaint.getTextBounds(headerText, 0, headerText.length, textRect)
+//            c.drawText(
+//                headerText,
+//                headerLeft,
+//                headerHeight / 2 + textRect.height() / 2 - (headerHeight - bottom),
+//                textPaint
+//            )
+//        } else {
+//            c.drawRect(
+//                0f,
+//                0f,
+//                parent.width.toFloat(),
+//                headerHeight,
+//                headerPaint
+//            )
+//            textPaint.getTextBounds(headerText, 0, headerText.length, textRect)
+//            c.drawText(
+//                headerText,
+//                headerLeft,
+//                headerHeight / 2 + textRect.height() / 2,
+//                textPaint
+//            )
+//        }
+//        c.save()
+//    }
+
+    override fun getItemOffsets(
+        outRect: Rect,
+        view: View,
+        parent: RecyclerView,
+        state: RecyclerView.State
+    ) {
+        val position = parent.getChildLayoutPosition(view)
+        val isHeader = adapter.isItemHeader(position)
+        if (isHeader) {
+            outRect.top = headerHeight.toInt()
+        }
+    }
+
+}

+ 3 - 0
app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt

@@ -6,6 +6,7 @@ import io.legado.app.base.adapter.ItemViewHolder
 import io.legado.app.base.adapter.RecyclerAdapter
 import io.legado.app.data.entities.Bookmark
 import io.legado.app.databinding.ItemBookmarkBinding
+import io.legado.app.utils.gone
 import splitties.views.onLongClick
 
 class BookmarkAdapter(context: Context, val callback: Callback) :
@@ -22,7 +23,9 @@ class BookmarkAdapter(context: Context, val callback: Callback) :
         payloads: MutableList<Any>
     ) {
         binding.tvChapterName.text = item.chapterName
+        binding.tvBookText.gone(item.bookText.isEmpty())
         binding.tvBookText.text = item.bookText
+        binding.tvContent.gone(item.content.isEmpty())
         binding.tvContent.text = item.content
     }
 

+ 1 - 1
app/src/main/java/io/legado/app/ui/book/toc/BookmarkDialog.kt

@@ -75,7 +75,7 @@ class BookmarkDialog() : BaseDialogFragment(R.layout.dialog_bookmark) {
         }
     }
 
-    fun getCallback(): Callback? {
+    private fun getCallback(): Callback? {
         return parentFragment as? Callback
     }
 

+ 8 - 0
app/src/main/java/io/legado/app/utils/ViewExtensions.kt

@@ -98,6 +98,14 @@ fun View.gone() {
     }
 }
 
+fun View.gone(gone: Boolean) {
+    if (gone) {
+        gone()
+    } else {
+        visibility = VISIBLE
+    }
+}
+
 fun View.invisible() {
     if (visibility != INVISIBLE) {
         visibility = INVISIBLE