pull/1606/head
kunfei 3 years ago
parent e5984acd9c
commit 2f93dc912a
  1. 48
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt
  2. 23
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt

@ -14,33 +14,31 @@ import io.legado.app.utils.getCompatColor
import io.legado.app.utils.gone import io.legado.app.utils.gone
import io.legado.app.utils.visible import io.legado.app.utils.visible
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.launch
class ChapterListAdapter(context: Context, val callback: Callback) : class ChapterListAdapter(context: Context, val callback: Callback) :
RecyclerAdapter<Pair<BookChapter, Deferred<String>>, ItemChapterListBinding>(context) { RecyclerAdapter<BookChapter, ItemChapterListBinding>(context) {
val cacheFileNames = hashSetOf<String>() val cacheFileNames = hashSetOf<String>()
val diffCallBack = object : DiffUtil.ItemCallback<Pair<BookChapter, Deferred<String>>>() { val diffCallBack = object : DiffUtil.ItemCallback<BookChapter>() {
override fun areItemsTheSame( override fun areItemsTheSame(
oldItem: Pair<BookChapter, Deferred<String>>, oldItem: BookChapter,
newItem: Pair<BookChapter, Deferred<String>> newItem: BookChapter
): Boolean { ): Boolean {
return oldItem.first.index == newItem.first.index return oldItem.index == newItem.index
} }
override fun areContentsTheSame( override fun areContentsTheSame(
oldItem: Pair<BookChapter, Deferred<String>>, oldItem: BookChapter,
newItem: Pair<BookChapter, Deferred<String>> newItem: BookChapter
): Boolean { ): Boolean {
return oldItem.first.bookUrl == newItem.first.bookUrl return oldItem.bookUrl == newItem.bookUrl
&& oldItem.first.url == newItem.first.url && oldItem.url == newItem.url
&& oldItem.first.isVip == newItem.first.isVip && oldItem.isVip == newItem.isVip
&& oldItem.first.isPay == newItem.first.isPay && oldItem.isPay == newItem.isPay
&& oldItem.first.title == newItem.first.title && oldItem.title == newItem.title
&& oldItem.first.tag == newItem.first.tag && oldItem.tag == newItem.tag
&& oldItem.first.isVolume == newItem.first.isVolume && oldItem.isVolume == newItem.isVolume
} }
} }
@ -52,22 +50,20 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
override fun convert( override fun convert(
holder: ItemViewHolder, holder: ItemViewHolder,
binding: ItemChapterListBinding, binding: ItemChapterListBinding,
item: Pair<BookChapter, Deferred<String>>, item: BookChapter,
payloads: MutableList<Any> payloads: MutableList<Any>
) { ) {
binding.run { binding.run {
val isDur = callback.durChapterIndex() == item.first.index val isDur = callback.durChapterIndex() == item.index
val cached = callback.isLocalBook || cacheFileNames.contains(item.first.getFileName()) val cached = callback.isLocalBook || cacheFileNames.contains(item.getFileName())
if (payloads.isEmpty()) { if (payloads.isEmpty()) {
if (isDur) { if (isDur) {
tvChapterName.setTextColor(context.accentColor) tvChapterName.setTextColor(context.accentColor)
} else { } else {
tvChapterName.setTextColor(context.getCompatColor(R.color.primaryText)) tvChapterName.setTextColor(context.getCompatColor(R.color.primaryText))
} }
callback.scope.launch { tvChapterName.text = item.getDisplayTitle()
tvChapterName.text = item.second.await() if (item.isVolume) {
}
if (item.first.isVolume) {
//卷名,如第一卷 突出显示 //卷名,如第一卷 突出显示
tvChapterItem.setBackgroundColor(context.getCompatColor(R.color.btn_bg_press)) tvChapterItem.setBackgroundColor(context.getCompatColor(R.color.btn_bg_press))
} else { } else {
@ -75,9 +71,9 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
tvChapterItem.background = tvChapterItem.background =
ThemeUtils.resolveDrawable(context, android.R.attr.selectableItemBackground) ThemeUtils.resolveDrawable(context, android.R.attr.selectableItemBackground)
} }
if (!item.first.tag.isNullOrEmpty() && !item.first.isVolume) { if (!item.tag.isNullOrEmpty() && !item.isVolume) {
//卷名不显示tag(更新时间规则) //卷名不显示tag(更新时间规则)
tvTag.text = item.first.tag tvTag.text = item.tag
tvTag.visible() tvTag.visible()
} else { } else {
tvTag.gone() tvTag.gone()
@ -92,7 +88,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
override fun registerListener(holder: ItemViewHolder, binding: ItemChapterListBinding) { override fun registerListener(holder: ItemViewHolder, binding: ItemChapterListBinding) {
holder.itemView.setOnClickListener { holder.itemView.setOnClickListener {
getItem(holder.layoutPosition)?.let { getItem(holder.layoutPosition)?.let {
callback.openChapter(it.first) callback.openChapter(it)
} }
} }
} }

@ -13,9 +13,7 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.databinding.FragmentChapterListBinding import io.legado.app.databinding.FragmentChapterListBinding
import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp 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.bottomBackground
import io.legado.app.lib.theme.getPrimaryTextColor import io.legado.app.lib.theme.getPrimaryTextColor
import io.legado.app.ui.widget.recycler.UpLinearLayoutManager import io.legado.app.ui.widget.recycler.UpLinearLayoutManager
@ -23,9 +21,12 @@ import io.legado.app.ui.widget.recycler.VerticalDivider
import io.legado.app.utils.ColorUtils import io.legado.app.utils.ColorUtils
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.* import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapter_list), class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapter_list),
ChapterListAdapter.Callback, ChapterListAdapter.Callback,
@ -111,21 +112,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey) else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey)
}.collect { }.collect {
if (!(searchKey.isNullOrBlank() && it.isEmpty())) { if (!(searchKey.isNullOrBlank() && it.isEmpty())) {
val data = withContext(IO) { adapter.setItems(it, adapter.diffCallBack)
val replaces = viewModel.bookData.value?.let { book ->
ContentProcessor.get(book.name, book.origin).getReplaceRules()
}
val useReplace =
AppConfig.tocUiUseReplace && viewModel.bookData.value?.getUseReplaceRule() == true
it.map { chapter ->
Pair(
chapter,
async(IO) {
chapter.getDisplayTitle(replaces, useReplace)
})
}
}
adapter.setItems(data, adapter.diffCallBack)
if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) { if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) {
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
} }

Loading…
Cancel
Save