From 57173d414809985bd3ba452042d82a27f8720351 Mon Sep 17 00:00:00 2001 From: HapeLee <1321903405@qq.com> Date: Sun, 28 Dec 2025 03:06:12 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BF=AE=E5=A4=8D]=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=BA=86=E4=B9=A6=E6=9E=B6=E4=B9=A6=E7=B1=8D=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E7=BC=93=E6=85=A2=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/main/bookshelf/books/BooksFragment.kt | 77 +++++++++--------- .../bookshelf/books/BookshelfFragment2.kt | 78 ++++++++++--------- 2 files changed, 82 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt index a2d802c94..cb838133e 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt @@ -23,6 +23,7 @@ import io.legado.app.R import io.legado.app.base.BaseFragment import io.legado.app.constant.AppLog import io.legado.app.constant.EventBus +import io.legado.app.data.AppDatabase import io.legado.app.data.appDb import io.legado.app.data.entities.Book 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.bookshelfLayoutMode import io.legado.app.utils.cnCompare +import io.legado.app.utils.flowWithLifecycleAndDatabaseChangeFirst import io.legado.app.utils.observeEvent import io.legado.app.utils.startActivity import io.legado.app.utils.startActivityForBook @@ -52,6 +54,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.isActive @@ -204,47 +207,51 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books), private fun upRecyclerData() { booksFlowJob?.cancel() booksFlowJob = viewLifecycleOwner.lifecycleScope.launch { + appDb.bookDao.flowByGroup(groupId).map { list -> + //排序 - viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) { + val isDescending = AppConfig.bookshelfSortOrder == 1 - appDb.bookDao.flowByGroup(groupId) - .map { list -> - val isDescending = AppConfig.bookshelfSortOrder == 1 + when (bookSort) { + 1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime } + else list.sortedBy { it.latestChapterTime } - when (bookSort) { - 1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime } - else list.sortedBy { it.latestChapterTime } + 2 -> if (isDescending) + list.sortedWith { o1, o2 -> o2.name.cnCompare(o1.name) } + else + list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) } - 2 -> if (isDescending) - list.sortedWith { o1, o2 -> o2.name.cnCompare(o1.name) } - else - list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) } + 3 -> if (isDescending) list.sortedByDescending { it.order } + else list.sortedBy { it.order } - 3 -> if (isDescending) list.sortedByDescending { it.order } - else list.sortedBy { it.order } - - 4 -> if (isDescending) - list.sortedByDescending { max(it.latestChapterTime, it.durChapterTime) } - else - list.sortedBy { max(it.latestChapterTime, it.durChapterTime) } - - 5 -> if (isDescending) - list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) } - else - list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) } - - else -> - if (isDescending) list.sortedByDescending { it.durChapterTime } - else list.sortedBy { it.durChapterTime } - } - } - .flowOn(Dispatchers.Default) - .catch { AppLog.put("书架更新出错", it) } - .collect { list -> - binding.emptyView.isGone = list.isNotEmpty() - binding.refreshLayout.isEnabled = enableRefresh && list.isNotEmpty() - booksAdapter.setItems(list) + 4 -> if (isDescending) list.sortedByDescending { + max( + it.latestChapterTime, + it.durChapterTime + ) } + else list.sortedBy { max(it.latestChapterTime, it.durChapterTime) } + + 5 -> if (isDescending) + list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) } + else + list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) } + + else -> if (isDescending) list.sortedByDescending { it.durChapterTime } + else list.sortedBy { it.durChapterTime } + } + }.flowWithLifecycleAndDatabaseChangeFirst( + viewLifecycleOwner.lifecycle, + Lifecycle.State.RESUMED, + 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.refreshLayout.isEnabled = enableRefresh && list.isNotEmpty() + booksAdapter.setItems(list) + delay(500) } } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BookshelfFragment2.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BookshelfFragment2.kt index 7cca0c1e6..42ad87197 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BookshelfFragment2.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BookshelfFragment2.kt @@ -54,6 +54,7 @@ import io.legado.app.utils.startActivityForBook import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.flowOn @@ -248,14 +249,16 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2) } private fun initBooksData() { - if (groupId == BookGroup.IdRoot) { + if (groupId == BookGroup.Companion.IdRoot) { if (isAdded) { binding.collTopBar.title = getString(R.string.bookshelf) binding.refreshLayout.isEnabled = true enableRefresh = true } } else { - bookGroups.firstOrNull { groupId == it.groupId }?.let { + bookGroups.firstOrNull { + groupId == it.groupId + }?.let { binding.collTopBar.title = it.groupName binding.refreshLayout.isEnabled = it.enableRefresh enableRefresh = it.enableRefresh @@ -263,49 +266,48 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2) } booksFlowJob?.cancel() booksFlowJob = viewLifecycleOwner.lifecycleScope.launch { - viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) { - appDb.bookDao.flowByGroup(groupId) - .map { list -> - val isDescending = AppConfig.bookshelfSortOrder == 1 - val sortType = AppConfig.getBookSortByGroupId(groupId) + appDb.bookDao.flowByGroup(groupId).map { list -> + //排序 + val isDescending = AppConfig.bookshelfSortOrder == 1 - when (sortType) { - 1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime } - else list.sortedBy { it.latestChapterTime } + val sortType = AppConfig.getBookSortByGroupId(groupId) + when (sortType) { + 1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime } + else list.sortedBy { it.latestChapterTime } - 2 -> if (isDescending) - list.sortedWith { o1, o2 -> o2.name.cnCompare(o1.name) } - else - list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) } + 2 -> if (isDescending) + list.sortedWith { o1, o2 -> o2.name.cnCompare(o1.name) } + else + list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) } - 3 -> if (isDescending) list.sortedByDescending { it.order } - else list.sortedBy { it.order } + 3 -> if (isDescending) list.sortedByDescending { it.order } + else list.sortedBy { it.order } - 4 -> if (isDescending) - list.sortedByDescending { max(it.latestChapterTime, it.durChapterTime) } - else - list.sortedBy { max(it.latestChapterTime, it.durChapterTime) } + 4 -> if (isDescending) list.sortedByDescending { + max(it.latestChapterTime, it.durChapterTime) + } else list.sortedBy { max(it.latestChapterTime, it.durChapterTime) } - 5 -> if (isDescending) - list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) } - else - list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) } + 5 -> if (isDescending) + list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) } + else + list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) } - else -> - if (isDescending) list.sortedByDescending { it.durChapterTime } - else list.sortedBy { it.durChapterTime } - } - } - .flowOn(Dispatchers.Default) - .catch { AppLog.put("书架更新出错", it) } - .collect { list -> + else -> if (isDescending) list.sortedByDescending { it.durChapterTime } + else list.sortedBy { it.durChapterTime } + } - books = list - booksAdapter.updateItems() - val empty = getItemCount() > 0 - binding.emptyView.isGone = empty - binding.refreshLayout.isEnabled = enableRefresh && empty - } + }.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() + delay(500) } } }