diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index b421c3ffa..3623792a0 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -18,16 +18,9 @@ interface BookDao { @Query("SELECT * FROM books WHERE origin = '${BookType.local}'") fun observeLocal(): LiveData> - @Query( - """ - select * from books where ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0 and type != ${BookType.audio} and origin != '${BookType.local}' - """ - ) + @Query("select * from books where ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0 and type != ${BookType.audio} and origin != '${BookType.local}'") fun observeNoGroup(): LiveData> - @Query("select count(bookUrl) from books where (SELECT sum(groupId) FROM book_groups where groupId > 0) & `group` = 0") - fun observeNoGroupSize(): LiveData - @Query("SELECT bookUrl FROM books WHERE origin = '${BookType.local}'") fun observeLocalUri(): LiveData> diff --git a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt index e23aa9eb5..eb3395a91 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt @@ -2,6 +2,7 @@ package io.legado.app.data.dao import androidx.lifecycle.LiveData import androidx.room.* +import io.legado.app.constant.BookType import io.legado.app.data.entities.BookGroup @Dao @@ -16,7 +17,16 @@ interface BookGroupDao { @Query("SELECT * FROM book_groups ORDER BY `order`") fun liveDataAll(): LiveData> - @Query("SELECT * FROM book_groups where groupId >= 0 or (groupId < 0 and show > 0) ORDER BY `order`") + @Query( + """ + SELECT * FROM book_groups where groupId >= 0 + or (groupId = -4 and show > 0 and (select count(bookUrl) from books where ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0 and type != ${BookType.audio} and origin != '${BookType.local}') > 0) + or (groupId = -3 and show > 0 and (select count(bookUrl) from books where type = ${BookType.audio}) > 0) + or (groupId = -2 and show > 0 and (select count(bookUrl) from books where origin = '${BookType.local}') > 0) + or (groupId = -1 and show > 0) + ORDER BY `order` + """ + ) fun liveDataShow(): LiveData> @Query("SELECT * FROM book_groups where groupId >= 0 ORDER BY `order`") diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 861e1025c..218bc004a 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -105,11 +105,12 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b private fun initBookGroupData() { bookGroupLiveData?.removeObservers(viewLifecycleOwner) - bookGroupLiveData = App.db.bookGroupDao().liveDataShow() - bookGroupLiveData?.observe(viewLifecycleOwner, { - viewModel.checkGroup(it) - upGroup(it) - }) + bookGroupLiveData = App.db.bookGroupDao().liveDataShow().apply { + observe(viewLifecycleOwner) { + viewModel.checkGroup(it) + upGroup(it) + } + } } override fun onQueryTextSubmit(query: String?): Boolean { @@ -126,10 +127,12 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b if (data.isEmpty()) { App.db.bookGroupDao().enableGroup(AppConst.bookGroupAllId) } else { - bookGroups.clear() - bookGroups.addAll(data) - adapter.notifyDataSetChanged() - selectLastTab() + if (data != bookGroups) { + bookGroups.clear() + bookGroups.addAll(data) + adapter.notifyDataSetChanged() + selectLastTab() + } } } 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 ebd3e8cad..a52856d33 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 @@ -107,19 +107,20 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), AppConst.bookGroupAudioId -> App.db.bookDao().observeAudio() AppConst.bookGroupNoneId -> App.db.bookDao().observeNoGroup() else -> App.db.bookDao().observeByGroup(groupId) - } - bookshelfLiveData?.observe(this, { list -> - tv_empty_msg.isGone = list.isNotEmpty() - val books = when (getPrefInt(PreferKey.bookshelfSort)) { - 1 -> list.sortedByDescending { it.latestChapterTime } - 2 -> list.sortedBy { it.name } - 3 -> list.sortedBy { it.order } - else -> list.sortedByDescending { it.durChapterTime } + }.apply { + observe(viewLifecycleOwner) { list -> + tv_empty_msg.isGone = list.isNotEmpty() + val books = when (getPrefInt(PreferKey.bookshelfSort)) { + 1 -> list.sortedByDescending { it.latestChapterTime } + 2 -> list.sortedBy { it.name } + 3 -> list.sortedBy { it.order } + else -> list.sortedByDescending { it.durChapterTime } + } + val diffResult = DiffUtil + .calculateDiff(BooksDiffCallBack(booksAdapter.getItems(), books)) + booksAdapter.setItems(books, diffResult) } - val diffResult = DiffUtil - .calculateDiff(BooksDiffCallBack(booksAdapter.getItems(), books)) - booksAdapter.setItems(books, diffResult) - }) + } } fun getBooks(): List {