pull/1649/head
kunfei 3 years ago
parent 797e236bf3
commit b0b2cc302b
  1. 5
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt
  2. 6
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt

@ -4,6 +4,7 @@ import android.app.Application
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import cn.hutool.core.collection.ConcurrentHashSet
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
@ -33,7 +34,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
var name: String = "" var name: String = ""
var author: String = "" var author: String = ""
val dataFlow = callbackFlow<List<SearchBook>> { val dataFlow = callbackFlow<List<SearchBook>> {
val searchBooks = arrayListOf<SearchBook>() val searchBooks = ConcurrentHashSet<SearchBook>()
searchSuccess = { searchSuccess = {
if (!searchBooks.contains(it)) { if (!searchBooks.contains(it)) {
@ -44,7 +45,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
appDb.searchBookDao.getEnableHasCover(name, author).let { appDb.searchBookDao.getEnableHasCover(name, author).let {
searchBooks.addAll(it) searchBooks.addAll(it)
trySend(searchBooks) trySend(ArrayList(searchBooks))
} }
if (searchBooks.size <= 1) { if (searchBooks.size <= 1) {

@ -4,6 +4,7 @@ import android.app.Application
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import cn.hutool.core.collection.ConcurrentHashSet
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
@ -27,7 +28,6 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import splitties.init.appCtx import splitties.init.appCtx
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.min import kotlin.math.min
@ -41,7 +41,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
private var tasks = CompositeCoroutine() private var tasks = CompositeCoroutine()
private var screenKey: String = "" private var screenKey: String = ""
private var bookSourceList = arrayListOf<BookSource>() private var bookSourceList = arrayListOf<BookSource>()
private val searchBooks = CopyOnWriteArrayList<SearchBook>() private val searchBooks = ConcurrentHashSet<SearchBook>()
private val searchGroup get() = appCtx.getPrefString("searchGroup") ?: "" private val searchGroup get() = appCtx.getPrefString("searchGroup") ?: ""
private var searchCallback: SourceCallback? = null private var searchCallback: SourceCallback? = null
val searchDataFlow = callbackFlow { val searchDataFlow = callbackFlow {
@ -50,9 +50,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
override fun searchSuccess(searchBook: SearchBook) { override fun searchSuccess(searchBook: SearchBook) {
appDb.searchBookDao.insert(searchBook) appDb.searchBookDao.insert(searchBook)
val index = searchBooks.indexOf(searchBook)
when { when {
index >= 0 -> searchBooks[index] = searchBook
screenKey.isEmpty() -> searchBooks.add(searchBook) screenKey.isEmpty() -> searchBooks.add(searchBook)
searchBook.name.contains(screenKey) -> searchBooks.add(searchBook) searchBook.name.contains(screenKey) -> searchBooks.add(searchBook)
else -> return else -> return

Loading…
Cancel
Save