From 2f93dc912af7787fa22a6eba8c1774f8d1265811 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 10 Feb 2022 20:41:58 +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 | 48 +++++++++---------- .../app/ui/book/toc/ChapterListFragment.kt | 23 ++------- 2 files changed, 27 insertions(+), 44 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 8072f2d70..90c56609d 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 @@ -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>, ItemChapterListBinding>(context) { + RecyclerAdapter(context) { val cacheFileNames = hashSetOf() - val diffCallBack = object : DiffUtil.ItemCallback>>() { + val diffCallBack = object : DiffUtil.ItemCallback() { override fun areItemsTheSame( - oldItem: Pair>, - newItem: Pair> + oldItem: BookChapter, + newItem: BookChapter ): Boolean { - return oldItem.first.index == newItem.first.index + return oldItem.index == newItem.index } override fun areContentsTheSame( - oldItem: Pair>, - newItem: Pair> + 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>, + item: BookChapter, payloads: MutableList ) { 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) } } } 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 267819561..6a2b81970 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 @@ -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(R.layout.fragment_chapter_list), ChapterListAdapter.Callback, @@ -111,21 +112,7 @@ class ChapterListFragment : VMBaseFragment(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) }