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 { override fun isInBookshelf(name: String, author: String): Boolean {
return if (author.isNotBlank()) { return viewModel.isInBookShelf(name, author)
viewModel.bookshelf.contains("$name-$author")
} else {
viewModel.bookshelf.any { it.startsWith("$name-") }
}
} }
override fun showBookInfo(book: Book) { 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.Dispatchers.IO
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.mapLatest
import java.util.Collections import java.util.concurrent.ConcurrentHashMap
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
class ExploreShowViewModel(application: Application) : BaseViewModel(application) { 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 upAdapterLiveData = MutableLiveData<String>()
val booksData = MutableLiveData<List<SearchBook>>() val booksData = MutableLiveData<List<SearchBook>>()
val errorLiveData = MutableLiveData<String>() val errorLiveData = MutableLiveData<String>()
@ -32,7 +32,12 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
init { init {
execute { execute {
appDb.bookDao.flowAll().mapLatest { books -> 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 { }.collect {
bookshelf.clear() bookshelf.clear()
bookshelf.addAll(it) 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 { override fun isInBookshelf(name: String, author: String): Boolean {
return if (author.isNotBlank()) { return viewModel.isInBookShelf(name, author)
viewModel.bookshelf.contains("$name-$author")
} else {
viewModel.bookshelf.any { it.startsWith("$name-") }
}
} }
/** /**

View File

@ -16,12 +16,12 @@ import io.legado.app.utils.ConflateLiveData
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.mapLatest
import java.util.Collections import java.util.concurrent.ConcurrentHashMap
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
val handler = Handler(Looper.getMainLooper()) val handler = Handler(Looper.getMainLooper())
val bookshelf: MutableSet<String> = Collections.synchronizedSet(hashSetOf<String>()) val bookshelf: MutableSet<String> = ConcurrentHashMap.newKeySet()
val upAdapterLiveData = MutableLiveData<String>() val upAdapterLiveData = MutableLiveData<String>()
var searchBookLiveData = ConflateLiveData<List<SearchBook>>(1000) var searchBookLiveData = ConflateLiveData<List<SearchBook>>(1000)
val searchScope: SearchScope = SearchScope(AppConfig.searchScope) val searchScope: SearchScope = SearchScope(AppConfig.searchScope)
@ -60,7 +60,12 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
init { init {
execute { execute {
appDb.bookDao.flowAll().mapLatest { books -> 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 { }.collect {
bookshelf.clear() bookshelf.clear()
bookshelf.addAll(it) 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) { fun saveSearchKey(key: String) {
execute { execute {
appDb.searchKeywordDao.get(key)?.let { appDb.searchKeywordDao.get(key)?.let {
it.usage = it.usage + 1 it.usage += 1
it.lastUseTime = System.currentTimeMillis() it.lastUseTime = System.currentTimeMillis()
appDb.searchKeywordDao.update(it) appDb.searchKeywordDao.update(it)
} ?: appDb.searchKeywordDao.insert(SearchKeyword(key, 1)) } ?: appDb.searchKeywordDao.insert(SearchKeyword(key, 1))