This commit is contained in:
kunfei 2022-02-09 23:01:04 +08:00
parent 9c28737a40
commit d639b993a6
3 changed files with 39 additions and 41 deletions

View File

@ -17,7 +17,6 @@ import io.legado.app.utils.setEdgeEffectColor
import io.legado.app.utils.showDialogFragment
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

View File

@ -6,41 +6,38 @@ import androidx.recyclerview.widget.DiffUtil
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.databinding.ItemChapterListBinding
import io.legado.app.help.ContentProcessor
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.theme.ThemeUtils
import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.gone
import io.legado.app.utils.visible
import kotlinx.coroutines.CoroutineScope
class ChapterListAdapter(context: Context, private val scope: CoroutineScope, val callback: Callback) :
RecyclerAdapter<BookChapter, ItemChapterListBinding>(context) {
class ChapterListAdapter(context: Context, val callback: Callback) :
RecyclerAdapter<Pair<BookChapter, String>, ItemChapterListBinding>(context) {
private val replaceRules
get() = callback.book?.let {
ContentProcessor.get(it.name, it.origin).getReplaceRules()
}
private val useReplace get() = callback.book?.getUseReplaceRule() == true
val cacheFileNames = hashSetOf<String>()
val diffCallBack = object : DiffUtil.ItemCallback<BookChapter>() {
val diffCallBack = object : DiffUtil.ItemCallback<Pair<BookChapter, String>>() {
override fun areItemsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean {
return oldItem.index == newItem.index
override fun areItemsTheSame(
oldItem: Pair<BookChapter, String>,
newItem: Pair<BookChapter, String>
): Boolean {
return oldItem.first.index == newItem.first.index
}
override fun areContentsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean {
return oldItem.bookUrl == newItem.bookUrl
&& oldItem.url == newItem.url
&& oldItem.isVip == newItem.isVip
&& oldItem.isPay == newItem.isPay
&& oldItem.title == newItem.title
&& oldItem.tag == newItem.tag
&& oldItem.isVolume == newItem.isVolume
override fun areContentsTheSame(
oldItem: Pair<BookChapter, String>,
newItem: Pair<BookChapter, String>
): Boolean {
return oldItem.first.bookUrl == newItem.first.bookUrl
&& oldItem.first.url == newItem.first.url
&& oldItem.first.isVip == newItem.first.isVip
&& oldItem.first.isPay == newItem.first.isPay
&& oldItem.first.title == newItem.first.title
&& oldItem.first.tag == newItem.first.tag
&& oldItem.first.isVolume == newItem.first.isVolume
}
}
@ -52,24 +49,20 @@ class ChapterListAdapter(context: Context, private val scope: CoroutineScope, va
override fun convert(
holder: ItemViewHolder,
binding: ItemChapterListBinding,
item: BookChapter,
item: Pair<BookChapter, String>,
payloads: MutableList<Any>
) {
binding.run {
val isDur = callback.durChapterIndex() == item.index
val cached = callback.isLocalBook || cacheFileNames.contains(item.getFileName())
val isDur = callback.durChapterIndex() == item.first.index
val cached = callback.isLocalBook || cacheFileNames.contains(item.first.getFileName())
if (payloads.isEmpty()) {
if (isDur) {
tvChapterName.setTextColor(context.accentColor)
} else {
tvChapterName.setTextColor(context.getCompatColor(R.color.primaryText))
}
Coroutine.async(scope) {
item.getDisplayTitle(replaceRules, useReplace)
}.onSuccess {
tvChapterName.text = it
}
if (item.isVolume) {
tvChapterName.text = item.second
if (item.first.isVolume) {
//卷名,如第一卷 突出显示
tvChapterItem.setBackgroundColor(context.getCompatColor(R.color.btn_bg_press))
} else {
@ -77,9 +70,9 @@ class ChapterListAdapter(context: Context, private val scope: CoroutineScope, va
tvChapterItem.background =
ThemeUtils.resolveDrawable(context, android.R.attr.selectableItemBackground)
}
if (!item.tag.isNullOrEmpty() && !item.isVolume) {
if (!item.first.tag.isNullOrEmpty() && !item.first.isVolume) {
//卷名不显示tag(更新时间规则)
tvTag.text = item.tag
tvTag.text = item.first.tag
tvTag.visible()
} else {
tvTag.gone()
@ -94,7 +87,7 @@ class ChapterListAdapter(context: Context, private val scope: CoroutineScope, va
override fun registerListener(holder: ItemViewHolder, binding: ItemChapterListBinding) {
holder.itemView.setOnClickListener {
getItem(holder.layoutPosition)?.let {
callback.openChapter(it)
callback.openChapter(it.first)
}
}
}
@ -110,7 +103,6 @@ class ChapterListAdapter(context: Context, private val scope: CoroutineScope, va
}
interface Callback {
val book: Book?
val isLocalBook: Boolean
fun openChapter(bookChapter: BookChapter)
fun durChapterIndex(): Int

View File

@ -14,6 +14,7 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.databinding.FragmentChapterListBinding
import io.legado.app.help.BookHelp
import io.legado.app.help.ContentProcessor
import io.legado.app.lib.theme.bottomBackground
import io.legado.app.lib.theme.getPrimaryTextColor
import io.legado.app.ui.widget.recycler.UpLinearLayoutManager
@ -33,7 +34,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
override val viewModel by activityViewModels<TocViewModel>()
private val binding by viewBinding(FragmentChapterListBinding::bind)
private val mLayoutManager by lazy { UpLinearLayoutManager(requireContext()) }
private val adapter by lazy { ChapterListAdapter(requireContext(), this, this) }
private val adapter by lazy { ChapterListAdapter(requireContext(), this) }
private var durChapterIndex = 0
private var tocFlowJob: Job? = null
@ -111,7 +112,16 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey)
}.collect {
if (!(searchKey.isNullOrBlank() && it.isEmpty())) {
adapter.setItems(it, adapter.diffCallBack)
val data = withContext(IO) {
val replaces = viewModel.bookData.value?.let { book ->
ContentProcessor.get(book.name, book.origin).getReplaceRules()
}
val useReplace = viewModel.bookData.value?.getUseReplaceRule() == true
it.map { chapter ->
Pair(chapter, chapter.getDisplayTitle(replaces, useReplace))
}
}
adapter.setItems(data, adapter.diffCallBack)
if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) {
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
}
@ -120,9 +130,6 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
}
}
override val book: Book?
get() = viewModel.bookData.value
override val isLocalBook: Boolean
get() = viewModel.bookData.value?.isLocalBook() == true