feat: 优化

pull/241/head
gedoor 5 years ago
parent eb163896ce
commit 59335a00c8
  1. 2
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt
  2. 9
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  3. 23
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -63,8 +63,8 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
@Synchronized @Synchronized
private fun upAdapter() { private fun upAdapter() {
if (System.currentTimeMillis() > postTime + 500) { if (System.currentTimeMillis() > postTime + 500) {
postTime = System.currentTimeMillis()
handler.removeCallbacks(sendRunnable) handler.removeCallbacks(sendRunnable)
postTime = System.currentTimeMillis()
val books = searchBooks.toList() val books = searchBooks.toList()
searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) searchBooksLiveData.postValue(books.sortedBy { it.originOrder })
} else { } else {

@ -52,7 +52,6 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
private var menu: Menu? = null private var menu: Menu? = null
private var precisionSearchMenuItem: MenuItem? = null private var precisionSearchMenuItem: MenuItem? = null
private var groups = linkedSetOf<String>() private var groups = linkedSetOf<String>()
private var refreshTime = System.currentTimeMillis()
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView() initRecyclerView()
@ -181,14 +180,13 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
upGroupMenu() upGroupMenu()
}) })
viewModel.searchBookLiveData.observe(this, Observer { viewModel.searchBookLiveData.observe(this, Observer {
upSearchItems(it, false) upSearchItems(it)
}) })
viewModel.isSearchLiveData.observe(this, Observer { viewModel.isSearchLiveData.observe(this, Observer {
if (it) { if (it) {
startSearch() startSearch()
} else { } else {
searchFinally() searchFinally()
upSearchItems(viewModel.searchBooks, true)
} }
}) })
} }
@ -284,15 +282,12 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
* 更新搜索结果 * 更新搜索结果
*/ */
@Synchronized @Synchronized
private fun upSearchItems(items: List<SearchBook>, isMandatoryUpdate: Boolean) { private fun upSearchItems(items: List<SearchBook>) {
val searchItems = ArrayList(items) val searchItems = ArrayList(items)
if (isMandatoryUpdate || System.currentTimeMillis() - refreshTime > 500) {
refreshTime = System.currentTimeMillis()
val diffResult = val diffResult =
DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), searchItems)) DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), searchItems))
adapter.setItems(searchItems, diffResult) adapter.setItems(searchItems, diffResult)
} }
}
/** /**
* 开始搜索 * 开始搜索

@ -1,6 +1,7 @@
package io.legado.app.ui.book.search package io.legado.app.ui.book.search
import android.app.Application import android.app.Application
import android.os.Handler
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
@ -12,15 +13,18 @@ import io.legado.app.utils.getPrefBoolean
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
class SearchViewModel(application: Application) : BaseViewModel(application) class SearchViewModel(application: Application) : BaseViewModel(application),
, SearchBookModel.CallBack { SearchBookModel.CallBack {
val handler = Handler()
private val searchBookModel = SearchBookModel(this, this) private val searchBookModel = SearchBookModel(this, this)
var isSearchLiveData = MutableLiveData<Boolean>() var isSearchLiveData = MutableLiveData<Boolean>()
var searchBookLiveData = MutableLiveData<List<SearchBook>>() var searchBookLiveData = MutableLiveData<List<SearchBook>>()
var searchKey: String = "" var searchKey: String = ""
var isLoading = false var isLoading = false
var searchBooks = arrayListOf<SearchBook>() private var searchBooks = arrayListOf<SearchBook>()
private var searchID = 0L private var searchID = 0L
private var postTime = 0L
private val sendRunnable = Runnable { upAdapter() }
/** /**
* 开始搜索 * 开始搜索
@ -36,6 +40,17 @@ class SearchViewModel(application: Application) : BaseViewModel(application)
searchBookModel.search(searchID, searchKey) searchBookModel.search(searchID, searchKey)
} }
@Synchronized
private fun upAdapter() {
if (System.currentTimeMillis() > postTime + 500) {
handler.removeCallbacks(sendRunnable)
postTime = System.currentTimeMillis()
searchBookLiveData.postValue(searchBooks)
} else {
handler.postDelayed(sendRunnable, 500)
}
}
override fun onSearchStart() { override fun onSearchStart() {
isSearchLiveData.postValue(true) isSearchLiveData.postValue(true)
} }
@ -151,7 +166,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application)
}) })
if (!scope.isActive) return if (!scope.isActive) return
searchBooks = copyDataS searchBooks = copyDataS
searchBookLiveData.postValue(copyDataS) upAdapter()
} }
} }

Loading…
Cancel
Save