优化
This commit is contained in:
@@ -14,33 +14,31 @@ import io.legado.app.utils.getCompatColor
|
||||
import io.legado.app.utils.gone
|
||||
import io.legado.app.utils.visible
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class ChapterListAdapter(context: Context, val callback: Callback) :
|
||||
RecyclerAdapter<Pair<BookChapter, Deferred<String>>, ItemChapterListBinding>(context) {
|
||||
RecyclerAdapter<BookChapter, ItemChapterListBinding>(context) {
|
||||
|
||||
val cacheFileNames = hashSetOf<String>()
|
||||
val diffCallBack = object : DiffUtil.ItemCallback<Pair<BookChapter, Deferred<String>>>() {
|
||||
val diffCallBack = object : DiffUtil.ItemCallback<BookChapter>() {
|
||||
|
||||
override fun areItemsTheSame(
|
||||
oldItem: Pair<BookChapter, Deferred<String>>,
|
||||
newItem: Pair<BookChapter, Deferred<String>>
|
||||
oldItem: BookChapter,
|
||||
newItem: BookChapter
|
||||
): Boolean {
|
||||
return oldItem.first.index == newItem.first.index
|
||||
return oldItem.index == newItem.index
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(
|
||||
oldItem: Pair<BookChapter, Deferred<String>>,
|
||||
newItem: Pair<BookChapter, Deferred<String>>
|
||||
oldItem: BookChapter,
|
||||
newItem: BookChapter
|
||||
): 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
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -52,22 +50,20 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
|
||||
override fun convert(
|
||||
holder: ItemViewHolder,
|
||||
binding: ItemChapterListBinding,
|
||||
item: Pair<BookChapter, Deferred<String>>,
|
||||
item: BookChapter,
|
||||
payloads: MutableList<Any>
|
||||
) {
|
||||
binding.run {
|
||||
val isDur = callback.durChapterIndex() == item.first.index
|
||||
val cached = callback.isLocalBook || cacheFileNames.contains(item.first.getFileName())
|
||||
val isDur = callback.durChapterIndex() == item.index
|
||||
val cached = callback.isLocalBook || cacheFileNames.contains(item.getFileName())
|
||||
if (payloads.isEmpty()) {
|
||||
if (isDur) {
|
||||
tvChapterName.setTextColor(context.accentColor)
|
||||
} else {
|
||||
tvChapterName.setTextColor(context.getCompatColor(R.color.primaryText))
|
||||
}
|
||||
callback.scope.launch {
|
||||
tvChapterName.text = item.second.await()
|
||||
}
|
||||
if (item.first.isVolume) {
|
||||
tvChapterName.text = item.getDisplayTitle()
|
||||
if (item.isVolume) {
|
||||
//卷名,如第一卷 突出显示
|
||||
tvChapterItem.setBackgroundColor(context.getCompatColor(R.color.btn_bg_press))
|
||||
} else {
|
||||
@@ -75,9 +71,9 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
|
||||
tvChapterItem.background =
|
||||
ThemeUtils.resolveDrawable(context, android.R.attr.selectableItemBackground)
|
||||
}
|
||||
if (!item.first.tag.isNullOrEmpty() && !item.first.isVolume) {
|
||||
if (!item.tag.isNullOrEmpty() && !item.isVolume) {
|
||||
//卷名不显示tag(更新时间规则)
|
||||
tvTag.text = item.first.tag
|
||||
tvTag.text = item.tag
|
||||
tvTag.visible()
|
||||
} else {
|
||||
tvTag.gone()
|
||||
@@ -92,7 +88,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
|
||||
override fun registerListener(holder: ItemViewHolder, binding: ItemChapterListBinding) {
|
||||
holder.itemView.setOnClickListener {
|
||||
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.BookChapter
|
||||
import io.legado.app.databinding.FragmentChapterListBinding
|
||||
import io.legado.app.help.AppConfig
|
||||
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
|
||||
@@ -23,9 +21,12 @@ import io.legado.app.ui.widget.recycler.VerticalDivider
|
||||
import io.legado.app.utils.ColorUtils
|
||||
import io.legado.app.utils.observeEvent
|
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
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),
|
||||
ChapterListAdapter.Callback,
|
||||
@@ -111,21 +112,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
|
||||
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey)
|
||||
}.collect {
|
||||
if (!(searchKey.isNullOrBlank() && it.isEmpty())) {
|
||||
val data = withContext(IO) {
|
||||
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)
|
||||
adapter.setItems(it, adapter.diffCallBack)
|
||||
if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) {
|
||||
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user