pull/1876/head^2
kunfei 3 years ago
parent 6247a49b6e
commit 49a207d931
  1. 55
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt
  2. 13
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/toc/TocActivity.kt
  4. 1
      app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt

@ -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
}

@ -97,7 +97,15 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
viewModel.bookData.value?.bookUrl?.let { bookUrl ->
if (chapter.bookUrl == bookUrl) {
adapter.cacheFileNames.add(chapter.getFileName())
adapter.notifyItemChanged(chapter.index, true)
if (viewModel.searchKey.isNullOrEmpty()) {
adapter.notifyItemChanged(chapter.index, true)
} else {
adapter.getItems().forEachIndexed { index, bookChapter ->
if (bookChapter.index == chapter.index) {
adapter.notifyItemChanged(index, true)
}
}
}
}
}
}
@ -128,12 +136,13 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
}
}
mLayoutManager.scrollToPositionWithOffset(scrollPos, 0)
adapter.upDisplayTitle(scrollPos)
}
}
override fun clearDisplayTitle() {
adapter.clearDisplayTitle()
adapter.upDisplayTitle()
adapter.upDisplayTitle(mLayoutManager.findFirstVisibleItemPosition())
}
override val scope: CoroutineScope

@ -59,10 +59,12 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>() {
setOnSearchClickListener { tabLayout.gone() }
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
viewModel.searchKey = query
return false
}
override fun onQueryTextChange(newText: String): Boolean {
viewModel.searchKey = newText
if (tabLayout.selectedTabPosition == 1) {
viewModel.startBookmarkSearch(newText)
} else {

@ -12,6 +12,7 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
var bookData = MutableLiveData<Book>()
var chapterListCallBack: ChapterListCallBack? = null
var bookMarkCallBack: BookmarkCallBack? = null
var searchKey: String? = null
fun initBook(bookUrl: String) {
this.bookUrl = bookUrl

Loading…
Cancel
Save