This commit is contained in:
kunfei 2022-03-02 09:01:23 +08:00
parent b7b6c44974
commit 89e53fbd17
5 changed files with 49 additions and 38 deletions

View File

@ -1,6 +1,7 @@
package io.legado.app.data.dao
import androidx.room.*
import io.legado.app.constant.AppConst
import io.legado.app.constant.BookType
import io.legado.app.data.entities.Book
import kotlinx.coroutines.flow.Flow
@ -8,6 +9,16 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface BookDao {
@Query(
"""
select * from books where type != ${BookType.audio}
and origin != '${BookType.local}'
and ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0
and (select show from book_groups where groupId = ${AppConst.bookGroupNoneId}) != 1
"""
)
fun flowRoot(): Flow<List<Book>>
@Query("SELECT * FROM books order by durChapterTime desc")
fun flowAll(): Flow<List<Book>>
@ -17,7 +28,13 @@ interface BookDao {
@Query("SELECT * FROM books WHERE origin = '${BookType.local}'")
fun flowLocal(): Flow<List<Book>>
@Query("select * from books where type != ${BookType.audio} and origin != '${BookType.local}' and ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0")
@Query(
"""
select * from books where type != ${BookType.audio}
and origin != '${BookType.local}'
and ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0
"""
)
fun flowNoGroup(): Flow<List<Book>>
@Query("SELECT bookUrl FROM books WHERE origin = '${BookType.local}'")

View File

@ -73,6 +73,14 @@ abstract class BaseBooksAdapter<VH : RecyclerView.ViewHolder>(
}
}
override fun getItemCount(): Int {
return callBack.getItemCount()
}
override fun getItemViewType(position: Int): Int {
return callBack.getItemType(position)
}
interface CallBack {
fun onItemClick(position: Int)
@ -80,6 +88,6 @@ abstract class BaseBooksAdapter<VH : RecyclerView.ViewHolder>(
fun isUpdate(bookUrl: String): Boolean
fun getItemCount(): Int
fun getItemType(position: Int): Int
fun getItem(position: Int): Any
fun getItem(position: Int): Any?
}
}

View File

@ -17,14 +17,6 @@ import splitties.views.onLongClick
class BooksAdapterGrid(context: Context, callBack: CallBack) :
BaseBooksAdapter<RecyclerView.ViewHolder>(context, callBack) {
override fun getItemCount(): Int {
return callBack.getItemCount()
}
override fun getItemViewType(position: Int): Int {
return callBack.getItemType(position)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
@ -67,7 +59,7 @@ class BooksAdapterGrid(context: Context, callBack: CallBack) :
private fun onBindBook(binding: ItemBookshelfGridBinding, position: Int, bundle: Bundle) {
binding.run {
val item = callBack.getItem(position) as Book
val item = callBack.getItem(position) as? Book ?: return
bundle.keySet().forEach {
when (it) {
"name" -> tvName.text = item.name

View File

@ -19,14 +19,6 @@ import splitties.views.onLongClick
class BooksAdapterList(context: Context, callBack: CallBack) :
BaseBooksAdapter<RecyclerView.ViewHolder>(context, callBack) {
override fun getItemCount(): Int {
return callBack.getItemCount()
}
override fun getItemViewType(position: Int): Int {
return callBack.getItemType(position)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
1 -> GroupViewHolder(
@ -65,7 +57,7 @@ class BooksAdapterList(context: Context, callBack: CallBack) :
private fun onBindBook(binding: ItemBookshelfListBinding, position: Int, bundle: Bundle) {
binding.run {
val item = callBack.getItem(position) as Book
val item = callBack.getItem(position) as? Book ?: return
tvRead.text = item.durChapterTitle
tvLast.text = item.latestChapterTitle
bundle.keySet().forEach {

View File

@ -43,6 +43,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
BaseBooksAdapter.CallBack {
private val binding by viewBinding(FragmentBookshelf1Binding::bind)
private val rootGroupId = -100L
private val bookshelfLayout by lazy {
getPrefInt(PreferKey.bookshelfLayout)
}
@ -55,7 +56,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
private var bookGroups: List<BookGroup> = emptyList()
private var booksFlowJob: Job? = null
override var groupId = AppConst.bookGroupNoneId
override var groupId = rootGroupId
override var books: List<Book> = emptyList()
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
@ -108,7 +109,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
@SuppressLint("NotifyDataSetChanged")
private fun initBooksData() {
if (groupId == AppConst.bookGroupNoneId) {
if (groupId == -100L) {
binding.titleBar.title = getString(R.string.bookshelf)
} else {
bookGroups.forEach {
@ -120,6 +121,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
booksFlowJob?.cancel()
booksFlowJob = launch {
when (groupId) {
rootGroupId -> appDb.bookDao.flowRoot()
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
@ -152,8 +154,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
fun back(): Boolean {
if (groupId != AppConst.bookGroupNoneId) {
groupId = AppConst.bookGroupNoneId
if (groupId != -100L) {
groupId = -100L
initBooksData()
return true
}
@ -210,7 +212,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
override fun getItemCount(): Int {
return if (groupId == AppConst.bookGroupNoneId) {
return if (groupId == rootGroupId) {
bookGroups.size + books.size
} else {
books.size
@ -218,23 +220,23 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
override fun getItemType(position: Int): Int {
return if (groupId == AppConst.bookGroupNoneId) {
if (position < bookGroups.size) 1 else 0
} else {
0
if (groupId != rootGroupId) {
return 0
}
if (position < bookGroups.size) {
return 1
}
return 0
}
override fun getItem(position: Int): Any {
return if (groupId == AppConst.bookGroupNoneId) {
if (position < bookGroups.size) {
bookGroups[position]
} else {
books[position - bookGroups.size]
}
} else {
books[position]
override fun getItem(position: Int): Any? {
if (groupId != rootGroupId) {
return books.getOrNull(position)
}
if (position < bookGroups.size) {
return bookGroups[position]
}
return books.getOrNull(position - bookGroups.size)
}
@SuppressLint("NotifyDataSetChanged")