mirror of
https://github.com/gedoor/legado.git
synced 2024-09-03 09:55:00 +08:00
优化
This commit is contained in:
parent
1445410c20
commit
737f22997d
@ -8,6 +8,7 @@ import android.view.View.VISIBLE
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import io.legado.app.App
|
||||
@ -15,6 +16,7 @@ import io.legado.app.R
|
||||
import io.legado.app.base.VMBaseActivity
|
||||
import io.legado.app.constant.PreferKey
|
||||
import io.legado.app.data.entities.Book
|
||||
import io.legado.app.data.entities.SearchBook
|
||||
import io.legado.app.data.entities.SearchKeyword
|
||||
import io.legado.app.lib.theme.ATH
|
||||
import io.legado.app.lib.theme.primaryTextColor
|
||||
@ -40,7 +42,7 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
|
||||
override val viewModel: SearchViewModel
|
||||
get() = getViewModel(SearchViewModel::class.java)
|
||||
|
||||
override lateinit var adapter: SearchAdapter
|
||||
lateinit var adapter: SearchAdapter
|
||||
private lateinit var bookAdapter: BookAdapter
|
||||
private lateinit var historyKeyAdapter: HistoryKeyAdapter
|
||||
private lateinit var loadMoreView: LoadMoreView
|
||||
@ -176,6 +178,9 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
|
||||
}
|
||||
upGroupMenu()
|
||||
})
|
||||
viewModel.searchBookLiveData.observe(this, Observer {
|
||||
setSearchItems(it)
|
||||
})
|
||||
}
|
||||
|
||||
private fun initIntent() {
|
||||
@ -251,9 +256,14 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
|
||||
})
|
||||
}
|
||||
|
||||
private fun setSearchItems(items: List<SearchBook>) {
|
||||
val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), items))
|
||||
adapter.setItems(items, false)
|
||||
diffResult.dispatchUpdatesTo(adapter)
|
||||
}
|
||||
|
||||
override fun startSearch() {
|
||||
refresh_progress_bar.isAutoLoading = true
|
||||
initData()
|
||||
fb_stop.visible()
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package io.legado.app.ui.book.search
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import io.legado.app.App
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.constant.PreferKey
|
||||
@ -12,7 +13,6 @@ import io.legado.app.model.WebBook
|
||||
import io.legado.app.utils.getPrefBoolean
|
||||
import io.legado.app.utils.getPrefString
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.Dispatchers.Main
|
||||
import kotlinx.coroutines.asCoroutineDispatcher
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.Executors
|
||||
@ -22,6 +22,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
|
||||
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher()
|
||||
private var task: Coroutine<*>? = null
|
||||
var callBack: CallBack? = null
|
||||
var searchBookLiveData = MutableLiveData<List<SearchBook>>()
|
||||
var searchKey: String = ""
|
||||
var searchPage = 1
|
||||
var isLoading = false
|
||||
@ -83,12 +84,12 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
|
||||
books.add(searchBook)
|
||||
}
|
||||
App.db.searchBookDao().insert(*books.toTypedArray())
|
||||
addToAdapter(books)
|
||||
mergeItems(books)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private suspend fun addToAdapter(newDataS: List<SearchBook>) {
|
||||
private fun mergeItems(newDataS: List<SearchBook>) {
|
||||
if (newDataS.isNotEmpty()) {
|
||||
val copyDataS = ArrayList(searchBooks)
|
||||
val searchBooksAdd = ArrayList<SearchBook>()
|
||||
@ -134,10 +135,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
}
|
||||
}
|
||||
withContext(Main) {
|
||||
searchBooks = copyDataS
|
||||
callBack?.adapter?.setItems(searchBooks)
|
||||
}
|
||||
searchBooks = copyDataS
|
||||
searchBookLiveData.postValue(copyDataS)
|
||||
}
|
||||
}
|
||||
|
||||
@ -173,7 +172,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
|
||||
interface CallBack {
|
||||
var adapter: SearchAdapter
|
||||
fun startSearch()
|
||||
fun searchFinally()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user