diff --git a/app/src/main/java/io/legado/app/ui/changecover/ChangeCoverDialog.kt b/app/src/main/java/io/legado/app/ui/changecover/ChangeCoverDialog.kt index cc8554a7f..24ecbd7f0 100644 --- a/app/src/main/java/io/legado/app/ui/changecover/ChangeCoverDialog.kt +++ b/app/src/main/java/io/legado/app/ui/changecover/ChangeCoverDialog.kt @@ -7,6 +7,7 @@ import android.view.View import android.view.ViewGroup import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentManager +import androidx.lifecycle.Observer import androidx.recyclerview.widget.GridLayoutManager import io.legado.app.R import io.legado.app.utils.getViewModel @@ -46,6 +47,7 @@ class ChangeCoverDialog : DialogFragment(), ChangeCoverViewModel.CallBack { container: ViewGroup?, savedInstanceState: Bundle? ): View? { + callBack = activity as? CallBack viewModel = getViewModel(ChangeCoverViewModel::class.java) viewModel.callBack = this return inflater.inflate(R.layout.dialog_change_source, container) @@ -53,7 +55,9 @@ class ChangeCoverDialog : DialogFragment(), ChangeCoverViewModel.CallBack { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - callBack = activity as? CallBack + viewModel.searchStateData.observe(this, Observer { + refresh_progress_bar.isAutoLoading = it + }) tool_bar.setTitle(R.string.change_cover_source) arguments?.let { bundle -> bundle.getString("name")?.let { diff --git a/app/src/main/java/io/legado/app/ui/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/changecover/ChangeCoverViewModel.kt index 6c5fb759b..066fdf99c 100644 --- a/app/src/main/java/io/legado/app/ui/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/changecover/ChangeCoverViewModel.kt @@ -1,9 +1,13 @@ package io.legado.app.ui.changecover import android.app.Application +import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.help.AppConfig +import io.legado.app.help.coroutine.Coroutine +import io.legado.app.model.WebBook +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.asCoroutineDispatcher import java.util.concurrent.Executors @@ -13,17 +17,44 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application var callBack: CallBack? = null var name: String = "" var author: String = "" + private var task: Coroutine<*>? = null + val searchStateData = MutableLiveData() fun initData() { execute { App.db.searchBookDao().getEnableHasCover(name, author).let { callBack?.adapter?.setItems(it) + search() } } } fun search() { + task = execute { + searchStateData.postValue(true) + val bookSourceList = App.db.bookSourceDao().allEnabled + for (item in bookSourceList) { + //task取消时自动取消 by (scope = this@execute) + WebBook(item).searchBook(name, scope = this@execute, context = searchPool) + .timeout(30000L) + .onSuccess(Dispatchers.IO) { + if (it != null && it.isNotEmpty()) { + val searchBook = it[0] + if (searchBook.name == name && searchBook.author == author) { + callBack?.adapter?.let { adapter -> + if (!adapter.getItems().contains(searchBook)) { + adapter.addItem(searchBook) + } + } + } + } + } + } + } + task?.invokeOnCompletion { + searchStateData.postValue(false) + } } override fun onCleared() { diff --git a/app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt b/app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt index 0290d7749..ba250a8a2 100644 --- a/app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt @@ -7,6 +7,7 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.data.entities.SearchBook import io.legado.app.help.ImageLoader import kotlinx.android.synthetic.main.item_cover.view.* +import org.jetbrains.anko.sdk27.listeners.onClick class CoverAdapter(context: Context) : SimpleRecyclerAdapter(context, R.layout.item_cover) { @@ -19,6 +20,9 @@ class CoverAdapter(context: Context) : .into(iv_cover) } tv_source.text = item.originName + onClick { + + } } } diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt index be039e6d0..c05e171b1 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt @@ -54,6 +54,7 @@ class ChangeSourceDialog : DialogFragment(), container: ViewGroup?, savedInstanceState: Bundle? ): View? { + callBack = activity as? CallBack viewModel = getViewModel(ChangeSourceViewModel::class.java) viewModel.callBack = this return inflater.inflate(R.layout.dialog_change_source, container) @@ -61,7 +62,6 @@ class ChangeSourceDialog : DialogFragment(), override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - callBack = activity as? CallBack viewModel.searchStateData.observe(viewLifecycleOwner, Observer { refresh_progress_bar.isAutoLoading = it })