pull/1693/head
kunfei 3 years ago
parent 417c1c28a0
commit 7e97e60b58
  1. 14
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt
  2. 5
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt

@ -4,8 +4,8 @@ import android.content.Context
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import io.legado.app.R 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.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
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.ItemChapterListBinding import io.legado.app.databinding.ItemChapterListBinding
@ -22,11 +22,13 @@ import kotlinx.coroutines.isActive
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
class ChapterListAdapter(context: Context, val callback: Callback) : class ChapterListAdapter(context: Context, val callback: Callback) :
RecyclerAdapter<BookChapter, ItemChapterListBinding>(context) { DiffRecyclerAdapter<BookChapter, ItemChapterListBinding>(context) {
val cacheFileNames = hashSetOf<String>() val cacheFileNames = hashSetOf<String>()
val displayTileMap = ConcurrentHashMap<Int, String>() val displayTileMap = ConcurrentHashMap<Int, String>()
val diffCallBack = object : DiffUtil.ItemCallback<BookChapter>() {
override val diffItemCallback: DiffUtil.ItemCallback<BookChapter>
get() = object : DiffUtil.ItemCallback<BookChapter>() {
override fun areItemsTheSame( override fun areItemsTheSame(
oldItem: BookChapter, oldItem: BookChapter,
@ -58,6 +60,11 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
get() = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true get() = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true
private var upDisplayTileJob: Coroutine<*>? = null private var upDisplayTileJob: Coroutine<*>? = null
override fun onCurrentListChanged() {
super.onCurrentListChanged()
upDisplayTile()
}
fun upDisplayTile() { fun upDisplayTile() {
upDisplayTileJob?.cancel() upDisplayTileJob?.cancel()
upDisplayTileJob = Coroutine.async(callback.scope) { upDisplayTileJob = Coroutine.async(callback.scope) {
@ -151,4 +158,5 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
fun openChapter(bookChapter: BookChapter) fun openChapter(bookChapter: BookChapter)
fun durChapterIndex(): Int fun durChapterIndex(): Int
} }
} }

@ -109,13 +109,10 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
searchKey.isNullOrBlank() -> appDb.bookChapterDao.flowByBook(viewModel.bookUrl) searchKey.isNullOrBlank() -> appDb.bookChapterDao.flowByBook(viewModel.bookUrl)
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey) else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey)
}.conflate().collect { }.conflate().collect {
if (!(searchKey.isNullOrBlank() && it.isEmpty())) { adapter.setItems(it)
adapter.setItems(it, adapter.diffCallBack)
adapter.upDisplayTile()
if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) { if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) {
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
} }
}
delay(100) delay(100)
} }
} }

Loading…
Cancel
Save