From dfaad579eed05136c7fa900e0b843fccfe7407dc Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 19 Aug 2019 11:18:44 +0800 Subject: [PATCH] up --- .../app/ui/changesource/ChangeSourceDialog.kt | 10 +++++----- .../ui/changesource/ChangeSourceViewModel.kt | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) 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 775b1e33b..b5d0e565a 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 @@ -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) diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt index 58d687f3f..de6f4681a 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt @@ -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>() private val searchBooks = linkedSetOf() 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 + } } \ No newline at end of file