feat: 优化代码

pull/132/head
kunfei 5 years ago
parent 14aa7b6d33
commit 79c1ce8b7e
  1. 20
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt
  2. 31
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt
  3. 52
      app/src/main/java/io/legado/app/ui/book/changecover/DiffCallBack.kt
  4. 3
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.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()

@ -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<Boolean>()
val searchBooksLiveData = MutableLiveData<List<SearchBook>>()
private val searchBooks = ArrayList<SearchBook>()
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
}
}

@ -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<SearchBook>, private val newItems: List<SearchBook>) :
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
}
}

@ -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()

Loading…
Cancel
Save