优化
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package io.legado.app.data.dao
|
package io.legado.app.data.dao
|
||||||
|
|
||||||
import androidx.room.*
|
import androidx.room.*
|
||||||
|
import io.legado.app.constant.AppConst
|
||||||
import io.legado.app.constant.BookType
|
import io.legado.app.constant.BookType
|
||||||
import io.legado.app.data.entities.Book
|
import io.legado.app.data.entities.Book
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -8,6 +9,16 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
@Dao
|
@Dao
|
||||||
interface BookDao {
|
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")
|
@Query("SELECT * FROM books order by durChapterTime desc")
|
||||||
fun flowAll(): Flow<List<Book>>
|
fun flowAll(): Flow<List<Book>>
|
||||||
|
|
||||||
@@ -17,7 +28,13 @@ interface BookDao {
|
|||||||
@Query("SELECT * FROM books WHERE origin = '${BookType.local}'")
|
@Query("SELECT * FROM books WHERE origin = '${BookType.local}'")
|
||||||
fun flowLocal(): Flow<List<Book>>
|
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>>
|
fun flowNoGroup(): Flow<List<Book>>
|
||||||
|
|
||||||
@Query("SELECT bookUrl FROM books WHERE origin = '${BookType.local}'")
|
@Query("SELECT bookUrl FROM books WHERE origin = '${BookType.local}'")
|
||||||
|
|||||||
@@ -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 {
|
interface CallBack {
|
||||||
fun onItemClick(position: Int)
|
fun onItemClick(position: Int)
|
||||||
@@ -80,6 +88,6 @@ abstract class BaseBooksAdapter<VH : RecyclerView.ViewHolder>(
|
|||||||
fun isUpdate(bookUrl: String): Boolean
|
fun isUpdate(bookUrl: String): Boolean
|
||||||
fun getItemCount(): Int
|
fun getItemCount(): Int
|
||||||
fun getItemType(position: Int): Int
|
fun getItemType(position: Int): Int
|
||||||
fun getItem(position: Int): Any
|
fun getItem(position: Int): Any?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,14 +17,6 @@ import splitties.views.onLongClick
|
|||||||
class BooksAdapterGrid(context: Context, callBack: CallBack) :
|
class BooksAdapterGrid(context: Context, callBack: CallBack) :
|
||||||
BaseBooksAdapter<RecyclerView.ViewHolder>(context, 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(
|
override fun onCreateViewHolder(
|
||||||
parent: ViewGroup,
|
parent: ViewGroup,
|
||||||
viewType: Int
|
viewType: Int
|
||||||
@@ -67,7 +59,7 @@ class BooksAdapterGrid(context: Context, callBack: CallBack) :
|
|||||||
|
|
||||||
private fun onBindBook(binding: ItemBookshelfGridBinding, position: Int, bundle: Bundle) {
|
private fun onBindBook(binding: ItemBookshelfGridBinding, position: Int, bundle: Bundle) {
|
||||||
binding.run {
|
binding.run {
|
||||||
val item = callBack.getItem(position) as Book
|
val item = callBack.getItem(position) as? Book ?: return
|
||||||
bundle.keySet().forEach {
|
bundle.keySet().forEach {
|
||||||
when (it) {
|
when (it) {
|
||||||
"name" -> tvName.text = item.name
|
"name" -> tvName.text = item.name
|
||||||
|
|||||||
@@ -19,14 +19,6 @@ import splitties.views.onLongClick
|
|||||||
class BooksAdapterList(context: Context, callBack: CallBack) :
|
class BooksAdapterList(context: Context, callBack: CallBack) :
|
||||||
BaseBooksAdapter<RecyclerView.ViewHolder>(context, 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 {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
||||||
return when (viewType) {
|
return when (viewType) {
|
||||||
1 -> GroupViewHolder(
|
1 -> GroupViewHolder(
|
||||||
@@ -65,7 +57,7 @@ class BooksAdapterList(context: Context, callBack: CallBack) :
|
|||||||
|
|
||||||
private fun onBindBook(binding: ItemBookshelfListBinding, position: Int, bundle: Bundle) {
|
private fun onBindBook(binding: ItemBookshelfListBinding, position: Int, bundle: Bundle) {
|
||||||
binding.run {
|
binding.run {
|
||||||
val item = callBack.getItem(position) as Book
|
val item = callBack.getItem(position) as? Book ?: return
|
||||||
tvRead.text = item.durChapterTitle
|
tvRead.text = item.durChapterTitle
|
||||||
tvLast.text = item.latestChapterTitle
|
tvLast.text = item.latestChapterTitle
|
||||||
bundle.keySet().forEach {
|
bundle.keySet().forEach {
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
|
|||||||
BaseBooksAdapter.CallBack {
|
BaseBooksAdapter.CallBack {
|
||||||
|
|
||||||
private val binding by viewBinding(FragmentBookshelf1Binding::bind)
|
private val binding by viewBinding(FragmentBookshelf1Binding::bind)
|
||||||
|
private val rootGroupId = -100L
|
||||||
private val bookshelfLayout by lazy {
|
private val bookshelfLayout by lazy {
|
||||||
getPrefInt(PreferKey.bookshelfLayout)
|
getPrefInt(PreferKey.bookshelfLayout)
|
||||||
}
|
}
|
||||||
@@ -55,7 +56,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
|
|||||||
}
|
}
|
||||||
private var bookGroups: List<BookGroup> = emptyList()
|
private var bookGroups: List<BookGroup> = emptyList()
|
||||||
private var booksFlowJob: Job? = null
|
private var booksFlowJob: Job? = null
|
||||||
override var groupId = AppConst.bookGroupNoneId
|
override var groupId = rootGroupId
|
||||||
override var books: List<Book> = emptyList()
|
override var books: List<Book> = emptyList()
|
||||||
|
|
||||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
@@ -108,7 +109,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
|
|||||||
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
private fun initBooksData() {
|
private fun initBooksData() {
|
||||||
if (groupId == AppConst.bookGroupNoneId) {
|
if (groupId == -100L) {
|
||||||
binding.titleBar.title = getString(R.string.bookshelf)
|
binding.titleBar.title = getString(R.string.bookshelf)
|
||||||
} else {
|
} else {
|
||||||
bookGroups.forEach {
|
bookGroups.forEach {
|
||||||
@@ -120,6 +121,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
|
|||||||
booksFlowJob?.cancel()
|
booksFlowJob?.cancel()
|
||||||
booksFlowJob = launch {
|
booksFlowJob = launch {
|
||||||
when (groupId) {
|
when (groupId) {
|
||||||
|
rootGroupId -> appDb.bookDao.flowRoot()
|
||||||
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
|
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
|
||||||
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
|
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
|
||||||
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
|
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
|
||||||
@@ -152,8 +154,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun back(): Boolean {
|
fun back(): Boolean {
|
||||||
if (groupId != AppConst.bookGroupNoneId) {
|
if (groupId != -100L) {
|
||||||
groupId = AppConst.bookGroupNoneId
|
groupId = -100L
|
||||||
initBooksData()
|
initBooksData()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -210,7 +212,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemCount(): Int {
|
override fun getItemCount(): Int {
|
||||||
return if (groupId == AppConst.bookGroupNoneId) {
|
return if (groupId == rootGroupId) {
|
||||||
bookGroups.size + books.size
|
bookGroups.size + books.size
|
||||||
} else {
|
} else {
|
||||||
books.size
|
books.size
|
||||||
@@ -218,23 +220,23 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemType(position: Int): Int {
|
override fun getItemType(position: Int): Int {
|
||||||
return if (groupId == AppConst.bookGroupNoneId) {
|
if (groupId != rootGroupId) {
|
||||||
if (position < bookGroups.size) 1 else 0
|
return 0
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
if (position < bookGroups.size) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getItem(position: Int): Any {
|
override fun getItem(position: Int): Any? {
|
||||||
return if (groupId == AppConst.bookGroupNoneId) {
|
if (groupId != rootGroupId) {
|
||||||
if (position < bookGroups.size) {
|
return books.getOrNull(position)
|
||||||
bookGroups[position]
|
|
||||||
} else {
|
|
||||||
books[position - bookGroups.size]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
books[position]
|
|
||||||
}
|
}
|
||||||
|
if (position < bookGroups.size) {
|
||||||
|
return bookGroups[position]
|
||||||
|
}
|
||||||
|
return books.getOrNull(position - bookGroups.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
|
|||||||
Reference in New Issue
Block a user