From c6191403b7fd10bc1afc8f17ceabe136d6078976 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 24 Sep 2021 23:28:09 +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/changecover/ChangeCoverDialog.kt | 18 ++++++++++++------ .../book/changecover/ChangeCoverViewModel.kt | 12 +++++++++--- .../ui/book/changesource/ChangeSourceDialog.kt | 16 +++++++++++----- app/src/main/res/menu/change_cover.xml | 2 +- app/src/main/res/menu/change_source.xml | 2 +- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt index bd0fcc305..617ed7ae9 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt @@ -33,6 +33,9 @@ class ChangeCoverDialog() : BaseDialogFragment(), private val viewModel: ChangeCoverViewModel by viewModels() private val adapter by lazy { CoverAdapter(requireContext(), this) } + private val startStopMenuItem: MenuItem? + get() = binding.toolBar.menu.findItem(R.id.menu_start_stop) + override fun onStart() { super.onStart() val dm = requireActivity().windowSize @@ -73,9 +76,15 @@ class ChangeCoverDialog() : BaseDialogFragment(), viewModel.searchStateData.observe(viewLifecycleOwner, { binding.refreshProgressBar.isAutoLoading = it if (it) { - stopMenuItem?.setIcon(R.drawable.ic_stop_black_24dp) + startStopMenuItem?.let { item -> + item.setIcon(R.drawable.ic_stop_black_24dp) + item.setTitle(R.string.stop) + } } else { - stopMenuItem?.setIcon(R.drawable.ic_refresh_black_24dp) + startStopMenuItem?.let { item -> + item.setIcon(R.drawable.ic_refresh_black_24dp) + item.setTitle(R.string.refresh) + } } binding.toolBar.menu.applyTint(requireContext()) }) @@ -86,14 +95,11 @@ class ChangeCoverDialog() : BaseDialogFragment(), override fun onMenuItemClick(item: MenuItem?): Boolean { when (item?.itemId) { - R.id.menu_stop -> viewModel.stopSearch() + R.id.menu_start_stop -> viewModel.startOrStopSearch() } return false } - private val stopMenuItem: MenuItem? - get() = binding.toolBar.menu.findItem(R.id.menu_stop) - override fun changeTo(coverUrl: String) { callBack?.coverChangeTo(coverUrl) dismissAllowingStateLoss() diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt index b6b731b3d..9c39f7375 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt @@ -81,6 +81,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application private fun startSearch() { execute { + stopSearch() bookSourceList.clear() bookSourceList.addAll(appDb.bookSourceDao.allEnabled) searchStateData.postValue(true) @@ -142,15 +143,20 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application } } - fun stopSearch() { + fun startOrStopSearch() { if (tasks.isEmpty) { startSearch() } else { - tasks.clear() - searchStateData.postValue(false) + stopSearch() } } + fun stopSearch() { + tasks.clear() + searchPool?.close() + searchStateData.postValue(false) + } + override fun onCleared() { super.onCleared() searchPool?.close() 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 c5de815b6..44425d977 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 @@ -140,9 +140,15 @@ class ChangeSourceDialog() : BaseDialogFragment(), viewModel.searchStateData.observe(viewLifecycleOwner, { binding.refreshProgressBar.isAutoLoading = it if (it) { - stopMenuItem?.setIcon(R.drawable.ic_stop_black_24dp) + startStopMenuItem?.let { item -> + item.setIcon(R.drawable.ic_stop_black_24dp) + item.setTitle(R.string.stop) + } } else { - stopMenuItem?.setIcon(R.drawable.ic_refresh_black_24dp) + startStopMenuItem?.let { item -> + item.setIcon(R.drawable.ic_refresh_black_24dp) + item.setTitle(R.string.refresh) + } } binding.toolBar.menu.applyTint(requireContext()) }) @@ -160,8 +166,8 @@ class ChangeSourceDialog() : BaseDialogFragment(), } } - private val stopMenuItem: MenuItem? - get() = binding.toolBar.menu.findItem(R.id.menu_stop) + private val startStopMenuItem: MenuItem? + get() = binding.toolBar.menu.findItem(R.id.menu_start_stop) override fun onMenuItemClick(item: MenuItem?): Boolean { when (item?.itemId) { @@ -178,7 +184,7 @@ class ChangeSourceDialog() : BaseDialogFragment(), putPrefBoolean(PreferKey.changeSourceLoadInfo, !item.isChecked) item.isChecked = !item.isChecked } - R.id.menu_stop -> viewModel.startOrStopSearch() + R.id.menu_start_stop -> viewModel.startOrStopSearch() R.id.menu_source_manage -> startActivity() else -> if (item?.groupId == R.id.source_group) { if (!item.isChecked) { diff --git a/app/src/main/res/menu/change_cover.xml b/app/src/main/res/menu/change_cover.xml index bf7d0d51b..a2a2407a1 100644 --- a/app/src/main/res/menu/change_cover.xml +++ b/app/src/main/res/menu/change_cover.xml @@ -4,7 +4,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto">