默认分组无书籍时自动隐藏

pull/433/head
gedoor 4 years ago
parent 49508c2204
commit a2f1941820
  1. 9
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 12
      app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt
  3. 21
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  4. 25
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt

@ -18,16 +18,9 @@ interface BookDao {
@Query("SELECT * FROM books WHERE origin = '${BookType.local}'") @Query("SELECT * FROM books WHERE origin = '${BookType.local}'")
fun observeLocal(): LiveData<List<Book>> fun observeLocal(): LiveData<List<Book>>
@Query( @Query("select * from books where ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0 and type != ${BookType.audio} and origin != '${BookType.local}'")
"""
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<List<Book>> fun observeNoGroup(): LiveData<List<Book>>
@Query("select count(bookUrl) from books where (SELECT sum(groupId) FROM book_groups where groupId > 0) & `group` = 0")
fun observeNoGroupSize(): LiveData<Int>
@Query("SELECT bookUrl FROM books WHERE origin = '${BookType.local}'") @Query("SELECT bookUrl FROM books WHERE origin = '${BookType.local}'")
fun observeLocalUri(): LiveData<List<String>> fun observeLocalUri(): LiveData<List<String>>

@ -2,6 +2,7 @@ package io.legado.app.data.dao
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.room.* import androidx.room.*
import io.legado.app.constant.BookType
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
@Dao @Dao
@ -16,7 +17,16 @@ interface BookGroupDao {
@Query("SELECT * FROM book_groups ORDER BY `order`") @Query("SELECT * FROM book_groups ORDER BY `order`")
fun liveDataAll(): LiveData<List<BookGroup>> fun liveDataAll(): LiveData<List<BookGroup>>
@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<List<BookGroup>> fun liveDataShow(): LiveData<List<BookGroup>>
@Query("SELECT * FROM book_groups where groupId >= 0 ORDER BY `order`") @Query("SELECT * FROM book_groups where groupId >= 0 ORDER BY `order`")

@ -105,11 +105,12 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
private fun initBookGroupData() { private fun initBookGroupData() {
bookGroupLiveData?.removeObservers(viewLifecycleOwner) bookGroupLiveData?.removeObservers(viewLifecycleOwner)
bookGroupLiveData = App.db.bookGroupDao().liveDataShow() bookGroupLiveData = App.db.bookGroupDao().liveDataShow().apply {
bookGroupLiveData?.observe(viewLifecycleOwner, { observe(viewLifecycleOwner) {
viewModel.checkGroup(it) viewModel.checkGroup(it)
upGroup(it) upGroup(it)
}) }
}
} }
override fun onQueryTextSubmit(query: String?): Boolean { override fun onQueryTextSubmit(query: String?): Boolean {
@ -126,10 +127,12 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
if (data.isEmpty()) { if (data.isEmpty()) {
App.db.bookGroupDao().enableGroup(AppConst.bookGroupAllId) App.db.bookGroupDao().enableGroup(AppConst.bookGroupAllId)
} else { } else {
bookGroups.clear() if (data != bookGroups) {
bookGroups.addAll(data) bookGroups.clear()
adapter.notifyDataSetChanged() bookGroups.addAll(data)
selectLastTab() adapter.notifyDataSetChanged()
selectLastTab()
}
} }
} }

@ -107,19 +107,20 @@ class BooksFragment : BaseFragment(R.layout.fragment_books),
AppConst.bookGroupAudioId -> App.db.bookDao().observeAudio() AppConst.bookGroupAudioId -> App.db.bookDao().observeAudio()
AppConst.bookGroupNoneId -> App.db.bookDao().observeNoGroup() AppConst.bookGroupNoneId -> App.db.bookDao().observeNoGroup()
else -> App.db.bookDao().observeByGroup(groupId) else -> App.db.bookDao().observeByGroup(groupId)
} }.apply {
bookshelfLiveData?.observe(this, { list -> observe(viewLifecycleOwner) { list ->
tv_empty_msg.isGone = list.isNotEmpty() tv_empty_msg.isGone = list.isNotEmpty()
val books = when (getPrefInt(PreferKey.bookshelfSort)) { val books = when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> list.sortedByDescending { it.latestChapterTime } 1 -> list.sortedByDescending { it.latestChapterTime }
2 -> list.sortedBy { it.name } 2 -> list.sortedBy { it.name }
3 -> list.sortedBy { it.order } 3 -> list.sortedBy { it.order }
else -> list.sortedByDescending { it.durChapterTime } 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<Book> { fun getBooks(): List<Book> {

Loading…
Cancel
Save