pull/32/head
kunfei 5 years ago
parent dacc76e2eb
commit dfaad579ee
  1. 10
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt
  2. 18
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt

@ -8,7 +8,6 @@ import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
@ -18,7 +17,7 @@ import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.dialog_change_source.*
class ChangeSourceDialog : DialogFragment() {
class ChangeSourceDialog : DialogFragment(), ChangeSourceViewModel.CallBack {
companion object {
const val tag = "changeSourceDialog"
@ -91,9 +90,7 @@ class ChangeSourceDialog : DialogFragment() {
recycler_view.layoutManager = LinearLayoutManager(context)
recycler_view.addItemDecoration(DividerItemDecoration(requireContext(), LinearLayout.VERTICAL))
recycler_view.adapter = changeSourceAdapter
viewModel.searchBookData.observe(viewLifecycleOwner, Observer {
changeSourceAdapter.addItems(it)
})
viewModel.callBack = this
}
private fun initSearchView() {
@ -119,6 +116,9 @@ class ChangeSourceDialog : DialogFragment() {
})
}
override fun adapter(): ChangeSourceAdapter {
return changeSourceAdapter
}
interface CallBack {
fun changeTo(book: Book)

@ -1,7 +1,7 @@
package io.legado.app.ui.changesource
import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseViewModel
@ -12,24 +12,27 @@ import kotlinx.coroutines.Dispatchers
import org.jetbrains.anko.debug
class ChangeSourceViewModel(application: Application) : BaseViewModel(application) {
var callBack: CallBack? = null
var curBookUrl = ""
var name: String = ""
var author: String = ""
val searchBookData = MutableLiveData<List<SearchBook>>()
private val searchBooks = linkedSetOf<SearchBook>()
fun initData() {
execute {
App.db.searchBookDao().getByNameAuthorEnable(name, author).let {
searchBooks.addAll(it)
searchBookData.postValue(it)
upAdapter()
search()
}
}
}
fun upAdapter() {
val diffResult = DiffUtil.calculateDiff(DiffCallBack(arrayListOf(), searchBooks.toList()))
callBack?.adapter()?.let {
diffResult.dispatchUpdatesTo(it)
}
}
fun search() {
@ -75,6 +78,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
book.latestChapterTitle = chapter.title
val searchBook = book.toSearchBook()
searchBooks.add(searchBook)
upAdapter()
App.db.searchBookDao().insert(searchBook)
}
}.onError {
@ -85,9 +89,13 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
fun screen(key: String?) {
if (key.isNullOrEmpty()) {
searchBookData.postValue(searchBooks.toList())
} else {
}
}
interface CallBack {
fun adapter(): ChangeSourceAdapter
}
}
Loading…
Cancel
Save