Horis 2 лет назад
Родитель
Сommit
fbc3f5d39c

+ 1 - 5
app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt

@@ -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) {

+ 16 - 3
app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt

@@ -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)
+        }
+    }
+
 }

+ 1 - 5
app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt

@@ -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)
     }
 
     /**

+ 17 - 4
app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@@ -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))