fix: 换源界面

pull/95/head
kunfei 5 years ago
parent 01d00bbba9
commit 16ddb2f642
  1. 26
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt
  2. 22
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt

@ -11,6 +11,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
import io.legado.app.constant.PreferKey
@ -25,7 +26,6 @@ import kotlinx.android.synthetic.main.dialog_change_source.*
class ChangeSourceDialog : DialogFragment(),
Toolbar.OnMenuItemClickListener,
ChangeSourceViewModel.CallBack,
ChangeSourceAdapter.CallBack {
companion object {
@ -45,7 +45,7 @@ class ChangeSourceDialog : DialogFragment(),
private var callBack: CallBack? = null
private lateinit var viewModel: ChangeSourceViewModel
override lateinit var changeSourceAdapter: ChangeSourceAdapter
lateinit var adapter: ChangeSourceAdapter
override fun onStart() {
super.onStart()
@ -61,22 +61,19 @@ class ChangeSourceDialog : DialogFragment(),
): View? {
callBack = activity as? CallBack
viewModel = getViewModel(ChangeSourceViewModel::class.java)
viewModel.callBack = this
return inflater.inflate(R.layout.dialog_change_source, container)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.searchStateData.observe(viewLifecycleOwner, Observer {
refresh_progress_bar.isAutoLoading = it
})
viewModel.initData(arguments)
showTitle()
tool_bar.inflateMenu(R.menu.change_source)
tool_bar.setOnMenuItemClickListener(this)
showTitle()
initRecyclerView()
initMenu()
initSearchView()
initLiveData()
viewModel.loadDbSearchBook()
viewModel.search()
}
@ -92,10 +89,10 @@ class ChangeSourceDialog : DialogFragment(),
}
private fun initRecyclerView() {
changeSourceAdapter = ChangeSourceAdapter(requireContext(), this)
adapter = ChangeSourceAdapter(requireContext(), this)
recycler_view.layoutManager = LinearLayoutManager(context)
recycler_view.addItemDecoration(recycler_view.getVerticalDivider())
recycler_view.adapter = changeSourceAdapter
recycler_view.adapter = adapter
}
private fun initSearchView() {
@ -121,6 +118,17 @@ class ChangeSourceDialog : DialogFragment(),
})
}
private fun initLiveData() {
viewModel.searchStateData.observe(viewLifecycleOwner, Observer {
refresh_progress_bar.isAutoLoading = it
})
viewModel.searchBooksLiveData.observe(viewLifecycleOwner, Observer {
val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), it))
adapter.setItems(it)
diffResult.dispatchUpdatesTo(adapter)
})
}
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_load_toc -> {

@ -3,7 +3,6 @@ package io.legado.app.ui.changesource
import android.app.Application
import android.os.Bundle
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
@ -15,17 +14,15 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook
import io.legado.app.utils.getPrefBoolean
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.withContext
import org.jetbrains.anko.debug
import java.util.concurrent.Executors
class ChangeSourceViewModel(application: Application) : BaseViewModel(application) {
private var searchPool =
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher()
var callBack: CallBack? = null
val searchStateData = MutableLiveData<Boolean>()
val searchBooksLiveData = MutableLiveData<List<SearchBook>>()
var name: String = ""
var author: String = ""
private var task: Coroutine<*>? = null
@ -54,16 +51,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
private fun upAdapter() {
execute {
callBack?.changeSourceAdapter?.let {
val books = searchBooks.toList()
val diffResult = DiffUtil.calculateDiff(DiffCallBack(it.getItems(), books))
withContext(Main) {
synchronized(this) {
it.setItems(books, false)
diffResult.dispatchUpdatesTo(it)
}
}
}
val books = searchBooks.toList()
books.sortedBy { it.originOrder }
searchBooksLiveData.postValue(books)
}
}
@ -154,8 +144,4 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
searchPool.close()
}
interface CallBack {
var changeSourceAdapter: ChangeSourceAdapter
}
}
Loading…
Cancel
Save