diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt index f23ddd7ff..efa72ab0f 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt @@ -23,14 +23,17 @@ import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.main.MainViewModel import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.launch import kotlin.math.max /** * 书架界面 */ +@ExperimentalCoroutinesApi class BooksFragment() : BaseFragment(R.layout.fragment_books), BaseBooksAdapter.CallBack { @@ -109,9 +112,8 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books), else -> appDb.bookDao.flowByGroup(groupId) }.catch { AppLog.put("书架更新出错", it) - }.collect { list -> - binding.tvEmptyMsg.isGone = list.isNotEmpty() - val books = when (getPrefInt(PreferKey.bookshelfSort)) { + }.mapLatest { list -> + when (getPrefInt(PreferKey.bookshelfSort)) { 1 -> list.sortedByDescending { it.latestChapterTime } 2 -> list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) @@ -119,7 +121,9 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books), 3 -> list.sortedBy { it.order } else -> list.sortedByDescending { it.durChapterTime } } - booksAdapter.setItems(books) + }.collect { list -> + binding.tvEmptyMsg.isGone = list.isNotEmpty() + booksAdapter.setItems(list) } } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt index 7e05728aa..30008647e 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt @@ -25,15 +25,18 @@ import io.legado.app.ui.book.search.SearchActivity import io.legado.app.ui.main.bookshelf.BaseBookshelfFragment import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.launch import kotlin.math.max /** * 书架界面 */ +@ExperimentalCoroutinesApi class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), SearchView.OnQueryTextListener, BaseBooksAdapter.CallBack { @@ -123,8 +126,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), else -> appDb.bookDao.flowByGroup(groupId) }.catch { AppLog.put("书架更新出错", it) - }.collectLatest { list -> - books = when (getPrefInt(PreferKey.bookshelfSort)) { + }.mapLatest { list -> + when (getPrefInt(PreferKey.bookshelfSort)) { 1 -> list.sortedByDescending { it.latestChapterTime } @@ -138,6 +141,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), it.durChapterTime } } + }.collectLatest { list -> + books = list booksAdapter.notifyDataSetChanged() binding.tvEmptyMsg.isGone = getItemCount() > 0 }