添加所有书签界面

pull/1751/head
kunfei 3 years ago
parent 9cd4234fb0
commit a29ea1c62b
  1. 1
      app/src/main/java/io/legado/app/ui/book/bookmark/AllBookmarkActivity.kt
  2. 16
      app/src/main/java/io/legado/app/ui/book/bookmark/BookmarkAdapter.kt
  3. 102
      app/src/main/java/io/legado/app/ui/book/bookmark/BookmarkDecoration.kt
  4. 3
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkDialog.kt
  6. 8
      app/src/main/java/io/legado/app/utils/ViewExtensions.kt

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

@ -6,6 +6,7 @@ import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
import io.legado.app.databinding.ItemBookmarkBinding import io.legado.app.databinding.ItemBookmarkBinding
import io.legado.app.utils.gone
class BookmarkAdapter(context: Context) : RecyclerAdapter<Bookmark, ItemBookmarkBinding>(context) { class BookmarkAdapter(context: Context) : RecyclerAdapter<Bookmark, ItemBookmarkBinding>(context) {
@ -20,7 +21,9 @@ class BookmarkAdapter(context: Context) : RecyclerAdapter<Bookmark, ItemBookmark
payloads: MutableList<Any> payloads: MutableList<Any>
) { ) {
binding.tvChapterName.text = item.chapterName binding.tvChapterName.text = item.chapterName
binding.tvBookText.gone(item.bookText.isEmpty())
binding.tvBookText.text = item.bookText binding.tvBookText.text = item.bookText
binding.tvContent.gone(item.content.isEmpty())
binding.tvContent.text = item.content 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)
}
} }

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

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

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

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

Loading…
Cancel
Save