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 197f62fe4..85df58172 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 @@ -3,19 +3,24 @@ package io.legado.app.ui.book.changecover import android.os.Bundle import android.util.DisplayMetrics import android.view.LayoutInflater +import android.view.MenuItem import android.view.View import android.view.ViewGroup +import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager import androidx.lifecycle.Observer import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.GridLayoutManager import io.legado.app.R import io.legado.app.base.BaseDialogFragment +import io.legado.app.constant.Theme +import io.legado.app.utils.applyTint import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.dialog_change_source.* class ChangeCoverDialog : BaseDialogFragment(), + Toolbar.OnMenuItemClickListener, CoverAdapter.CallBack { companion object { @@ -55,13 +60,19 @@ class ChangeCoverDialog : BaseDialogFragment(), } override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { - viewModel.searchStateData.observe(viewLifecycleOwner, Observer { - refresh_progress_bar.isAutoLoading = it - }) tool_bar.setTitle(R.string.change_cover_source) - arguments?.let { bundle -> - viewModel.initData(bundle) - } + viewModel.initData(arguments) + initMenu() + initView() + } + + private fun initMenu() { + tool_bar.inflateMenu(R.menu.change_cover) + tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) + tool_bar.setOnMenuItemClickListener(this) + } + + private fun initView() { recycler_view.layoutManager = GridLayoutManager(requireContext(), 3) adapter = CoverAdapter(requireContext(), this) recycler_view.adapter = adapter @@ -70,6 +81,15 @@ class ChangeCoverDialog : BaseDialogFragment(), override fun observeLiveBus() { super.observeLiveBus() + viewModel.searchStateData.observe(viewLifecycleOwner, Observer { + refresh_progress_bar.isAutoLoading = it + if (it) { + stopMenuItem?.setIcon(R.drawable.ic_stop_black_24dp) + } else { + stopMenuItem?.setIcon(R.drawable.ic_refresh_black_24dp) + } + tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) + }) viewModel.searchBooksLiveData.observe(viewLifecycleOwner, Observer { val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), it)) adapter.setItems(it) @@ -77,6 +97,16 @@ class ChangeCoverDialog : BaseDialogFragment(), }) } + override fun onMenuItemClick(item: MenuItem?): Boolean { + when (item?.itemId) { + R.id.menu_stop -> viewModel.stopSearch() + } + return false + } + + private val stopMenuItem: MenuItem? + get() = tool_bar.menu.findItem(R.id.menu_stop) + override fun changeTo(coverUrl: String) { callBack?.coverChangeTo(coverUrl) dismiss() 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 ad1b1a8df..627bacc09 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 @@ -24,12 +24,14 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application val searchBooksLiveData = MutableLiveData>() private val searchBooks = ArrayList() - fun initData(bundle: Bundle) { - bundle.getString("name")?.let { - name = it - } - bundle.getString("author")?.let { - author = it.replace(AppPattern.authorRegex, "") + fun initData(arguments: Bundle?) { + arguments?.let { bundle -> + bundle.getString("name")?.let { + name = it + } + bundle.getString("author")?.let { + author = it.replace(AppPattern.authorRegex, "") + } } } @@ -77,6 +79,14 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application } } + fun stopSearch() { + if (task?.isActive == true) { + task?.cancel() + } else { + search() + } + } + override fun onCleared() { super.onCleared() searchPool.close() diff --git a/app/src/main/res/menu/change_cover.xml b/app/src/main/res/menu/change_cover.xml new file mode 100644 index 000000000..cce382d12 --- /dev/null +++ b/app/src/main/res/menu/change_cover.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 966353f4e..cc7adbeb8 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -163,4 +163,10 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings_yxd.xml b/app/src/main/res/values/strings_yxd.xml deleted file mode 100644 index d1ca165ed..000000000 --- a/app/src/main/res/values/strings_yxd.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file