This commit is contained in:
gedoor 2021-10-15 22:59:40 +08:00
parent 76c9b08541
commit 744b5cdb12
4 changed files with 56 additions and 36 deletions

View File

@ -14,6 +14,7 @@
**2021/10/15**
* 再次修复朗读卡住问题
* 导入书单改为多线程
* 修复其它一些bug
**2021/10/14**

View File

@ -7,6 +7,7 @@ import io.legado.app.data.entities.SearchBook
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.http.StrResponse
import io.legado.app.model.Debug
import io.legado.app.model.NoStackTraceException
import io.legado.app.model.analyzeRule.AnalyzeUrl
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -15,32 +16,6 @@ import kotlin.coroutines.CoroutineContext
@Suppress("MemberVisibilityCanBePrivate")
object WebBook {
/**
* 精准搜索
*/
suspend fun preciseSearch(
scope: CoroutineScope,
bookSources: List<BookSource>,
name: String,
author: String
): Pair<BookSource, Book>? {
bookSources.forEach { source ->
kotlin.runCatching {
if (!scope.isActive) return null
searchBookAwait(scope, source, name).firstOrNull {
it.name == name && it.author == author
}?.let { searchBook ->
if (!scope.isActive) return null
var book = searchBook.toBook()
if (book.tocUrl.isBlank()) {
book = getBookInfoAwait(scope, source, book)
}
return Pair(source, book)
}
}
}
return null
}
/**
* 搜索
@ -326,4 +301,45 @@ object WebBook {
)
}
}
/**
* 精准搜索
*/
fun preciseSearch(
scope: CoroutineScope,
bookSources: List<BookSource>,
name: String,
author: String,
context: CoroutineContext = Dispatchers.IO,
): Coroutine<Pair<BookSource, Book>> {
return Coroutine.async(scope, context) {
preciseSearchAwait(scope, bookSources, name, author)
?: throw NoStackTraceException("没有搜索到<$name>$author")
}
}
suspend fun preciseSearchAwait(
scope: CoroutineScope,
bookSources: List<BookSource>,
name: String,
author: String
): Pair<BookSource, Book>? {
bookSources.forEach { source ->
kotlin.runCatching {
if (!scope.isActive) return null
searchBookAwait(scope, source, name).firstOrNull {
it.name == name && it.author == author
}?.let { searchBook ->
if (!scope.isActive) return null
var book = searchBook.toBook()
if (book.tocUrl.isBlank()) {
book = getBookInfoAwait(scope, source, book)
}
return Pair(source, book)
}
}
}
return null
}
}

View File

@ -197,7 +197,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (!AppConfig.autoChangeSource) return
execute {
val sources = appDb.bookSourceDao.allTextEnabled
WebBook.preciseSearch(this, sources, name, author)?.let {
WebBook.preciseSearchAwait(this, sources, name, author)?.let {
it.second.upInfoFromOld(ReadBook.book)
changeTo(it.first, it.second)
} ?: throw NoStackTraceException("自动换源失败")

View File

@ -108,18 +108,21 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
private fun importBookshelfByJson(json: String, groupId: Long) {
execute {
val bookSources = appDb.bookSourceDao.allEnabled
GSON.fromJsonArray<Map<String, String?>>(json)?.forEach {
GSON.fromJsonArray<Map<String, String?>>(json)?.forEach { bookInfo ->
if (!isActive) return@execute
val name = it["name"] ?: ""
val author = it["author"] ?: ""
val name = bookInfo["name"] ?: ""
val author = bookInfo["author"] ?: ""
if (name.isNotEmpty() && appDb.bookDao.getBook(name, author) == null) {
val book = WebBook.preciseSearch(this, bookSources, name, author)?.second
book?.let {
if (groupId > 0) {
book.group = groupId
WebBook.preciseSearch(this, bookSources, name, author)
.onSuccess {
val book = it.second
if (groupId > 0) {
book.group = groupId
}
book.save()
}.onError { e ->
context.toastOnUi(e.localizedMessage)
}
book.save()
}
}
}
}.onFinally {