feat: 优化代码

pull/95/head
kunfei 5 years ago
parent 18ef671211
commit 853a43272d
  1. 16
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  2. 10
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -34,7 +34,6 @@ import org.jetbrains.anko.startActivity
class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_search),
SearchViewModel.CallBack,
BookAdapter.CallBack,
HistoryKeyAdapter.CallBack,
SearchAdapter.CallBack {
@ -53,7 +52,6 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
private var groups = hashSetOf<String>()
override fun onActivityCreated(savedInstanceState: Bundle?) {
viewModel.callBack = this
initRecyclerView()
initSearchView()
initOtherView()
@ -181,6 +179,13 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
viewModel.searchBookLiveData.observe(this, Observer {
setSearchItems(it)
})
viewModel.isSearchLiveData.observe(this, Observer {
if (it) {
startSearch()
} else {
searchFinally()
}
})
}
private fun initIntent() {
@ -256,18 +261,19 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
})
}
@Synchronized
private fun setSearchItems(items: List<SearchBook>) {
val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), items))
val diffResult = DiffUtil.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), items))
adapter.setItems(items, false)
diffResult.dispatchUpdatesTo(adapter)
}
override fun startSearch() {
fun startSearch() {
refresh_progress_bar.isAutoLoading = true
fb_stop.visible()
}
override fun searchFinally() {
fun searchFinally() {
refresh_progress_bar.isAutoLoading = false
loadMoreView.startLoad()
fb_stop.invisible()

@ -21,7 +21,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
private var searchPool =
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher()
private var task: Coroutine<*>? = null
var callBack: CallBack? = null
var isSearchLiveData = MutableLiveData<Boolean>()
var searchBookLiveData = MutableLiveData<List<SearchBook>>()
var searchKey: String = ""
var searchPage = 1
@ -41,7 +41,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
searchKey = key
searchBooks.clear()
}
callBack?.startSearch()
isSearchLiveData.postValue(true)
task = execute {
val searchGroup = context.getPrefString("searchGroup") ?: ""
val bookSourceList = if (searchGroup.isBlank()) {
@ -67,7 +67,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
}
task?.invokeOnCompletion {
callBack?.searchFinally()
isSearchLiveData.postValue(false)
isLoading = false
}
}
@ -169,8 +169,4 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
searchPool.close()
}
interface CallBack {
fun startSearch()
fun searchFinally()
}
}

Loading…
Cancel
Save