pull/783/head
gedoor 4 years ago
parent a9fa608da4
commit f47e49f984
  1. 19
      app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt
  2. 25
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  3. 39
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt

@ -1,5 +1,6 @@
package io.legado.app.model.webBook
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource
import kotlinx.coroutines.CoroutineScope
@ -13,8 +14,22 @@ object PreciseSearch {
bookSources: List<BookSource>,
name: String,
author: String
) {
): Book? {
bookSources.forEach { bookSource ->
val webBook = WebBook(bookSource)
kotlin.runCatching {
webBook.searchBookAwait(scope, name).firstOrNull {
it.name == name && it.author == author
}?.let {
return if (it.tocUrl.isBlank()) {
webBook.getBookInfoAwait(scope, it.toBook())
} else {
it.toBook()
}
}
}
}
return null
}
}

@ -13,10 +13,12 @@ import io.legado.app.help.BookHelp
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.storage.BookWebDav
import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.webBook.PreciseSearch
import io.legado.app.model.webBook.WebBook
import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.help.ReadAloud
import io.legado.app.service.help.ReadBook
import io.legado.app.utils.msg
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.withContext
@ -213,23 +215,18 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun autoChangeSource(name: String, author: String) {
if (!AppConfig.autoChangeSource) return
execute {
App.db.bookSourceDao.allTextEnabled.forEach { source ->
try {
WebBook(source).searchBookAwait(this, name)
.getOrNull(0)?.let {
if (it.name == name && (it.author == author || author == "")) {
val book = it.toBook()
book.upInfoFromOld(ReadBook.book)
changeTo(book)
return@execute
}
}
} catch (e: Exception) {
//nothing
}
val sources = App.db.bookSourceDao.allTextEnabled
val book = PreciseSearch.searchFirstBook(this, sources, name, author)
if (book != null) {
book.upInfoFromOld(ReadBook.book)
changeTo(book)
} else {
throw Exception("自动换源失败")
}
}.onStart {
ReadBook.upMsg(context.getString(R.string.source_auto_changing))
}.onError {
toast(it.msg)
}.onFinally {
ReadBook.upMsg(null)
}

@ -7,6 +7,7 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup
import io.legado.app.data.entities.BookSource
import io.legado.app.model.webBook.PreciseSearch
import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO
@ -89,22 +90,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
}
}
text.isJsonArray() -> {
val bookSources = App.db.bookSourceDao.allEnabled
GSON.fromJsonArray<Map<String, String?>>(text)?.forEach {
val name = it["name"] ?: ""
val author = it["author"] ?: ""
bookSources.forEach { bookSource ->
runCatching {
val webBook = WebBook(bookSource)
val searchBooks = webBook.searchBookAwait(this, name)
val searchBook = searchBooks.firstOrNull()
if (searchBook != null && searchBook.name == name && searchBook.author == author) {
val book = webBook.getBookInfoAwait(this, searchBook.toBook())
App.db.bookDao.insert(book)
}
}
}
}
importBookshelfByJson(text, groupId)
}
else -> {
throw Exception("格式不对")
@ -115,6 +101,27 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
}
}
private fun importBookshelfByJson(json: String, groupId: Long) {
execute {
val bookSources = App.db.bookSourceDao.allEnabled
GSON.fromJsonArray<Map<String, String?>>(json)?.forEach {
val name = it["name"] ?: ""
val author = it["author"] ?: ""
if (name.isNotEmpty() && App.db.bookDao.getBook(name, author) == null) {
val book = PreciseSearch
.searchFirstBook(this, bookSources, name, author)
book?.let {
if (groupId > 0) {
book.group = groupId
}
}
}
}
}.onFinally {
toast(R.string.success)
}
}
fun checkGroup(groups: List<BookGroup>) {
groups.forEach { group ->
if (group.groupId >= 0 && group.groupId and (group.groupId - 1) != 0L) {

Loading…
Cancel
Save