From 5520ffc2fdcb2f7e9e3bfffb7a58dbd4abf7c74b Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 9 Feb 2022 23:18:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/toc/ChapterListAdapter.kt | 23 +++++++++++-------- .../app/ui/book/toc/ChapterListFragment.kt | 5 ++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt index bd1db1305..9a9cfa421 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt @@ -13,23 +13,26 @@ 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 +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.launch -class ChapterListAdapter(context: Context, val callback: Callback) : - RecyclerAdapter, ItemChapterListBinding>(context) { +class ChapterListAdapter(context: Context, val callback: Callback, private val scope: CoroutineScope) : + RecyclerAdapter>, ItemChapterListBinding>(context) { val cacheFileNames = hashSetOf() - val diffCallBack = object : DiffUtil.ItemCallback>() { + val diffCallBack = object : DiffUtil.ItemCallback>>() { override fun areItemsTheSame( - oldItem: Pair, - newItem: Pair + oldItem: Pair>, + newItem: Pair> ): Boolean { return oldItem.first.index == newItem.first.index } override fun areContentsTheSame( - oldItem: Pair, - newItem: Pair + oldItem: Pair>, + newItem: Pair> ): Boolean { return oldItem.first.bookUrl == newItem.first.bookUrl && oldItem.first.url == newItem.first.url @@ -49,7 +52,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) : override fun convert( holder: ItemViewHolder, binding: ItemChapterListBinding, - item: Pair, + item: Pair>, payloads: MutableList ) { binding.run { @@ -61,7 +64,9 @@ class ChapterListAdapter(context: Context, val callback: Callback) : } else { tvChapterName.setTextColor(context.getCompatColor(R.color.primaryText)) } - tvChapterName.text = item.second + scope.launch { + tvChapterName.text = item.second.await() + } if (item.first.isVolume) { //卷名,如第一卷 突出显示 tvChapterItem.setBackgroundColor(context.getCompatColor(R.color.btn_bg_press)) diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt index b941a881a..218d96d6e 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt @@ -25,6 +25,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Job +import kotlinx.coroutines.async import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -34,7 +35,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt override val viewModel by activityViewModels() private val binding by viewBinding(FragmentChapterListBinding::bind) private val mLayoutManager by lazy { UpLinearLayoutManager(requireContext()) } - private val adapter by lazy { ChapterListAdapter(requireContext(), this) } + private val adapter by lazy { ChapterListAdapter(requireContext(), this, this) } private var durChapterIndex = 0 private var tocFlowJob: Job? = null @@ -118,7 +119,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt } val useReplace = viewModel.bookData.value?.getUseReplaceRule() == true it.map { chapter -> - Pair(chapter, chapter.getDisplayTitle(replaces, useReplace)) + Pair(chapter, async { chapter.getDisplayTitle(replaces, useReplace) }) } } adapter.setItems(data, adapter.diffCallBack)