[修复] 修复了书架书籍加载缓慢的问题

This commit is contained in:
HapeLee committed 2025-12-28 03:06:12 +08:00
1 parent 80664185f0
commit 57173d4148
2 files changed
+45 -36

No files matched your search

@@ -23,6 +23,7 @@ import io.legado.app.R
import io.legado.app.base.BaseFragment import io.legado.app.base.BaseFragment
import io.legado.app.constant.AppLog import io.legado.app.constant.AppLog
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.data.AppDatabase
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
@@ -44,6 +45,7 @@ import io.legado.app.ui.main.bookshelf.books.styleDefalut.BooksAdapterListCompac
import io.legado.app.utils.bookshelfLayoutGrid import io.legado.app.utils.bookshelfLayoutGrid
import io.legado.app.utils.bookshelfLayoutMode import io.legado.app.utils.bookshelfLayoutMode
import io.legado.app.utils.cnCompare import io.legado.app.utils.cnCompare
import io.legado.app.utils.flowWithLifecycleAndDatabaseChangeFirst
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import io.legado.app.utils.startActivity import io.legado.app.utils.startActivity
import io.legado.app.utils.startActivityForBook import io.legado.app.utils.startActivityForBook
@@ -52,6 +54,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
@@ -204,11 +207,9 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
private fun upRecyclerData() { private fun upRecyclerData() {
booksFlowJob?.cancel() booksFlowJob?.cancel()
booksFlowJob = viewLifecycleOwner.lifecycleScope.launch { booksFlowJob = viewLifecycleOwner.lifecycleScope.launch {
appDb.bookDao.flowByGroup(groupId).map { list ->
//排序
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
appDb.bookDao.flowByGroup(groupId)
.map { list ->
val isDescending = AppConfig.bookshelfSortOrder == 1 val isDescending = AppConfig.bookshelfSortOrder == 1
when (bookSort) { when (bookSort) {
@@ -223,28 +224,34 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
3 -> if (isDescending) list.sortedByDescending { it.order } 3 -> if (isDescending) list.sortedByDescending { it.order }
else list.sortedBy { it.order } else list.sortedBy { it.order }
4 -> if (isDescending) 4 -> if (isDescending) list.sortedByDescending {
list.sortedByDescending { max(it.latestChapterTime, it.durChapterTime) } max(
else it.latestChapterTime,
list.sortedBy { max(it.latestChapterTime, it.durChapterTime) } it.durChapterTime
)
}
else list.sortedBy { max(it.latestChapterTime, it.durChapterTime) }
5 -> if (isDescending) 5 -> if (isDescending)
list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) } list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) }
else else
list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) } list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) }
else -> else -> if (isDescending) list.sortedByDescending { it.durChapterTime }
if (isDescending) list.sortedByDescending { it.durChapterTime }
else list.sortedBy { it.durChapterTime } else list.sortedBy { it.durChapterTime }
} }
} }.flowWithLifecycleAndDatabaseChangeFirst(
.flowOn(Dispatchers.Default) viewLifecycleOwner.lifecycle,
.catch { AppLog.put("书架更新出错", it) } Lifecycle.State.RESUMED,
.collect { list -> AppDatabase.BOOK_TABLE_NAME
).catch {
AppLog.put("书架更新出错", it)
}.conflate().flowOn(Dispatchers.Default).collect { list ->
if (view == null) return@collect
binding.emptyView.isGone = list.isNotEmpty() binding.emptyView.isGone = list.isNotEmpty()
binding.refreshLayout.isEnabled = enableRefresh && list.isNotEmpty() binding.refreshLayout.isEnabled = enableRefresh && list.isNotEmpty()
booksAdapter.setItems(list) booksAdapter.setItems(list)
} delay(500)
} }
} }
} }
@@ -54,6 +54,7 @@ import io.legado.app.utils.startActivityForBook
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
@@ -248,14 +249,16 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
} }
private fun initBooksData() { private fun initBooksData() {
if (groupId == BookGroup.IdRoot) { if (groupId == BookGroup.Companion.IdRoot) {
if (isAdded) { if (isAdded) {
binding.collTopBar.title = getString(R.string.bookshelf) binding.collTopBar.title = getString(R.string.bookshelf)
binding.refreshLayout.isEnabled = true binding.refreshLayout.isEnabled = true
enableRefresh = true enableRefresh = true
} }
} else { } else {
bookGroups.firstOrNull { groupId == it.groupId }?.let { bookGroups.firstOrNull {
groupId == it.groupId
}?.let {
binding.collTopBar.title = it.groupName binding.collTopBar.title = it.groupName
binding.refreshLayout.isEnabled = it.enableRefresh binding.refreshLayout.isEnabled = it.enableRefresh
enableRefresh = it.enableRefresh enableRefresh = it.enableRefresh
@@ -263,12 +266,11 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
} }
booksFlowJob?.cancel() booksFlowJob?.cancel()
booksFlowJob = viewLifecycleOwner.lifecycleScope.launch { booksFlowJob = viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) { appDb.bookDao.flowByGroup(groupId).map { list ->
appDb.bookDao.flowByGroup(groupId) //排序
.map { list ->
val isDescending = AppConfig.bookshelfSortOrder == 1 val isDescending = AppConfig.bookshelfSortOrder == 1
val sortType = AppConfig.getBookSortByGroupId(groupId)
val sortType = AppConfig.getBookSortByGroupId(groupId)
when (sortType) { when (sortType) {
1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime } 1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime }
else list.sortedBy { it.latestChapterTime } else list.sortedBy { it.latestChapterTime }
@@ -281,31 +283,31 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
3 -> if (isDescending) list.sortedByDescending { it.order } 3 -> if (isDescending) list.sortedByDescending { it.order }
else list.sortedBy { it.order } else list.sortedBy { it.order }
4 -> if (isDescending) 4 -> if (isDescending) list.sortedByDescending {
list.sortedByDescending { max(it.latestChapterTime, it.durChapterTime) } max(it.latestChapterTime, it.durChapterTime)
else } else list.sortedBy { max(it.latestChapterTime, it.durChapterTime) }
list.sortedBy { max(it.latestChapterTime, it.durChapterTime) }
5 -> if (isDescending) 5 -> if (isDescending)
list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) } list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) }
else else
list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) } list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) }
else -> else -> if (isDescending) list.sortedByDescending { it.durChapterTime }
if (isDescending) list.sortedByDescending { it.durChapterTime }
else list.sortedBy { it.durChapterTime } else list.sortedBy { it.durChapterTime }
} }
}
.flowOn(Dispatchers.Default)
.catch { AppLog.put("书架更新出错", it) }
.collect { list ->
books = list }.flowWithLifecycleAndDatabaseChangeFirst(
viewLifecycleOwner.lifecycle,
Lifecycle.State.RESUMED,
AppDatabase.Companion.BOOK_TABLE_NAME
).catch {
AppLog.put("书架更新出错", it)
}.conflate().flowOn(Dispatchers.Default).collect { list ->
if (view == null) return@collect
binding.emptyView.isGone = list.isNotEmpty()
binding.refreshLayout.isEnabled = enableRefresh && list.isNotEmpty()
booksAdapter.updateItems() booksAdapter.updateItems()
val empty = getItemCount() > 0 delay(500)
binding.emptyView.isGone = empty
binding.refreshLayout.isEnabled = enableRefresh && empty
}
} }
} }
} }