From 79c1ce8b7e0d7c008441aa4bdea57f471a08535c Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 5 Mar 2020 19:50:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/changecover/ChangeCoverDialog.kt | 20 ++++--- .../book/changecover/ChangeCoverViewModel.kt | 31 +++++------ .../app/ui/book/changecover/DiffCallBack.kt | 52 +++++++++++++++++++ .../changesource/ChangeSourceViewModel.kt | 3 +- 4 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/changecover/DiffCallBack.kt diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt index fdc64fa5f..62108ec53 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt @@ -5,17 +5,17 @@ import android.util.DisplayMetrics import android.view.LayoutInflater 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.DiffUtil import androidx.recyclerview.widget.GridLayoutManager import io.legado.app.R +import io.legado.app.base.BaseDialogFragment import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.dialog_change_source.* -class ChangeCoverDialog : DialogFragment(), - ChangeCoverViewModel.CallBack, +class ChangeCoverDialog : BaseDialogFragment(), CoverAdapter.CallBack { companion object { @@ -35,7 +35,7 @@ class ChangeCoverDialog : DialogFragment(), private var callBack: CallBack? = null private lateinit var viewModel: ChangeCoverViewModel - override lateinit var adapter: CoverAdapter + lateinit var adapter: CoverAdapter override fun onStart() { super.onStart() @@ -51,11 +51,10 @@ class ChangeCoverDialog : DialogFragment(), ): View? { callBack = activity as? CallBack viewModel = getViewModel(ChangeCoverViewModel::class.java) - viewModel.callBack = this return inflater.inflate(R.layout.dialog_change_cover, container) } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) viewModel.searchStateData.observe(viewLifecycleOwner, Observer { refresh_progress_bar.isAutoLoading = it @@ -70,6 +69,15 @@ class ChangeCoverDialog : DialogFragment(), viewModel.loadDbSearchBook() } + override fun observeLiveBus() { + super.observeLiveBus() + viewModel.searchBooksLiveData.observe(viewLifecycleOwner, Observer { + val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), it)) + adapter.setItems(it) + diffResult.dispatchUpdatesTo(adapter) + }) + } + override fun changeTo(coverUrl: String) { callBack?.coverChangeTo(coverUrl) dismiss() diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt index 0316c4fc9..ad1b1a8df 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt @@ -6,22 +6,23 @@ import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppPattern +import io.legado.app.data.entities.SearchBook 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.withContext import java.util.concurrent.Executors class ChangeCoverViewModel(application: Application) : BaseViewModel(application) { private var searchPool = Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() - var callBack: CallBack? = null var name: String = "" var author: String = "" private var task: Coroutine<*>? = null val searchStateData = MutableLiveData() + val searchBooksLiveData = MutableLiveData>() + private val searchBooks = ArrayList() fun initData(bundle: Bundle) { bundle.getString("name")?.let { @@ -34,13 +35,15 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application fun loadDbSearchBook() { execute { - App.db.searchBookDao().getEnableHasCover(name, author) - }.onSuccess { - it?.let { - callBack?.adapter?.setItems(it) + App.db.searchBookDao().getEnableHasCover(name, author).let { + searchBooks.addAll(it) + if (it.size <= 1) { + searchBooksLiveData.postValue(searchBooks) + search() + } else { + searchBooksLiveData.postValue(searchBooks) + } } - }.onFinally { - search() } } @@ -59,12 +62,9 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application && !searchBook.coverUrl.isNullOrEmpty() ) { App.db.searchBookDao().insert(searchBook) - callBack?.adapter?.let { adapter -> - if (!adapter.getItems().contains(searchBook)) { - withContext(Dispatchers.Main) { - adapter.addItem(searchBook) - } - } + if (!searchBooks.contains(searchBook)) { + searchBooks.add(searchBook) + searchBooksLiveData.postValue(searchBooks) } } } @@ -82,7 +82,4 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application searchPool.close() } - interface CallBack { - var adapter: CoverAdapter - } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/DiffCallBack.kt b/app/src/main/java/io/legado/app/ui/book/changecover/DiffCallBack.kt new file mode 100644 index 000000000..ffdac0f79 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/changecover/DiffCallBack.kt @@ -0,0 +1,52 @@ +package io.legado.app.ui.book.changecover + +import android.os.Bundle +import androidx.recyclerview.widget.DiffUtil +import io.legado.app.data.entities.SearchBook + +class DiffCallBack(private val oldItems: List, private val newItems: List) : + DiffUtil.Callback() { + + override fun getOldListSize(): Int { + return oldItems.size + } + + override fun getNewListSize(): Int { + return newItems.size + } + + override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + return oldItem.bookUrl == newItem.bookUrl + } + + override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + if (oldItem.originName != newItem.originName) { + return false + } + if (oldItem.coverUrl != newItem.coverUrl) { + return false + } + return true + } + + override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + val payload = Bundle() + if (oldItem.originName != newItem.originName) { + payload.putString("name", newItem.originName) + } + if (oldItem.coverUrl != newItem.coverUrl) { + payload.putString("coverUrl", newItem.coverUrl) + } + if (payload.isEmpty) { + return null + } + return payload + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index bc0a8943d..e0800c0a1 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -45,7 +45,8 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio execute { App.db.searchBookDao().getByNameAuthorEnable(name, author).let { searchBooks.addAll(it) - if (it.isEmpty()) { + if (it.size <= 1) { + upAdapter() search() } else { upAdapter()