pull/1352/head
gedoor 3 years ago
parent e9e55dfa1a
commit c1727d5373
  1. 6
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt
  2. 16
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt

@ -47,7 +47,7 @@ class ChangeSourceDialog() : BaseDialogFragment(),
private val adapter by lazy { ChangeSourceAdapter(requireContext(), viewModel, this) }
private val editSourceResult =
registerForActivityResult(StartActivityForResult(BookSourceEditActivity::class.java)) {
viewModel.startSearch()
}
@ -178,7 +178,7 @@ class ChangeSourceDialog() : BaseDialogFragment(),
putPrefBoolean(PreferKey.changeSourceLoadInfo, !item.isChecked)
item.isChecked = !item.isChecked
}
R.id.menu_stop -> viewModel.stopSearch()
R.id.menu_stop -> viewModel.startOrStopSearch()
R.id.menu_source_manage -> startActivity<BookSourceActivity>()
else -> if (item?.groupId == R.id.source_group) {
if (!item.isChecked) {
@ -188,7 +188,7 @@ class ChangeSourceDialog() : BaseDialogFragment(),
} else {
putPrefString("searchGroup", item.title.toString())
}
viewModel.stopSearch()
viewModel.startOrStopSearch()
viewModel.loadDbSearchBook()
}
}

@ -25,6 +25,7 @@ import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.Executors
import kotlin.math.min
@Suppress("MemberVisibilityCanBePrivate")
class ChangeSourceViewModel(application: Application) : BaseViewModel(application) {
private val threadCount = AppConfig.threadCount
private var searchPool: ExecutorCoroutineDispatcher? = null
@ -104,8 +105,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
upAdapter()
}
private fun startSearch() {
fun startSearch() {
execute {
stopSearch()
appDb.searchBookDao.clear(name, author)
searchBooks.clear()
upAdapter()
@ -218,16 +220,20 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
}
}
fun stopSearch() {
fun startOrStopSearch() {
if (tasks.isEmpty) {
startSearch()
} else {
tasks.clear()
searchPool?.close()
searchStateData.postValue(false)
stopSearch()
}
}
fun stopSearch() {
tasks.clear()
searchPool?.close()
searchStateData.postValue(false)
}
override fun onCleared() {
super.onCleared()
searchPool?.close()

Loading…
Cancel
Save