diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt index 906b2ba81..c5de815b6 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.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() 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() } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index d1680727e..84a7cafc3 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -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()