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

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