From b498deb2d352b4588fc2e64f174b56eb5d993a33 Mon Sep 17 00:00:00 2001 From: HapeLee <1321903405@qq.com> Date: Fri, 14 Nov 2025 22:32:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E9=A1=B5=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/main/bookshelf/books/BooksFragment.kt | 73 +++++++++-------- .../bookshelf/books/BookshelfFragment2.kt | 78 +++++++++---------- 2 files changed, 74 insertions(+), 77 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 36ce3ade3..90177d5b9 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 @@ -203,50 +203,47 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books), private fun upRecyclerData() { booksFlowJob?.cancel() booksFlowJob = viewLifecycleOwner.lifecycleScope.launch { - appDb.bookDao.flowByGroup(groupId).map { list -> - //排序 - val isDescending = AppConfig.bookshelfSortOrder == 1 + viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { - when (bookSort) { - 1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime } - else list.sortedBy { it.latestChapterTime } + appDb.bookDao.flowByGroup(groupId) + .map { list -> + val isDescending = AppConfig.bookshelfSortOrder == 1 - 2 -> if (isDescending) - list.sortedWith { o1, o2 -> o2.name.cnCompare(o1.name) } - else - list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) } + when (bookSort) { + 1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime } + else list.sortedBy { it.latestChapterTime } - 3 -> if (isDescending) list.sortedByDescending { it.order } - else list.sortedBy { it.order } + 2 -> if (isDescending) + list.sortedWith { o1, o2 -> o2.name.cnCompare(o1.name) } + else + list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) } - 4 -> if (isDescending) list.sortedByDescending { - max( - it.latestChapterTime, - it.durChapterTime - ) + 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) } - 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 -> - 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 4f9c27e35..5111d510b 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 @@ -14,6 +14,7 @@ import androidx.core.view.WindowInsetsCompat import androidx.core.view.isGone import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager @@ -246,16 +247,14 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2) } private fun initBooksData() { - if (groupId == BookGroup.Companion.IdRoot) { + if (groupId == BookGroup.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,48 +262,49 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2) } booksFlowJob?.cancel() booksFlowJob = viewLifecycleOwner.lifecycleScope.launch { - appDb.bookDao.flowByGroup(groupId).map { list -> - //排序 - val isDescending = AppConfig.bookshelfSortOrder == 1 + viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { + appDb.bookDao.flowByGroup(groupId) + .map { list -> + val isDescending = AppConfig.bookshelfSortOrder == 1 + val sortType = AppConfig.getBookSortByGroupId(groupId) - val sortType = AppConfig.getBookSortByGroupId(groupId) - when (sortType) { - 1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime } - else list.sortedBy { it.latestChapterTime } + 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 } - } + else -> + if (isDescending) list.sortedByDescending { it.durChapterTime } + else list.sortedBy { it.durChapterTime } + } + } + .flowOn(Dispatchers.Default) + .catch { AppLog.put("书架更新出错", it) } + .collect { list -> - }.flowWithLifecycleAndDatabaseChangeFirst( - viewLifecycleOwner.lifecycle, - Lifecycle.State.RESUMED, - AppDatabase.Companion.BOOK_TABLE_NAME - ).catch { - AppLog.put("书架更新出错", it) - }.conflate().flowOn(Dispatchers.Default).collect { list -> - books = list - booksAdapter.updateItems() - binding.emptyView.isGone = getItemCount() > 0 - binding.refreshLayout.isEnabled = enableRefresh && getItemCount() > 0 - delay(500) + books = list + booksAdapter.updateItems() + val empty = getItemCount() > 0 + binding.emptyView.isGone = empty + binding.refreshLayout.isEnabled = enableRefresh && empty + } } } }