优化
This commit is contained in:
@@ -17,7 +17,6 @@ import io.legado.app.utils.setEdgeEffectColor
|
|||||||
import io.legado.app.utils.showDialogFragment
|
import io.legado.app.utils.showDialogFragment
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.collect
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,41 +6,38 @@ 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.ContentProcessor
|
|
||||||
import io.legado.app.help.coroutine.Coroutine
|
|
||||||
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
|
|
||||||
|
|
||||||
class ChapterListAdapter(context: Context, private val scope: CoroutineScope, val callback: Callback) :
|
class ChapterListAdapter(context: Context, val callback: Callback) :
|
||||||
RecyclerAdapter<BookChapter, ItemChapterListBinding>(context) {
|
RecyclerAdapter<Pair<BookChapter, String>, ItemChapterListBinding>(context) {
|
||||||
|
|
||||||
private val replaceRules
|
|
||||||
get() = callback.book?.let {
|
|
||||||
ContentProcessor.get(it.name, it.origin).getReplaceRules()
|
|
||||||
}
|
|
||||||
private val useReplace get() = callback.book?.getUseReplaceRule() == true
|
|
||||||
val cacheFileNames = hashSetOf<String>()
|
val cacheFileNames = hashSetOf<String>()
|
||||||
val diffCallBack = object : DiffUtil.ItemCallback<BookChapter>() {
|
val diffCallBack = object : DiffUtil.ItemCallback<Pair<BookChapter, String>>() {
|
||||||
|
|
||||||
override fun areItemsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean {
|
override fun areItemsTheSame(
|
||||||
return oldItem.index == newItem.index
|
oldItem: Pair<BookChapter, String>,
|
||||||
|
newItem: Pair<BookChapter, String>
|
||||||
|
): Boolean {
|
||||||
|
return oldItem.first.index == newItem.first.index
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean {
|
override fun areContentsTheSame(
|
||||||
return oldItem.bookUrl == newItem.bookUrl
|
oldItem: Pair<BookChapter, String>,
|
||||||
&& oldItem.url == newItem.url
|
newItem: Pair<BookChapter, String>
|
||||||
&& oldItem.isVip == newItem.isVip
|
): Boolean {
|
||||||
&& oldItem.isPay == newItem.isPay
|
return oldItem.first.bookUrl == newItem.first.bookUrl
|
||||||
&& oldItem.title == newItem.title
|
&& oldItem.first.url == newItem.first.url
|
||||||
&& oldItem.tag == newItem.tag
|
&& oldItem.first.isVip == newItem.first.isVip
|
||||||
&& oldItem.isVolume == newItem.isVolume
|
&& oldItem.first.isPay == newItem.first.isPay
|
||||||
|
&& oldItem.first.title == newItem.first.title
|
||||||
|
&& oldItem.first.tag == newItem.first.tag
|
||||||
|
&& oldItem.first.isVolume == newItem.first.isVolume
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -52,24 +49,20 @@ class ChapterListAdapter(context: Context, private val scope: CoroutineScope, va
|
|||||||
override fun convert(
|
override fun convert(
|
||||||
holder: ItemViewHolder,
|
holder: ItemViewHolder,
|
||||||
binding: ItemChapterListBinding,
|
binding: ItemChapterListBinding,
|
||||||
item: BookChapter,
|
item: Pair<BookChapter, String>,
|
||||||
payloads: MutableList<Any>
|
payloads: MutableList<Any>
|
||||||
) {
|
) {
|
||||||
binding.run {
|
binding.run {
|
||||||
val isDur = callback.durChapterIndex() == item.index
|
val isDur = callback.durChapterIndex() == item.first.index
|
||||||
val cached = callback.isLocalBook || cacheFileNames.contains(item.getFileName())
|
val cached = callback.isLocalBook || cacheFileNames.contains(item.first.getFileName())
|
||||||
if (payloads.isEmpty()) {
|
if (payloads.isEmpty()) {
|
||||||
if (isDur) {
|
if (isDur) {
|
||||||
tvChapterName.setTextColor(context.accentColor)
|
tvChapterName.setTextColor(context.accentColor)
|
||||||
} else {
|
} else {
|
||||||
tvChapterName.setTextColor(context.getCompatColor(R.color.primaryText))
|
tvChapterName.setTextColor(context.getCompatColor(R.color.primaryText))
|
||||||
}
|
}
|
||||||
Coroutine.async(scope) {
|
tvChapterName.text = item.second
|
||||||
item.getDisplayTitle(replaceRules, useReplace)
|
if (item.first.isVolume) {
|
||||||
}.onSuccess {
|
|
||||||
tvChapterName.text = it
|
|
||||||
}
|
|
||||||
if (item.isVolume) {
|
|
||||||
//卷名,如第一卷 突出显示
|
//卷名,如第一卷 突出显示
|
||||||
tvChapterItem.setBackgroundColor(context.getCompatColor(R.color.btn_bg_press))
|
tvChapterItem.setBackgroundColor(context.getCompatColor(R.color.btn_bg_press))
|
||||||
} else {
|
} else {
|
||||||
@@ -77,9 +70,9 @@ class ChapterListAdapter(context: Context, private val scope: CoroutineScope, va
|
|||||||
tvChapterItem.background =
|
tvChapterItem.background =
|
||||||
ThemeUtils.resolveDrawable(context, android.R.attr.selectableItemBackground)
|
ThemeUtils.resolveDrawable(context, android.R.attr.selectableItemBackground)
|
||||||
}
|
}
|
||||||
if (!item.tag.isNullOrEmpty() && !item.isVolume) {
|
if (!item.first.tag.isNullOrEmpty() && !item.first.isVolume) {
|
||||||
//卷名不显示tag(更新时间规则)
|
//卷名不显示tag(更新时间规则)
|
||||||
tvTag.text = item.tag
|
tvTag.text = item.first.tag
|
||||||
tvTag.visible()
|
tvTag.visible()
|
||||||
} else {
|
} else {
|
||||||
tvTag.gone()
|
tvTag.gone()
|
||||||
@@ -94,7 +87,7 @@ class ChapterListAdapter(context: Context, private val scope: CoroutineScope, va
|
|||||||
override fun registerListener(holder: ItemViewHolder, binding: ItemChapterListBinding) {
|
override fun registerListener(holder: ItemViewHolder, binding: ItemChapterListBinding) {
|
||||||
holder.itemView.setOnClickListener {
|
holder.itemView.setOnClickListener {
|
||||||
getItem(holder.layoutPosition)?.let {
|
getItem(holder.layoutPosition)?.let {
|
||||||
callback.openChapter(it)
|
callback.openChapter(it.first)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,7 +103,6 @@ class ChapterListAdapter(context: Context, private val scope: CoroutineScope, va
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Callback {
|
interface Callback {
|
||||||
val book: Book?
|
|
||||||
val isLocalBook: Boolean
|
val isLocalBook: Boolean
|
||||||
fun openChapter(bookChapter: BookChapter)
|
fun openChapter(bookChapter: BookChapter)
|
||||||
fun durChapterIndex(): Int
|
fun durChapterIndex(): Int
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ 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.FragmentChapterListBinding
|
import io.legado.app.databinding.FragmentChapterListBinding
|
||||||
import io.legado.app.help.BookHelp
|
import io.legado.app.help.BookHelp
|
||||||
|
import io.legado.app.help.ContentProcessor
|
||||||
import io.legado.app.lib.theme.bottomBackground
|
import io.legado.app.lib.theme.bottomBackground
|
||||||
import io.legado.app.lib.theme.getPrimaryTextColor
|
import io.legado.app.lib.theme.getPrimaryTextColor
|
||||||
import io.legado.app.ui.widget.recycler.UpLinearLayoutManager
|
import io.legado.app.ui.widget.recycler.UpLinearLayoutManager
|
||||||
@@ -33,7 +34,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
|
|||||||
override val viewModel by activityViewModels<TocViewModel>()
|
override val viewModel by activityViewModels<TocViewModel>()
|
||||||
private val binding by viewBinding(FragmentChapterListBinding::bind)
|
private val binding by viewBinding(FragmentChapterListBinding::bind)
|
||||||
private val mLayoutManager by lazy { UpLinearLayoutManager(requireContext()) }
|
private val mLayoutManager by lazy { UpLinearLayoutManager(requireContext()) }
|
||||||
private val adapter by lazy { ChapterListAdapter(requireContext(), this, this) }
|
private val adapter by lazy { ChapterListAdapter(requireContext(), this) }
|
||||||
private var durChapterIndex = 0
|
private var durChapterIndex = 0
|
||||||
private var tocFlowJob: Job? = null
|
private var tocFlowJob: Job? = null
|
||||||
|
|
||||||
@@ -111,7 +112,16 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
|
|||||||
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey)
|
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey)
|
||||||
}.collect {
|
}.collect {
|
||||||
if (!(searchKey.isNullOrBlank() && it.isEmpty())) {
|
if (!(searchKey.isNullOrBlank() && it.isEmpty())) {
|
||||||
adapter.setItems(it, adapter.diffCallBack)
|
val data = withContext(IO) {
|
||||||
|
val replaces = viewModel.bookData.value?.let { book ->
|
||||||
|
ContentProcessor.get(book.name, book.origin).getReplaceRules()
|
||||||
|
}
|
||||||
|
val useReplace = viewModel.bookData.value?.getUseReplaceRule() == true
|
||||||
|
it.map { chapter ->
|
||||||
|
Pair(chapter, chapter.getDisplayTitle(replaces, useReplace))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
adapter.setItems(data, adapter.diffCallBack)
|
||||||
if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) {
|
if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) {
|
||||||
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
|
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
|
||||||
}
|
}
|
||||||
@@ -120,9 +130,6 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user