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 d8a961f33..9a75b9308 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 @@ -4,8 +4,8 @@ import android.content.Context import android.view.ViewGroup import androidx.recyclerview.widget.DiffUtil import io.legado.app.R +import io.legado.app.base.adapter.DiffRecyclerAdapter 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 @@ -22,21 +22,23 @@ import kotlinx.coroutines.isActive import java.util.concurrent.ConcurrentHashMap class ChapterListAdapter(context: Context, val callback: Callback) : - RecyclerAdapter(context) { + DiffRecyclerAdapter(context) { val cacheFileNames = hashSetOf() val displayTileMap = ConcurrentHashMap() - val diffCallBack = object : DiffUtil.ItemCallback() { - override fun areItemsTheSame( - oldItem: BookChapter, - newItem: BookChapter - ): Boolean { - return oldItem.index == newItem.index - } + override val diffItemCallback: DiffUtil.ItemCallback + get() = object : DiffUtil.ItemCallback() { + + override fun areItemsTheSame( + oldItem: BookChapter, + newItem: BookChapter + ): Boolean { + return oldItem.index == newItem.index + } - override fun areContentsTheSame( - oldItem: BookChapter, + override fun areContentsTheSame( + oldItem: BookChapter, newItem: BookChapter ): Boolean { return oldItem.bookUrl == newItem.bookUrl @@ -48,7 +50,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) : && oldItem.isVolume == newItem.isVolume } - } + } private val replaceRules get() = callback.book?.let { @@ -58,6 +60,11 @@ class ChapterListAdapter(context: Context, val callback: Callback) : get() = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true private var upDisplayTileJob: Coroutine<*>? = null + override fun onCurrentListChanged() { + super.onCurrentListChanged() + upDisplayTile() + } + fun upDisplayTile() { upDisplayTileJob?.cancel() upDisplayTileJob = Coroutine.async(callback.scope) { @@ -151,4 +158,5 @@ class ChapterListAdapter(context: Context, val callback: Callback) : fun openChapter(bookChapter: BookChapter) fun durChapterIndex(): Int } + } \ No newline at end of file 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 fa3c60bbc..18f34c0d7 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 @@ -109,12 +109,9 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt searchKey.isNullOrBlank() -> appDb.bookChapterDao.flowByBook(viewModel.bookUrl) else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey) }.conflate().collect { - if (!(searchKey.isNullOrBlank() && it.isEmpty())) { - adapter.setItems(it, adapter.diffCallBack) - adapter.upDisplayTile() - if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) { - mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) - } + adapter.setItems(it) + if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) { + mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) } delay(100) }