[优化] 优化代码

This commit is contained in:
HapeLee committed 2026-03-25 12:57:22 +08:00
1 parent 4888b82876
commit 28d206f134
3 files changed
+360 -4

No files matched your search

@@ -13,7 +13,6 @@ import io.legado.app.data.entities.BookGroup
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.book.isNotShelf import io.legado.app.help.book.isNotShelf
import io.legado.app.ui.main.bookshelf.BookShelfItem import io.legado.app.ui.main.bookshelf.BookShelfItem
import io.legado.app.ui.main.bookshelf.toBookShelfItem
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
@@ -41,8 +40,22 @@ interface BookDao {
} }
fun flowBookShelfByGroup(groupId: Long): Flow<List<BookShelfItem>> { fun flowBookShelfByGroup(groupId: Long): Flow<List<BookShelfItem>> {
return flowByGroup(groupId).map { list -> return when (groupId) {
list.map { it.toBookShelfItem() } BookGroup.IdRoot -> flowBookShelfRoot()
BookGroup.IdAll -> flowBookShelf()
BookGroup.IdLocal -> flowBookShelfLocal()
BookGroup.IdAudio -> flowBookShelfAudio()
BookGroup.IdNetNone -> flowBookShelfNetNoGroup()
BookGroup.IdLocalNone -> flowBookShelfLocalNoGroup()
BookGroup.IdManga -> flowBookShelfManga()
BookGroup.IdText -> flowBookShelfText()
BookGroup.IdError -> flowBookShelfUpdateError()
BookGroup.IdUnread -> flowBookShelfUnread()
BookGroup.IdReading -> flowBookShelfReading()
BookGroup.IdReadFinished -> flowBookShelfReadFinished()
else -> flowBookShelfByUserGroup(groupId)
}.map { list ->
list.filterNot { it.isNotShelf }
} }
} }
@@ -56,6 +69,34 @@ interface BookDao {
) )
fun flowRoot(): Flow<List<Book>> fun flowRoot(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
where type & ${BookType.text} > 0
and type & ${BookType.local} = 0
and ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0
and (select show from book_groups where groupId = ${BookGroup.IdNetNone}) != 1
"""
)
fun flowBookShelfRoot(): Flow<List<BookShelfItem>>
@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>>
@@ -87,9 +128,59 @@ interface BookDao {
@Query("SELECT * FROM books WHERE type & ${BookType.audio} > 0") @Query("SELECT * FROM books WHERE type & ${BookType.audio} > 0")
fun flowAudio(): Flow<List<Book>> fun flowAudio(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
WHERE type & ${BookType.audio} > 0
"""
)
fun flowBookShelfAudio(): Flow<List<BookShelfItem>>
@Query("SELECT * FROM books WHERE type & ${BookType.local} > 0") @Query("SELECT * FROM books WHERE type & ${BookType.local} > 0")
fun flowLocal(): Flow<List<Book>> fun flowLocal(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
WHERE type & ${BookType.local} > 0
"""
)
fun flowBookShelfLocal(): Flow<List<BookShelfItem>>
@Query( @Query(
""" """
select * from books where type & ${BookType.audio} = 0 and type & ${BookType.local} = 0 select * from books where type & ${BookType.audio} = 0 and type & ${BookType.local} = 0
@@ -98,6 +189,32 @@ interface BookDao {
) )
fun flowNetNoGroup(): Flow<List<Book>> fun flowNetNoGroup(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
where type & ${BookType.audio} = 0 and type & ${BookType.local} = 0
and ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0
"""
)
fun flowBookShelfNetNoGroup(): Flow<List<BookShelfItem>>
@Query( @Query(
""" """
select * from books where type & ${BookType.local} > 0 select * from books where type & ${BookType.local} > 0
@@ -106,30 +223,257 @@ interface BookDao {
) )
fun flowLocalNoGroup(): Flow<List<Book>> fun flowLocalNoGroup(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
where type & ${BookType.local} > 0
and ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0
"""
)
fun flowBookShelfLocalNoGroup(): Flow<List<BookShelfItem>>
@Query("SELECT * FROM books WHERE (`group` & :group) > 0") @Query("SELECT * FROM books WHERE (`group` & :group) > 0")
fun flowByUserGroup(group: Long): Flow<List<Book>> fun flowByUserGroup(group: Long): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
WHERE (`group` & :group) > 0
"""
)
fun flowBookShelfByUserGroup(group: Long): Flow<List<BookShelfItem>>
@Query("SELECT * FROM books WHERE name like '%'||:key||'%' or author like '%'||:key||'%'") @Query("SELECT * FROM books WHERE name like '%'||:key||'%' or author like '%'||:key||'%'")
fun flowSearch(key: String): Flow<List<Book>> fun flowSearch(key: String): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
WHERE name like '%'||:key||'%' or author like '%'||:key||'%'
"""
)
fun flowBookShelfSearch(key: String): Flow<List<BookShelfItem>>
@Query("SELECT * FROM books where type & ${BookType.updateError} > 0 order by durChapterTime desc") @Query("SELECT * FROM books where type & ${BookType.updateError} > 0 order by durChapterTime desc")
fun flowUpdateError(): Flow<List<Book>> fun flowUpdateError(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
where type & ${BookType.updateError} > 0
order by durChapterTime desc
"""
)
fun flowBookShelfUpdateError(): Flow<List<BookShelfItem>>
@Query("""SELECT * FROM books WHERE durChapterIndex = 0 AND durChapterPos = 0""") @Query("""SELECT * FROM books WHERE durChapterIndex = 0 AND durChapterPos = 0""")
fun flowUnread(): Flow<List<Book>> fun flowUnread(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
WHERE durChapterIndex = 0 AND durChapterPos = 0
"""
)
fun flowBookShelfUnread(): Flow<List<BookShelfItem>>
@Query("""SELECT * FROM books WHERE totalChapterNum > 0 AND durChapterIndex >= totalChapterNum - 1""") @Query("""SELECT * FROM books WHERE totalChapterNum > 0 AND durChapterIndex >= totalChapterNum - 1""")
fun flowReadFinished(): Flow<List<Book>> fun flowReadFinished(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
WHERE totalChapterNum > 0 AND durChapterIndex >= totalChapterNum - 1
"""
)
fun flowBookShelfReadFinished(): Flow<List<BookShelfItem>>
@Query("""SELECT * FROM books WHERE totalChapterNum > 0 AND durChapterIndex > 0 AND durChapterIndex < totalChapterNum - 1""") @Query("""SELECT * FROM books WHERE totalChapterNum > 0 AND durChapterIndex > 0 AND durChapterIndex < totalChapterNum - 1""")
fun flowReading(): Flow<List<Book>> fun flowReading(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
WHERE totalChapterNum > 0 AND durChapterIndex > 0 AND durChapterIndex < totalChapterNum - 1
"""
)
fun flowBookShelfReading(): Flow<List<BookShelfItem>>
@Query("SELECT * FROM books WHERE type & ${BookType.image} > 0") @Query("SELECT * FROM books WHERE type & ${BookType.image} > 0")
fun flowManga(): Flow<List<Book>> fun flowManga(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
WHERE type & ${BookType.image} > 0
"""
)
fun flowBookShelfManga(): Flow<List<BookShelfItem>>
@Query("SELECT * FROM books WHERE type & ${BookType.text} > 0") @Query("SELECT * FROM books WHERE type & ${BookType.text} > 0")
fun flowText(): Flow<List<Book>> fun flowText(): Flow<List<Book>>
@Query(
"""
SELECT
bookUrl,
name,
author,
coverUrl,
customCoverUrl,
durChapterTitle,
durChapterTime,
durChapterPos,
latestChapterTitle,
latestChapterTime,
totalChapterNum,
durChapterIndex,
type,
`group`,
`order`,
canUpdate
FROM books
WHERE type & ${BookType.text} > 0
"""
)
fun flowBookShelfText(): Flow<List<BookShelfItem>>
@Query("SELECT * FROM books WHERE (`group` & :group) > 0") @Query("SELECT * FROM books WHERE (`group` & :group) > 0")
fun getBooksByGroup(group: Long): List<Book> fun getBooksByGroup(group: Long): List<Book>
@@ -77,16 +77,26 @@ object BookCover {
return paths[random.nextInt(paths.size)] return paths[random.nextInt(paths.size)]
} }
// 缓存随机封面 Drawable,避免重复解码
private val randomDrawableCache = mutableMapOf<String, Drawable>()
fun getRandomDefaultDrawable( fun getRandomDefaultDrawable(
seed: Any? = null, seed: Any? = null,
isNight: Boolean = AppConfig.isNightTheme isNight: Boolean = AppConfig.isNightTheme
): Drawable { ): Drawable {
val randomPath = getRandomDefaultPath(seed, isNight) val randomPath = getRandomDefaultPath(seed, isNight)
?: return appCtx.resources.getDrawable(R.drawable.image_cover_default, null) ?: return appCtx.resources.getDrawable(R.drawable.image_cover_default, null)
return kotlin.runCatching {
// 生成缓存键
val cacheKey = "$randomPath-${isNight}"
// 从缓存中获取,如果没有则解码并缓存
return randomDrawableCache.getOrPut(cacheKey) {
kotlin.runCatching {
BitmapUtils.decodeBitmap(randomPath, 600, 900)!!.toDrawable(appCtx.resources) BitmapUtils.decodeBitmap(randomPath, 600, 900)!!.toDrawable(appCtx.resources)
}.getOrDefault(appCtx.resources.getDrawable(R.drawable.image_cover_default, null)) }.getOrDefault(appCtx.resources.getDrawable(R.drawable.image_cover_default, null))
} }
}
/** /**
* 加载封面 * 加载封面
@@ -32,6 +32,8 @@ data class BookShelfItem(
val isImage: Boolean get() = (type and BookType.image) > 0 val isImage: Boolean get() = (type and BookType.image) > 0
val isNotShelf: Boolean get() = (type and BookType.notShelf) > 0
fun getUnreadChapterNum() = max(totalChapterNum - durChapterIndex - 1, 0) fun getUnreadChapterNum() = max(totalChapterNum - durChapterIndex - 1, 0)
} }