|
|
@@ -19,8 +19,7 @@ import io.legado.app.utils.gone
|
|
|
import io.legado.app.utils.longToastOnUi
|
|
|
import io.legado.app.utils.visible
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
-import kotlinx.coroutines.isActive
|
|
|
-import kotlinx.coroutines.runBlocking
|
|
|
+import kotlinx.coroutines.ensureActive
|
|
|
import java.util.concurrent.ConcurrentHashMap
|
|
|
|
|
|
class ChapterListAdapter(context: Context, val callback: Callback) :
|
|
|
@@ -58,51 +57,55 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
|
|
|
get() = callback.book?.let {
|
|
|
ContentProcessor.get(it.name, it.origin).getTitleReplaceRules()
|
|
|
}
|
|
|
- private val useReplace
|
|
|
- get() = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true
|
|
|
+ private var useReplace = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true
|
|
|
private var upDisplayTileJob: Coroutine<*>? = null
|
|
|
|
|
|
override fun onCurrentListChanged() {
|
|
|
super.onCurrentListChanged()
|
|
|
- upDisplayTitle()
|
|
|
callback.onListChanged()
|
|
|
}
|
|
|
|
|
|
fun clearDisplayTitle() {
|
|
|
+ useReplace = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true
|
|
|
upDisplayTileJob?.cancel()
|
|
|
displayTitleMap.clear()
|
|
|
}
|
|
|
|
|
|
- fun upDisplayTitle() {
|
|
|
+ fun upDisplayTitle(startIndex: Int) {
|
|
|
upDisplayTileJob?.cancel()
|
|
|
upDisplayTileJob = Coroutine.async(callback.scope) {
|
|
|
val replaceRules = replaceRules
|
|
|
val useReplace = useReplace
|
|
|
- getItems().forEach {
|
|
|
- if (!isActive) {
|
|
|
- return@async
|
|
|
+ val items = getItems()
|
|
|
+ for (i in startIndex until items.size) {
|
|
|
+ val item = items[i]
|
|
|
+ if (displayTitleMap[item.title] == null) {
|
|
|
+ ensureActive()
|
|
|
+ val displayTitle = item.getDisplayTitle(replaceRules, useReplace)
|
|
|
+ ensureActive()
|
|
|
+ displayTitleMap[item.title] = displayTitle
|
|
|
+ notifyItemChanged(i, true)
|
|
|
}
|
|
|
- if (displayTitleMap[it.title] == null) {
|
|
|
- displayTitleMap[it.title] = it.getDisplayTitle(replaceRules, useReplace)
|
|
|
+ }
|
|
|
+ for (i in 0 until startIndex) {
|
|
|
+ val item = items[i]
|
|
|
+ if (displayTitleMap[item.title] == null) {
|
|
|
+ ensureActive()
|
|
|
+ val displayTitle = item.getDisplayTitle(replaceRules, useReplace)
|
|
|
+ ensureActive()
|
|
|
+ displayTitleMap[item.title] = displayTitle
|
|
|
+ notifyItemChanged(i, true)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun getViewBinding(parent: ViewGroup): ItemChapterListBinding {
|
|
|
- return ItemChapterListBinding.inflate(inflater, parent, false)
|
|
|
+ private fun getDisplayTitle(chapter: BookChapter): String {
|
|
|
+ return displayTitleMap[chapter.title] ?: chapter.title
|
|
|
}
|
|
|
|
|
|
- private fun getDisplayTile(chapter: BookChapter): String {
|
|
|
- var displayTitle = displayTitleMap[chapter.title]
|
|
|
- if (displayTitle != null) {
|
|
|
- return displayTitle
|
|
|
- }
|
|
|
- displayTitle = runBlocking {
|
|
|
- chapter.getDisplayTitle(replaceRules, useReplace)
|
|
|
- }
|
|
|
- displayTitleMap[chapter.title] = displayTitle
|
|
|
- return displayTitle
|
|
|
+ override fun getViewBinding(parent: ViewGroup): ItemChapterListBinding {
|
|
|
+ return ItemChapterListBinding.inflate(inflater, parent, false)
|
|
|
}
|
|
|
|
|
|
override fun convert(
|
|
|
@@ -120,7 +123,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
|
|
|
} else {
|
|
|
tvChapterName.setTextColor(context.getCompatColor(R.color.primaryText))
|
|
|
}
|
|
|
- tvChapterName.text = getDisplayTile(item)
|
|
|
+ tvChapterName.text = getDisplayTitle(item)
|
|
|
if (item.isVolume) {
|
|
|
//卷名,如第一卷 突出显示
|
|
|
tvChapterItem.setBackgroundColor(context.getCompatColor(R.color.btn_bg_press))
|
|
|
@@ -150,8 +153,8 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
|
|
|
}
|
|
|
}
|
|
|
holder.itemView.setOnLongClickListener {
|
|
|
- getItem(holder.layoutPosition)?.let {
|
|
|
- context.longToastOnUi(getDisplayTile(it))
|
|
|
+ getItem(holder.layoutPosition)?.let { item ->
|
|
|
+ context.longToastOnUi(getDisplayTitle(item))
|
|
|
}
|
|
|
true
|
|
|
}
|