This commit is contained in:
kunfei
2022-02-10 22:19:28 +08:00
parent 47f32f1667
commit 1d4dfcd421
2 changed files with 31 additions and 1 deletions
@@ -6,14 +6,19 @@ import androidx.recyclerview.widget.DiffUtil
import io.legado.app.R import io.legado.app.R
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.base.adapter.RecyclerAdapter
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
import io.legado.app.help.AppConfig
import io.legado.app.help.ContentProcessor
import io.legado.app.lib.theme.ThemeUtils import io.legado.app.lib.theme.ThemeUtils
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.getCompatColor import io.legado.app.utils.getCompatColor
import io.legado.app.utils.gone import io.legado.app.utils.gone
import io.legado.app.utils.visible import io.legado.app.utils.visible
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
class ChapterListAdapter(context: Context, val callback: Callback) : class ChapterListAdapter(context: Context, val callback: Callback) :
RecyclerAdapter<BookChapter, ItemChapterListBinding>(context) { RecyclerAdapter<BookChapter, ItemChapterListBinding>(context) {
@@ -44,6 +49,25 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
} }
private val replaceRules
get() = callback.book?.let {
ContentProcessor.get(it.name, it.origin).getReplaceRules()
}
private val useReplace
get() = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true
fun upDisplayTile() {
callback.scope.launch(IO) {
val replaceRules = replaceRules
val useReplace = useReplace
getItems().forEach {
if (displayTileMap[it.index] == null) {
displayTileMap[it.index] = it.getDisplayTitle(replaceRules, useReplace)
}
}
}
}
override fun getViewBinding(parent: ViewGroup): ItemChapterListBinding { override fun getViewBinding(parent: ViewGroup): ItemChapterListBinding {
return ItemChapterListBinding.inflate(inflater, parent, false) return ItemChapterListBinding.inflate(inflater, parent, false)
} }
@@ -53,7 +77,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
if (displayTile != null) { if (displayTile != null) {
return displayTile return displayTile
} }
displayTile = chapter.getDisplayTitle() displayTile = chapter.getDisplayTitle(replaceRules, useReplace)
displayTileMap[chapter.index] = displayTile displayTileMap[chapter.index] = displayTile
return displayTile return displayTile
} }
@@ -116,6 +140,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
interface Callback { interface Callback {
val scope: CoroutineScope val scope: CoroutineScope
val book: Book?
val isLocalBook: Boolean val isLocalBook: Boolean
fun openChapter(bookChapter: BookChapter) fun openChapter(bookChapter: BookChapter)
fun durChapterIndex(): Int fun durChapterIndex(): Int
@@ -113,6 +113,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
}.collect { }.collect {
if (!(searchKey.isNullOrBlank() && it.isEmpty())) { if (!(searchKey.isNullOrBlank() && it.isEmpty())) {
adapter.setItems(it, adapter.diffCallBack) 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)
} }
@@ -123,11 +124,15 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
override fun clearDisplayTitle() { override fun clearDisplayTitle() {
adapter.displayTileMap.clear() adapter.displayTileMap.clear()
adapter.upDisplayTile()
} }
override val scope: CoroutineScope override val scope: CoroutineScope
get() = this get() = this
override val book: Book?
get() = viewModel.bookData.value
override val isLocalBook: Boolean override val isLocalBook: Boolean
get() = viewModel.bookData.value?.isLocalBook() == true get() = viewModel.bookData.value?.isLocalBook() == true