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

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

Loading…
Cancel
Save