pull/84/head
kunfei 5 years ago
parent f09dda0e8a
commit d1d91d95d7
  1. 6
      app/src/main/java/io/legado/app/ui/changecover/ChangeCoverDialog.kt
  2. 31
      app/src/main/java/io/legado/app/ui/changecover/ChangeCoverViewModel.kt
  3. 4
      app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt
  4. 2
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt

@ -7,6 +7,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
@ -46,6 +47,7 @@ class ChangeCoverDialog : DialogFragment(), ChangeCoverViewModel.CallBack {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
callBack = activity as? CallBack
viewModel = getViewModel(ChangeCoverViewModel::class.java) viewModel = getViewModel(ChangeCoverViewModel::class.java)
viewModel.callBack = this viewModel.callBack = this
return inflater.inflate(R.layout.dialog_change_source, container) 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?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) 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) tool_bar.setTitle(R.string.change_cover_source)
arguments?.let { bundle -> arguments?.let { bundle ->
bundle.getString("name")?.let { bundle.getString("name")?.let {

@ -1,9 +1,13 @@
package io.legado.app.ui.changecover package io.legado.app.ui.changecover
import android.app.Application import android.app.Application
import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.help.AppConfig 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 kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -13,17 +17,44 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
var callBack: CallBack? = null var callBack: CallBack? = null
var name: String = "" var name: String = ""
var author: String = "" var author: String = ""
private var task: Coroutine<*>? = null
val searchStateData = MutableLiveData<Boolean>()
fun initData() { fun initData() {
execute { execute {
App.db.searchBookDao().getEnableHasCover(name, author).let { App.db.searchBookDao().getEnableHasCover(name, author).let {
callBack?.adapter?.setItems(it) callBack?.adapter?.setItems(it)
search()
} }
} }
} }
fun 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() { override fun onCleared() {

@ -7,6 +7,7 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
import kotlinx.android.synthetic.main.item_cover.view.* import kotlinx.android.synthetic.main.item_cover.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
class CoverAdapter(context: Context) : class CoverAdapter(context: Context) :
SimpleRecyclerAdapter<SearchBook>(context, R.layout.item_cover) { SimpleRecyclerAdapter<SearchBook>(context, R.layout.item_cover) {
@ -19,6 +20,9 @@ class CoverAdapter(context: Context) :
.into(iv_cover) .into(iv_cover)
} }
tv_source.text = item.originName tv_source.text = item.originName
onClick {
}
} }
} }

@ -54,6 +54,7 @@ class ChangeSourceDialog : DialogFragment(),
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
callBack = activity as? CallBack
viewModel = getViewModel(ChangeSourceViewModel::class.java) viewModel = getViewModel(ChangeSourceViewModel::class.java)
viewModel.callBack = this viewModel.callBack = this
return inflater.inflate(R.layout.dialog_change_source, container) return inflater.inflate(R.layout.dialog_change_source, container)
@ -61,7 +62,6 @@ class ChangeSourceDialog : DialogFragment(),
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
callBack = activity as? CallBack
viewModel.searchStateData.observe(viewLifecycleOwner, Observer { viewModel.searchStateData.observe(viewLifecycleOwner, Observer {
refresh_progress_bar.isAutoLoading = it refresh_progress_bar.isAutoLoading = it
}) })

Loading…
Cancel
Save