[优化] 优化代码
This commit is contained in:
@@ -13,6 +13,7 @@ import io.legado.app.data.entities.BookGroup
|
||||
import io.legado.app.data.entities.BookSource
|
||||
import io.legado.app.help.book.isNotShelf
|
||||
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.map
|
||||
|
||||
@@ -39,6 +40,12 @@ interface BookDao {
|
||||
}
|
||||
}
|
||||
|
||||
fun flowBookShelfByGroup(groupId: Long): Flow<List<BookShelfItem>> {
|
||||
return flowByGroup(groupId).map { list ->
|
||||
list.map { it.toBookShelfItem() }
|
||||
}
|
||||
}
|
||||
|
||||
@Query(
|
||||
"""
|
||||
select * from books where type & ${BookType.text} > 0
|
||||
|
||||
@@ -123,16 +123,15 @@ class BookshelfViewModel(
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
||||
|
||||
private val allBooksFlow = appDb.bookDao.flowBookShelf()
|
||||
.map { list -> list.map { it.toBookShelfItem() } }
|
||||
.distinctUntilChanged()
|
||||
.flowOn(Dispatchers.Default)
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
val booksFlow = combine(groupIdFlow, refreshTrigger) { groupId, _ -> groupId }
|
||||
.flatMapLatest { groupId ->
|
||||
appDb.bookDao.flowByGroup(groupId).map { list ->
|
||||
appDb.bookDao.flowBookShelfByGroup(groupId).map { list ->
|
||||
sortBooks(
|
||||
list.map { it.toBookShelfItem() },
|
||||
list,
|
||||
groupsFlow.value.find { it.groupId == groupId })
|
||||
}
|
||||
}.distinctUntilChanged().flowOn(Dispatchers.Default)
|
||||
@@ -310,17 +309,16 @@ class BookshelfViewModel(
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
fun getBooksFlow(groupId: Long): Flow<List<BookShelfItem>> {
|
||||
return combine(
|
||||
appDb.bookDao.flowByGroup(groupId),
|
||||
appDb.bookDao.flowBookShelfByGroup(groupId),
|
||||
searchKeyFlow,
|
||||
groupsFlow,
|
||||
refreshTrigger
|
||||
) { books, searchKey, groups, _ ->
|
||||
val group = groups.find { it.groupId == groupId }
|
||||
val bookshelfItems = books.map { it.toBookShelfItem() }
|
||||
val filtered = if (searchKey.isEmpty()) {
|
||||
bookshelfItems
|
||||
books
|
||||
} else {
|
||||
bookshelfItems.filter {
|
||||
books.filter {
|
||||
it.name.contains(searchKey, true) || it.author.contains(searchKey, true)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user