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 42ceea9a4..dc904794a 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 @@ -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> + @Query("SELECT * FROM books order by durChapterTime desc") fun flowAll(): Flow> @@ -17,7 +28,13 @@ interface BookDao { @Query("SELECT * FROM books WHERE origin = '${BookType.local}'") fun flowLocal(): Flow> - @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> @Query("SELECT bookUrl FROM books WHERE origin = '${BookType.local}'") diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BaseBooksAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BaseBooksAdapter.kt index 6f3a1f9a9..8a3d4cedb 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BaseBooksAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BaseBooksAdapter.kt @@ -73,6 +73,14 @@ abstract class BaseBooksAdapter( } } + 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( fun isUpdate(bookUrl: String): Boolean fun getItemCount(): Int fun getItemType(position: Int): Int - fun getItem(position: Int): Any + fun getItem(position: Int): Any? } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt index 9166759b0..8af4c7530 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt @@ -17,14 +17,6 @@ import splitties.views.onLongClick class BooksAdapterGrid(context: Context, callBack: CallBack) : BaseBooksAdapter(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 diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt index c2a700ab1..40acd6cdb 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt @@ -19,14 +19,6 @@ import splitties.views.onLongClick class BooksAdapterList(context: Context, callBack: CallBack) : BaseBooksAdapter(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 { 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 6c18a4989..e15624ef7 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 @@ -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 = emptyList() private var booksFlowJob: Job? = null - override var groupId = AppConst.bookGroupNoneId + override var groupId = rootGroupId override var books: List = 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")