pull/1771/head
kunfei 3 years ago
parent 8fc9394b63
commit bbb6548b5d
  1. 2
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 8
      app/src/main/java/io/legado/app/model/webBook/WebBook.kt
  3. 2
      app/src/main/java/io/legado/app/service/CheckSourceService.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt
  5. 11
      app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt
  6. 2
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  7. 2
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@ -124,7 +124,7 @@ object BookController {
if (book.tocUrl.isBlank()) { if (book.tocUrl.isBlank()) {
WebBook.getBookInfoAwait(this, bookSource, book) WebBook.getBookInfoAwait(this, bookSource, book)
} }
WebBook.getChapterListAwait(this, bookSource, book) WebBook.getChapterListAwait(this, bookSource, book).getOrThrow()
} }
appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.delByBook(book.bookUrl)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())

@ -188,7 +188,7 @@ object WebBook {
context: CoroutineContext = Dispatchers.IO context: CoroutineContext = Dispatchers.IO
): Coroutine<List<BookChapter>> { ): Coroutine<List<BookChapter>> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
getChapterListAwait(scope, bookSource, book) getChapterListAwait(scope, bookSource, book).getOrThrow()
} }
} }
@ -196,9 +196,10 @@ object WebBook {
scope: CoroutineScope, scope: CoroutineScope,
bookSource: BookSource, bookSource: BookSource,
book: Book, book: Book,
): List<BookChapter> { ): Result<List<BookChapter>> {
book.type = bookSource.bookSourceType book.type = bookSource.bookSourceType
return if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { return kotlin.runCatching {
if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) {
BookChapterList.analyzeChapterList( BookChapterList.analyzeChapterList(
scope = scope, scope = scope,
bookSource = bookSource, bookSource = bookSource,
@ -232,6 +233,7 @@ object WebBook {
) )
} }
} }
}
/** /**
* 章节内容 * 章节内容

@ -213,7 +213,7 @@ class CheckSourceService : BaseService() {
} }
//校验目录 //校验目录
if (CheckSource.checkCategory) { if (CheckSource.checkCategory) {
val toc = WebBook.getChapterListAwait(this, source, mBook) val toc = WebBook.getChapterListAwait(this, source, mBook).getOrThrow()
val nextChapterUrl = toc.getOrNull(1)?.url ?: toc.first().url val nextChapterUrl = toc.getOrNull(1)?.url ?: toc.first().url
//校验正文 //校验正文
if (CheckSource.checkContent) { if (CheckSource.checkContent) {

@ -195,7 +195,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
} }
private suspend fun loadBookToc(scope: CoroutineScope, source: BookSource, book: Book) { private suspend fun loadBookToc(scope: CoroutineScope, source: BookSource, book: Book) {
val chapters = WebBook.getChapterListAwait(scope, source, book) val chapters = WebBook.getChapterListAwait(scope, source, book).getOrThrow()
tocMap[book.bookUrl] = chapters tocMap[book.bookUrl] = chapters
book.latestChapterTitle = chapters.last().title book.latestChapterTitle = chapters.last().title
val searchBook: SearchBook = book.toSearchBook() val searchBook: SearchBook = book.toSearchBook()
@ -300,7 +300,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
WebBook.getBookInfoAwait(this, source, book) WebBook.getBookInfoAwait(this, source, book)
} }
val toc = WebBook.getChapterListAwait(this, source, book) val toc = WebBook.getChapterListAwait(this, source, book).getOrThrow()
Pair(toc, source) Pair(toc, source)
} }
} }

@ -8,6 +8,7 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.toastOnUi
class BookshelfManageViewModel(application: Application) : BaseViewModel(application) { class BookshelfManageViewModel(application: Application) : BaseViewModel(application) {
@ -47,13 +48,19 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica
if (book.isLocalBook()) return@forEachIndexed if (book.isLocalBook()) return@forEachIndexed
if (book.origin == source.bookSourceUrl) return@forEachIndexed if (book.origin == source.bookSourceUrl) return@forEachIndexed
WebBook.preciseSearchAwait(this, source, book.name, book.author) WebBook.preciseSearchAwait(this, source, book.name, book.author)
.getOrNull()?.let { newBook -> .onFailure {
val toc = WebBook.getChapterListAwait(this, source, newBook) context.toastOnUi("获取书籍出错\n${it.localizedMessage}")
}.getOrNull()?.let { newBook ->
WebBook.getChapterListAwait(this, source, newBook)
.onFailure {
context.toastOnUi("获取目录出错\n${it.localizedMessage}")
}.getOrNull()?.let { toc ->
book.changeTo(newBook, toc) book.changeTo(newBook, toc)
appDb.bookDao.insert(newBook) appDb.bookDao.insert(newBook)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())
} }
} }
}
}.onFinally { }.onFinally {
batchChangeSourceState.value = false batchChangeSourceState.value = false
} }

@ -231,7 +231,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
WebBook.getBookInfoAwait(this, source, book) WebBook.getBookInfoAwait(this, source, book)
} }
val toc = WebBook.getChapterListAwait(this, source, book) val toc = WebBook.getChapterListAwait(this, source, book).getOrThrow()
changeTo(source, book, toc) changeTo(source, book, toc)
return@execute return@execute
} }

@ -118,7 +118,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
if (book.tocUrl.isBlank()) { if (book.tocUrl.isBlank()) {
WebBook.getBookInfoAwait(this, source, book) WebBook.getBookInfoAwait(this, source, book)
} }
val toc = WebBook.getChapterListAwait(this, source, book) val toc = WebBook.getChapterListAwait(this, source, book).getOrThrow()
appDb.bookDao.update(book) appDb.bookDao.update(book)
appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.delByBook(book.bookUrl)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())

Loading…
Cancel
Save