This commit is contained in:
Horis 2024-01-02 10:04:43 +08:00
parent b4626e87cf
commit e7698ddd88
4 changed files with 35 additions and 17 deletions

View File

@ -84,11 +84,7 @@ class ExploreShowActivity : VMBaseActivity<ActivityExploreShowBinding, ExploreSh
}
override fun isInBookshelf(name: String, author: String): Boolean {
return if (author.isNotBlank()) {
viewModel.bookshelf.contains("$name-$author")
} else {
viewModel.bookshelf.any { it.startsWith("$name-") }
}
return viewModel.isInBookShelf(name, author)
}
override fun showBookInfo(book: Book) {

View File

@ -16,12 +16,12 @@ import io.legado.app.utils.stackTraceStr
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.mapLatest
import java.util.Collections
import java.util.concurrent.ConcurrentHashMap
@OptIn(ExperimentalCoroutinesApi::class)
class ExploreShowViewModel(application: Application) : BaseViewModel(application) {
val bookshelf: MutableSet<String> = Collections.synchronizedSet(hashSetOf<String>())
val bookshelf: MutableSet<String> = ConcurrentHashMap.newKeySet()
val upAdapterLiveData = MutableLiveData<String>()
val booksData = MutableLiveData<List<SearchBook>>()
val errorLiveData = MutableLiveData<String>()
@ -32,7 +32,12 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
init {
execute {
appDb.bookDao.flowAll().mapLatest { books ->
books.map { "${it.name}-${it.author}" }
val keys = arrayListOf<String>()
books.forEach {
keys.add("${it.name}-${it.author}")
keys.add(it.name)
}
keys
}.collect {
bookshelf.clear()
bookshelf.addAll(it)
@ -70,4 +75,12 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
}
}
fun isInBookShelf(name: String, author: String): Boolean {
return if (author.isNotBlank()) {
bookshelf.contains("$name-$author")
} else {
bookshelf.contains(name)
}
}
}

View File

@ -416,11 +416,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
* 是否已经加入书架
*/
override fun isInBookshelf(name: String, author: String): Boolean {
return if (author.isNotBlank()) {
viewModel.bookshelf.contains("$name-$author")
} else {
viewModel.bookshelf.any { it.startsWith("$name-") }
}
return viewModel.isInBookShelf(name, author)
}
/**

View File

@ -16,12 +16,12 @@ import io.legado.app.utils.ConflateLiveData
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.mapLatest
import java.util.Collections
import java.util.concurrent.ConcurrentHashMap
@OptIn(ExperimentalCoroutinesApi::class)
class SearchViewModel(application: Application) : BaseViewModel(application) {
val handler = Handler(Looper.getMainLooper())
val bookshelf: MutableSet<String> = Collections.synchronizedSet(hashSetOf<String>())
val bookshelf: MutableSet<String> = ConcurrentHashMap.newKeySet()
val upAdapterLiveData = MutableLiveData<String>()
var searchBookLiveData = ConflateLiveData<List<SearchBook>>(1000)
val searchScope: SearchScope = SearchScope(AppConfig.searchScope)
@ -60,7 +60,12 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
init {
execute {
appDb.bookDao.flowAll().mapLatest { books ->
books.map { "${it.name}-${it.author}" }
val keys = arrayListOf<String>()
books.forEach {
keys.add("${it.name}-${it.author}")
keys.add(it.name)
}
keys
}.collect {
bookshelf.clear()
bookshelf.addAll(it)
@ -71,6 +76,14 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
}
}
fun isInBookShelf(name: String, author: String): Boolean {
return if (author.isNotBlank()) {
bookshelf.contains("$name-$author")
} else {
bookshelf.contains(name)
}
}
/**
* 开始搜索
*/
@ -101,7 +114,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
fun saveSearchKey(key: String) {
execute {
appDb.searchKeywordDao.get(key)?.let {
it.usage = it.usage + 1
it.usage += 1
it.lastUseTime = System.currentTimeMillis()
appDb.searchKeywordDao.update(it)
} ?: appDb.searchKeywordDao.insert(SearchKeyword(key, 1))