pull/94/head
kunfei 5 years ago
parent f061f21487
commit 120dc86e39
  1. 8
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  2. 57
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -141,6 +141,14 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
adapter = SearchAdapter(this, this) adapter = SearchAdapter(this, this)
recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.adapter = adapter recycler_view.adapter = adapter
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)
if (positionStart == 0) {
recycler_view.scrollToPosition(0)
}
}
})
loadMoreView = LoadMoreView(this) loadMoreView = LoadMoreView(this)
recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

@ -11,9 +11,10 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch import kotlinx.coroutines.withContext
import java.util.concurrent.Executors import java.util.concurrent.Executors
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
@ -56,7 +57,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
context = searchPool context = searchPool
) )
.timeout(30000L) .timeout(30000L)
.onSuccess(Dispatchers.IO) { .onSuccess {
it?.let { list -> it?.let { list ->
searchSuccess(list) searchSuccess(list)
} }
@ -70,22 +71,24 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
} }
private fun searchSuccess(searchBooks: List<SearchBook>) { private suspend fun searchSuccess(searchBooks: List<SearchBook>) {
val books = arrayListOf<SearchBook>() withContext(IO) {
searchBooks.forEach { searchBook -> val books = arrayListOf<SearchBook>()
if (context.getPrefBoolean(PreferKey.precisionSearch)) { searchBooks.forEach { searchBook ->
if (searchBook.name.equals(searchKey, true) if (context.getPrefBoolean(PreferKey.precisionSearch)) {
|| searchBook.author.equals(searchKey, true) if (searchBook.name.equals(searchKey, true)
) books.add(searchBook) || searchBook.author.equals(searchKey, true)
} else ) books.add(searchBook)
books.add(searchBook) } else
books.add(searchBook)
}
App.db.searchBookDao().insert(*books.toTypedArray())
addToAdapter(books)
} }
App.db.searchBookDao().insert(*books.toTypedArray())
addToAdapter(books)
} }
@Synchronized @Synchronized
private fun addToAdapter(newDataS: List<SearchBook>) { private suspend fun addToAdapter(newDataS: List<SearchBook>) {
if (newDataS.isNotEmpty()) { if (newDataS.isNotEmpty()) {
val copyDataS = ArrayList(searchBooks) val copyDataS = ArrayList(searchBooks)
val searchBooksAdd = ArrayList<SearchBook>() val searchBooksAdd = ArrayList<SearchBook>()
@ -93,46 +96,46 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
copyDataS.addAll(newDataS) copyDataS.addAll(newDataS)
} else { } else {
//存在 //存在
for (temp in newDataS) { newDataS.forEach { item ->
var hasSame = false var hasSame = false
for (i in copyDataS.indices) { for (i in copyDataS.indices) {
val searchBook = copyDataS[i] val searchBook = copyDataS[i]
if (temp.name == searchBook.name if (item.name == searchBook.name
&& temp.author == searchBook.author && item.author == searchBook.author
) { ) {
hasSame = true hasSame = true
searchBook.addOrigin(temp.bookUrl) searchBook.addOrigin(item.bookUrl)
break break
} }
} }
if (!hasSame) { if (!hasSame) {
searchBooksAdd.add(temp) searchBooksAdd.add(item)
} }
} }
//添加 //添加
for (temp in searchBooksAdd) { searchBooksAdd.forEach { item ->
if (searchKey == temp.name) { if (searchKey == item.name) {
for (i in copyDataS.indices) { for (i in copyDataS.indices) {
val searchBook = copyDataS[i] val searchBook = copyDataS[i]
if (searchKey != searchBook.name) { if (searchKey != searchBook.name) {
copyDataS.add(i, temp) copyDataS.add(i, item)
break break
} }
} }
} else if (searchKey == temp.author) { } else if (searchKey == item.author) {
for (i in copyDataS.indices) { for (i in copyDataS.indices) {
val searchBook = copyDataS[i] val searchBook = copyDataS[i]
if (searchKey != searchBook.name && searchKey == searchBook.author) { if (searchKey != searchBook.name && searchKey == searchBook.author) {
copyDataS.add(i, temp) copyDataS.add(i, item)
break break
} }
} }
} else { } else {
copyDataS.add(temp) copyDataS.add(item)
} }
} }
} }
launch { withContext(Main) {
searchBooks = copyDataS searchBooks = copyDataS
callBack?.adapter?.setItems(searchBooks) callBack?.adapter?.setItems(searchBooks)
} }

Loading…
Cancel
Save