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. 15
      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
private fun upAdapter() {
if (System.currentTimeMillis() > postTime + 500) {
postTime = System.currentTimeMillis()
handler.removeCallbacks(sendRunnable)
postTime = System.currentTimeMillis()
val books = searchBooks.toList()
searchBooksLiveData.postValue(books.sortedBy { it.originOrder })
} else {

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

@ -1,6 +1,7 @@
package io.legado.app.ui.book.search
import android.app.Application
import android.os.Handler
import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.base.BaseViewModel
@ -12,15 +13,18 @@ import io.legado.app.utils.getPrefBoolean
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.isActive
class SearchViewModel(application: Application) : BaseViewModel(application)
, SearchBookModel.CallBack {
class SearchViewModel(application: Application) : BaseViewModel(application),
SearchBookModel.CallBack {
val handler = Handler()
private val searchBookModel = SearchBookModel(this, this)
var isSearchLiveData = MutableLiveData<Boolean>()
var searchBookLiveData = MutableLiveData<List<SearchBook>>()
var searchKey: String = ""
var isLoading = false
var searchBooks = arrayListOf<SearchBook>()
private var searchBooks = arrayListOf<SearchBook>()
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)
}
@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() {
isSearchLiveData.postValue(true)
}
@ -151,7 +166,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application)
})
if (!scope.isActive) return
searchBooks = copyDataS
searchBookLiveData.postValue(copyDataS)
upAdapter()
}
}

Loading…
Cancel
Save