From c1727d53736ab1dfcd27c02e3ead3975f8972b66 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 24 Sep 2021 22:30:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/changesource/ChangeSourceDialog.kt | 6 +++--- .../book/changesource/ChangeSourceViewModel.kt | 16 +++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) 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()