|
|
@ -12,8 +12,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.CoroutineScope |
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
import kotlinx.coroutines.asCoroutineDispatcher |
|
|
|
import kotlinx.coroutines.asCoroutineDispatcher |
|
|
|
|
|
|
|
import kotlinx.coroutines.isActive |
|
|
|
import java.util.concurrent.Executors |
|
|
|
import java.util.concurrent.Executors |
|
|
|
|
|
|
|
|
|
|
|
class SearchViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class SearchViewModel(application: Application) : BaseViewModel(application) { |
|
|
@ -53,19 +55,21 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
for (item in bookSourceList) { |
|
|
|
for (item in bookSourceList) { |
|
|
|
//task取消时自动取消 by (scope = this@execute) |
|
|
|
//task取消时自动取消 by (scope = this@execute) |
|
|
|
WebBook(item).searchBook( |
|
|
|
WebBook(item).searchBook( |
|
|
|
searchKey, |
|
|
|
searchKey, |
|
|
|
searchPage, |
|
|
|
searchPage, |
|
|
|
scope = this, |
|
|
|
scope = this, |
|
|
|
context = searchPool |
|
|
|
context = searchPool |
|
|
|
) |
|
|
|
) |
|
|
|
.timeout(30000L) |
|
|
|
.timeout(30000L) |
|
|
|
.onSuccess(IO) { |
|
|
|
.onSuccess(IO) { |
|
|
|
it?.let { list -> |
|
|
|
if (isActive) { |
|
|
|
if (context.getPrefBoolean(PreferKey.precisionSearch)) { |
|
|
|
it?.let { list -> |
|
|
|
precisionSearch(list) |
|
|
|
if (context.getPrefBoolean(PreferKey.precisionSearch)) { |
|
|
|
} else { |
|
|
|
precisionSearch(this, list) |
|
|
|
App.db.searchBookDao().insert(*list.toTypedArray()) |
|
|
|
} else { |
|
|
|
mergeItems(list) |
|
|
|
App.db.searchBookDao().insert(*list.toTypedArray()) |
|
|
|
|
|
|
|
mergeItems(this, list) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -86,7 +90,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 精确搜索处理 |
|
|
|
* 精确搜索处理 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun precisionSearch(searchBooks: List<SearchBook>) { |
|
|
|
private fun precisionSearch(scope: CoroutineScope, searchBooks: List<SearchBook>) { |
|
|
|
val books = arrayListOf<SearchBook>() |
|
|
|
val books = arrayListOf<SearchBook>() |
|
|
|
searchBooks.forEach { searchBook -> |
|
|
|
searchBooks.forEach { searchBook -> |
|
|
|
if (searchBook.name.contains(searchKey, true) |
|
|
|
if (searchBook.name.contains(searchKey, true) |
|
|
@ -94,14 +98,16 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
) books.add(searchBook) |
|
|
|
) books.add(searchBook) |
|
|
|
} |
|
|
|
} |
|
|
|
App.db.searchBookDao().insert(*books.toTypedArray()) |
|
|
|
App.db.searchBookDao().insert(*books.toTypedArray()) |
|
|
|
mergeItems(books) |
|
|
|
if (scope.isActive) { |
|
|
|
|
|
|
|
mergeItems(scope, books) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 合并搜索结果并排序 |
|
|
|
* 合并搜索结果并排序 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
private fun mergeItems(newDataS: List<SearchBook>) { |
|
|
|
private fun mergeItems(scope: CoroutineScope, 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>() |
|
|
@ -145,6 +151,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!scope.isActive) return |
|
|
|
searchBooks.sortWith(Comparator { o1, o2 -> |
|
|
|
searchBooks.sortWith(Comparator { o1, o2 -> |
|
|
|
if (o1.name == searchKey && o2.name != searchKey) { |
|
|
|
if (o1.name == searchKey && o2.name != searchKey) { |
|
|
|
1 |
|
|
|
1 |
|
|
@ -170,6 +177,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
0 |
|
|
|
0 |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
if (!scope.isActive) return |
|
|
|
searchBooks = copyDataS |
|
|
|
searchBooks = copyDataS |
|
|
|
searchBookLiveData.postValue(copyDataS) |
|
|
|
searchBookLiveData.postValue(copyDataS) |
|
|
|
} |
|
|
|
} |
|
|
|