pull/783/head
gedoor 4 years ago
parent 0ff8e5ae9a
commit a9fa608da4
  1. 2
      app/src/main/java/io/legado/app/api/controller/BookshelfController.kt
  2. 40
      app/src/main/java/io/legado/app/model/Debug.kt
  3. 20
      app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt
  4. 2
      app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt
  5. 26
      app/src/main/java/io/legado/app/model/webBook/WebBook.kt
  6. 2
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  7. 8
      app/src/main/java/io/legado/app/service/CacheBookService.kt
  8. 4
      app/src/main/java/io/legado/app/service/help/CacheBook.kt
  9. 2
      app/src/main/java/io/legado/app/service/help/CheckSource.kt
  10. 2
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  11. 4
      app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt
  12. 2
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt
  13. 6
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt
  14. 2
      app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt
  15. 4
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  16. 7
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  17. 11
      app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt
  18. 4
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  19. 38
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt
  20. 2
      app/src/main/java/io/legado/app/web/utils/SourceDebugWebSocket.kt

@ -66,7 +66,7 @@ object BookshelfController {
} else { } else {
App.db.bookSourceDao.getBookSource(book.origin)?.let { source -> App.db.bookSourceDao.getBookSource(book.origin)?.let { source ->
runBlocking { runBlocking {
WebBook(source).getContentAwait(book, chapter) WebBook(source).getContentAwait(this, book, chapter)
}.let { }.let {
saveBookReadIndex(book, index) saveBookReadIndex(book, index)
returnData.setData(it) returnData.setData(it)

@ -11,6 +11,7 @@ import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.htmlFormat import io.legado.app.utils.htmlFormat
import io.legado.app.utils.isAbsUrl import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.msg import io.legado.app.utils.msg
import kotlinx.coroutines.CoroutineScope
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@ -101,7 +102,7 @@ object Debug {
} }
} }
fun startDebug(webBook: WebBook, key: String) { fun startDebug(scope: CoroutineScope, webBook: WebBook, key: String) {
cancelDebug() cancelDebug()
debugSource = webBook.sourceUrl debugSource = webBook.sourceUrl
startTime = System.currentTimeMillis() startTime = System.currentTimeMillis()
@ -111,12 +112,12 @@ object Debug {
book.origin = webBook.sourceUrl book.origin = webBook.sourceUrl
book.bookUrl = key book.bookUrl = key
log(webBook.sourceUrl, "⇒开始访问详情页:$key") log(webBook.sourceUrl, "⇒开始访问详情页:$key")
infoDebug(webBook, book) infoDebug(scope, webBook, book)
} }
key.contains("::") -> { key.contains("::") -> {
val url = key.substring(key.indexOf("::") + 2) val url = key.substring(key.indexOf("::") + 2)
log(webBook.sourceUrl, "⇒开始访问发现页:$url") log(webBook.sourceUrl, "⇒开始访问发现页:$url")
exploreDebug(webBook, url) exploreDebug(scope, webBook, url)
} }
key.startsWith("++")-> { key.startsWith("++")-> {
val url = key.substring(2) val url = key.substring(2)
@ -124,7 +125,7 @@ object Debug {
book.origin = webBook.sourceUrl book.origin = webBook.sourceUrl
book.tocUrl = url book.tocUrl = url
log(webBook.sourceUrl, "⇒开始访目录页:$url") log(webBook.sourceUrl, "⇒开始访目录页:$url")
tocDebug(webBook, book) tocDebug(scope, webBook, book)
} }
key.startsWith("--")-> { key.startsWith("--")-> {
val url = key.substring(2) val url = key.substring(2)
@ -134,23 +135,23 @@ object Debug {
val chapter = BookChapter() val chapter = BookChapter()
chapter.title = "调试" chapter.title = "调试"
chapter.url = url chapter.url = url
contentDebug(webBook, book, chapter, null) contentDebug(scope, webBook, book, chapter, null)
} }
else -> { else -> {
log(webBook.sourceUrl, "⇒开始搜索关键字:$key") log(webBook.sourceUrl, "⇒开始搜索关键字:$key")
searchDebug(webBook, key) searchDebug(scope, webBook, key)
} }
} }
} }
private fun exploreDebug(webBook: WebBook, url: String) { private fun exploreDebug(scope: CoroutineScope, webBook: WebBook, url: String) {
log(debugSource, "︾开始解析发现页") log(debugSource, "︾开始解析发现页")
val explore = webBook.exploreBook(url, 1) val explore = webBook.exploreBook(scope, url, 1)
.onSuccess { exploreBooks -> .onSuccess { exploreBooks ->
if (exploreBooks.isNotEmpty()) { if (exploreBooks.isNotEmpty()) {
log(debugSource, "︽发现页解析完成") log(debugSource, "︽发现页解析完成")
log(debugSource, showTime = false) log(debugSource, showTime = false)
infoDebug(webBook, exploreBooks[0].toBook()) infoDebug(scope, webBook, exploreBooks[0].toBook())
} else { } else {
log(debugSource, "︽未获取到书籍", state = -1) log(debugSource, "︽未获取到书籍", state = -1)
} }
@ -161,14 +162,14 @@ object Debug {
tasks.add(explore) tasks.add(explore)
} }
private fun searchDebug(webBook: WebBook, key: String) { private fun searchDebug(scope: CoroutineScope, webBook: WebBook, key: String) {
log(debugSource, "︾开始解析搜索页") log(debugSource, "︾开始解析搜索页")
val search = webBook.searchBook(key, 1) val search = webBook.searchBook(scope, key, 1)
.onSuccess { searchBooks -> .onSuccess { searchBooks ->
if (searchBooks.isNotEmpty()) { if (searchBooks.isNotEmpty()) {
log(debugSource, "︽搜索页解析完成") log(debugSource, "︽搜索页解析完成")
log(debugSource, showTime = false) log(debugSource, showTime = false)
infoDebug(webBook, searchBooks[0].toBook()) infoDebug(scope, webBook, searchBooks[0].toBook())
} else { } else {
log(debugSource, "︽未获取到书籍", state = -1) log(debugSource, "︽未获取到书籍", state = -1)
} }
@ -179,13 +180,13 @@ object Debug {
tasks.add(search) tasks.add(search)
} }
private fun infoDebug(webBook: WebBook, book: Book) { private fun infoDebug(scope: CoroutineScope, webBook: WebBook, book: Book) {
log(debugSource, "︾开始解析详情页") log(debugSource, "︾开始解析详情页")
val info = webBook.getBookInfo(book) val info = webBook.getBookInfo(scope, book)
.onSuccess { .onSuccess {
log(debugSource, "︽详情页解析完成") log(debugSource, "︽详情页解析完成")
log(debugSource, showTime = false) log(debugSource, showTime = false)
tocDebug(webBook, book) tocDebug(scope, webBook, book)
} }
.onError { .onError {
log(debugSource, it.msg, state = -1) log(debugSource, it.msg, state = -1)
@ -193,15 +194,15 @@ object Debug {
tasks.add(info) tasks.add(info)
} }
private fun tocDebug(webBook: WebBook, book: Book) { private fun tocDebug(scope: CoroutineScope, webBook: WebBook, book: Book) {
log(debugSource, "︾开始解析目录页") log(debugSource, "︾开始解析目录页")
val chapterList = webBook.getChapterList(book) val chapterList = webBook.getChapterList(scope, book)
.onSuccess { .onSuccess {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
log(debugSource, "︽目录页解析完成") log(debugSource, "︽目录页解析完成")
log(debugSource, showTime = false) log(debugSource, showTime = false)
val nextChapterUrl = if (it.size > 1) it[1].url else null val nextChapterUrl = if (it.size > 1) it[1].url else null
contentDebug(webBook, book, it[0], nextChapterUrl) contentDebug(scope, webBook, book, it[0], nextChapterUrl)
} else { } else {
log(debugSource, "︽目录列表为空", state = -1) log(debugSource, "︽目录列表为空", state = -1)
} }
@ -213,13 +214,14 @@ object Debug {
} }
private fun contentDebug( private fun contentDebug(
scope: CoroutineScope,
webBook: WebBook, webBook: WebBook,
book: Book, book: Book,
bookChapter: BookChapter, bookChapter: BookChapter,
nextChapterUrl: String? nextChapterUrl: String?
) { ) {
log(debugSource, "︾开始解析正文页") log(debugSource, "︾开始解析正文页")
val content = webBook.getContent(book, bookChapter, nextChapterUrl) val content = webBook.getContent(scope, book, bookChapter, nextChapterUrl)
.onSuccess { .onSuccess {
log(debugSource, "︽正文页解析完成", state = 1000) log(debugSource, "︽正文页解析完成", state = 1000)
} }

@ -0,0 +1,20 @@
package io.legado.app.model.webBook
import io.legado.app.data.entities.BookSource
import kotlinx.coroutines.CoroutineScope
/**
* 精准搜索
*/
object PreciseSearch {
suspend fun searchFirstBook(
scope: CoroutineScope,
bookSources: List<BookSource>,
name: String,
author: String
) {
}
}

@ -68,9 +68,9 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C
searchIndex++ searchIndex++
val source = bookSourceList[searchIndex] val source = bookSourceList[searchIndex]
val task = WebBook(source).searchBook( val task = WebBook(source).searchBook(
scope,
searchKey, searchKey,
searchPage, searchPage,
scope = scope,
context = searchPool!! context = searchPool!!
).timeout(30000L) ).timeout(30000L)
.onSuccess(IO) { .onSuccess(IO) {

@ -21,9 +21,9 @@ class WebBook(val bookSource: BookSource) {
* 搜索 * 搜索
*/ */
fun searchBook( fun searchBook(
scope: CoroutineScope,
key: String, key: String,
page: Int? = 1, page: Int? = 1,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO, context: CoroutineContext = Dispatchers.IO,
): Coroutine<ArrayList<SearchBook>> { ): Coroutine<ArrayList<SearchBook>> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
@ -64,20 +64,20 @@ class WebBook(val bookSource: BookSource) {
* 发现 * 发现
*/ */
fun exploreBook( fun exploreBook(
scope: CoroutineScope,
url: String, url: String,
page: Int? = 1, page: Int? = 1,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO, context: CoroutineContext = Dispatchers.IO,
): Coroutine<List<SearchBook>> { ): Coroutine<List<SearchBook>> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
exploreBookAwait(url, page, scope) exploreBookAwait(scope, url, page)
} }
} }
suspend fun exploreBookAwait( suspend fun exploreBookAwait(
scope: CoroutineScope = Coroutine.DEFAULT,
url: String, url: String,
page: Int? = 1, page: Int? = 1,
scope: CoroutineScope = Coroutine.DEFAULT
): ArrayList<SearchBook> { ): ArrayList<SearchBook> {
val variableBook = SearchBook() val variableBook = SearchBook()
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
@ -103,19 +103,19 @@ class WebBook(val bookSource: BookSource) {
* 书籍信息 * 书籍信息
*/ */
fun getBookInfo( fun getBookInfo(
scope: CoroutineScope,
book: Book, book: Book,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO, context: CoroutineContext = Dispatchers.IO,
canReName: Boolean = true, canReName: Boolean = true,
): Coroutine<Book> { ): Coroutine<Book> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
getBookInfoAwait(book, scope, canReName) getBookInfoAwait(scope, book, canReName)
} }
} }
suspend fun getBookInfoAwait( suspend fun getBookInfoAwait(
book: Book,
scope: CoroutineScope = Coroutine.DEFAULT, scope: CoroutineScope = Coroutine.DEFAULT,
book: Book,
canReName: Boolean = true, canReName: Boolean = true,
): Book { ): Book {
book.type = bookSource.bookSourceType book.type = bookSource.bookSourceType
@ -145,18 +145,18 @@ class WebBook(val bookSource: BookSource) {
* 目录 * 目录
*/ */
fun getChapterList( fun getChapterList(
scope: CoroutineScope,
book: Book, book: Book,
scope: CoroutineScope = Coroutine.DEFAULT,
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(book, scope) getChapterListAwait(scope, book)
} }
} }
suspend fun getChapterListAwait( suspend fun getChapterListAwait(
scope: CoroutineScope = Coroutine.DEFAULT,
book: Book, book: Book,
scope: CoroutineScope = Coroutine.DEFAULT
): List<BookChapter> { ): List<BookChapter> {
book.type = bookSource.bookSourceType book.type = bookSource.bookSourceType
return if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { return if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) {
@ -176,22 +176,22 @@ class WebBook(val bookSource: BookSource) {
* 章节内容 * 章节内容
*/ */
fun getContent( fun getContent(
scope: CoroutineScope,
book: Book, book: Book,
bookChapter: BookChapter, bookChapter: BookChapter,
nextChapterUrl: String? = null, nextChapterUrl: String? = null,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO context: CoroutineContext = Dispatchers.IO
): Coroutine<String> { ): Coroutine<String> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
getContentAwait(book, bookChapter, nextChapterUrl, scope) getContentAwait(scope, book, bookChapter, nextChapterUrl)
} }
} }
suspend fun getContentAwait( suspend fun getContentAwait(
scope: CoroutineScope,
book: Book, book: Book,
bookChapter: BookChapter, bookChapter: BookChapter,
nextChapterUrl: String? = null, nextChapterUrl: String? = null,
scope: CoroutineScope = Coroutine.DEFAULT
): String { ): String {
if (bookSource.getContentRule().content.isNullOrEmpty()) { if (bookSource.getContentRule().content.isNullOrEmpty()) {
Debug.log(sourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}") Debug.log(sourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}")

@ -270,7 +270,7 @@ class AudioPlayService : BaseService(),
val book = AudioPlay.book val book = AudioPlay.book
val webBook = AudioPlay.webBook val webBook = AudioPlay.webBook
if (book != null && webBook != null) { if (book != null && webBook != null) {
webBook.getContent(book, chapter, scope = this@AudioPlayService) webBook.getContent(this@AudioPlayService, book, chapter)
.onSuccess { content -> .onSuccess { content ->
if (content.isEmpty()) { if (content.isEmpty()) {
withContext(Main) { withContext(Main) {

@ -184,12 +184,8 @@ class CacheBookService : BaseService() {
return@async return@async
} }
if (!BookHelp.hasContent(book, bookChapter)) { if (!BookHelp.hasContent(book, bookChapter)) {
webBook.getContent( webBook.getContent(this, book, bookChapter, context = searchPool)
book, .timeout(60000L)
bookChapter,
scope = this,
context = searchPool
).timeout(60000L)
.onError { .onError {
synchronized(this) { synchronized(this) {
downloadingList.remove(bookChapter.url) downloadingList.remove(bookChapter.url)

@ -10,6 +10,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.CacheBookService import io.legado.app.service.CacheBookService
import io.legado.app.utils.msg import io.legado.app.utils.msg
import kotlinx.coroutines.CoroutineScope
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
@ -61,6 +62,7 @@ object CacheBook {
} }
fun download( fun download(
scope: CoroutineScope,
webBook: WebBook, webBook: WebBook,
book: Book, book: Book,
chapter: BookChapter, chapter: BookChapter,
@ -73,7 +75,7 @@ object CacheBook {
downloadMap[book.bookUrl] = CopyOnWriteArraySet() downloadMap[book.bookUrl] = CopyOnWriteArraySet()
} }
downloadMap[book.bookUrl]?.add(chapter.index) downloadMap[book.bookUrl]?.add(chapter.index)
webBook.getContent(book, chapter) webBook.getContent(scope, book, chapter)
.onSuccess { content -> .onSuccess { content ->
if (ReadBook.book?.bookUrl == book.bookUrl) { if (ReadBook.book?.bookUrl == book.bookUrl) {
ReadBook.contentLoadFinish( ReadBook.contentLoadFinish(

@ -50,7 +50,7 @@ class CheckSource(val source: BookSource) {
): Coroutine<*> { ): Coroutine<*> {
val webBook = WebBook(source) val webBook = WebBook(source)
return webBook return webBook
.searchBook(keyword, scope = scope, context = context) .searchBook(scope, keyword, context = context)
.timeout(60000L) .timeout(60000L)
.onError(Dispatchers.IO) { .onError(Dispatchers.IO) {
source.addGroup("失效") source.addGroup("失效")

@ -282,7 +282,7 @@ object ReadBook {
val book = book val book = book
val webBook = webBook val webBook = webBook
if (book != null && webBook != null) { if (book != null && webBook != null) {
CacheBook.download(webBook, book, chapter) CacheBook.download(Coroutine.DEFAULT, webBook, book, chapter)
} else if (book != null) { } else if (book != null) {
contentLoadFinish( contentLoadFinish(
book, chapter, "没有书源", resetPageOffset = resetPageOffset book, chapter, "没有书源", resetPageOffset = resetPageOffset

@ -53,7 +53,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
execute { execute {
AudioPlay.webBook?.getBookInfo(book, this) AudioPlay.webBook?.getBookInfo(this, book)
?.onSuccess { ?.onSuccess {
loadChapterList(book, changeDruChapterIndex) loadChapterList(book, changeDruChapterIndex)
} }
@ -65,7 +65,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
execute { execute {
AudioPlay.webBook?.getChapterList(book, this) AudioPlay.webBook?.getChapterList(this, book)
?.onSuccess(Dispatchers.IO) { cList -> ?.onSuccess(Dispatchers.IO) { cList ->
if (cList.isNotEmpty()) { if (cList.isNotEmpty()) {
if (changeDruChapterIndex == null) { if (changeDruChapterIndex == null) {

@ -98,7 +98,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
searchIndex++ searchIndex++
val source = bookSourceList[searchIndex] val source = bookSourceList[searchIndex]
val task = WebBook(source) val task = WebBook(source)
.searchBook(name, scope = this, context = searchPool!!) .searchBook(this, name, context = searchPool!!)
.timeout(60000L) .timeout(60000L)
.onSuccess(Dispatchers.IO) { .onSuccess(Dispatchers.IO) {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {

@ -123,7 +123,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
val source = bookSourceList[searchIndex] val source = bookSourceList[searchIndex]
val webBook = WebBook(source) val webBook = WebBook(source)
val task = webBook val task = webBook
.searchBook(name, scope = this, context = searchPool!!) .searchBook(this, name, context = searchPool!!)
.timeout(60000L) .timeout(60000L)
.onSuccess(IO) { .onSuccess(IO) {
it.forEach { searchBook -> it.forEach { searchBook ->
@ -159,7 +159,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
} }
private fun loadBookInfo(webBook: WebBook, book: Book) { private fun loadBookInfo(webBook: WebBook, book: Book) {
webBook.getBookInfo(book, this) webBook.getBookInfo(this, book)
.onSuccess { .onSuccess {
if (context.getPrefBoolean(PreferKey.changeSourceLoadToc)) { if (context.getPrefBoolean(PreferKey.changeSourceLoadToc)) {
loadBookToc(webBook, book) loadBookToc(webBook, book)
@ -175,7 +175,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
} }
private fun loadBookToc(webBook: WebBook, book: Book) { private fun loadBookToc(webBook: WebBook, book: Book) {
webBook.getChapterList(book, this) webBook.getChapterList(this, book)
.onSuccess(IO) { chapters -> .onSuccess(IO) { chapters ->
if (chapters.isNotEmpty()) { if (chapters.isNotEmpty()) {
book.latestChapterTitle = chapters.last().title book.latestChapterTitle = chapters.last().title

@ -34,7 +34,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
val source = bookSource val source = bookSource
val url = exploreUrl val url = exploreUrl
if (source != null && url != null) { if (source != null && url != null) {
WebBook(source).exploreBook(url, page, this) WebBook(source).exploreBook(this, url, page)
.timeout(30000L) .timeout(30000L)
.onSuccess(IO) { searchBooks -> .onSuccess(IO) { searchBooks ->
booksData.postValue(searchBooks) booksData.postValue(searchBooks)

@ -57,7 +57,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
loadChapter(book, changeDruChapterIndex) loadChapter(book, changeDruChapterIndex)
} else { } else {
App.db.bookSourceDao.getBookSource(book.origin)?.let { bookSource -> App.db.bookSourceDao.getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getBookInfo(book, this, canReName = canReName) WebBook(bookSource).getBookInfo(this, book, canReName = canReName)
.onSuccess(IO) { .onSuccess(IO) {
bookData.postValue(book) bookData.postValue(book)
if (inBookshelf) { if (inBookshelf) {
@ -88,7 +88,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
} else { } else {
App.db.bookSourceDao.getBookSource(book.origin)?.let { bookSource -> App.db.bookSourceDao.getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getChapterList(book, this) WebBook(bookSource).getChapterList(this, book)
.onSuccess(IO) { .onSuccess(IO) {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
if (inBookshelf) { if (inBookshelf) {

@ -109,7 +109,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (book.isLocalBook()) { if (book.isLocalBook()) {
loadChapterList(book, changeDruChapterIndex) loadChapterList(book, changeDruChapterIndex)
} else { } else {
ReadBook.webBook?.getBookInfo(book, this, canReName = false) ReadBook.webBook?.getBookInfo(this, book, canReName = false)
?.onSuccess { ?.onSuccess {
loadChapterList(book, changeDruChapterIndex) loadChapterList(book, changeDruChapterIndex)
} }
@ -138,7 +138,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.upMsg("LoadTocError:${it.localizedMessage}") ReadBook.upMsg("LoadTocError:${it.localizedMessage}")
} }
} else { } else {
ReadBook.webBook?.getChapterList(book, this) ReadBook.webBook?.getChapterList(this, book)
?.onSuccess(IO) { cList -> ?.onSuccess(IO) { cList ->
if (cList.isNotEmpty()) { if (cList.isNotEmpty()) {
if (changeDruChapterIndex == null) { if (changeDruChapterIndex == null) {
@ -215,8 +215,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
App.db.bookSourceDao.allTextEnabled.forEach { source -> App.db.bookSourceDao.allTextEnabled.forEach { source ->
try { try {
WebBook(source) WebBook(source).searchBookAwait(this, name)
.searchBookAwait(this, name)
.getOrNull(0)?.let { .getOrNull(0)?.let {
if (it.name == name && (it.author == author || author == "")) { if (it.name == name && (it.author == author || author == "")) {
val book = it.toBook() val book = it.toBook()

@ -28,11 +28,14 @@ class BookSourceDebugModel(application: Application) : BaseViewModel(application
} }
fun startDebug(key: String, start: (() -> Unit)? = null, error: (() -> Unit)? = null) { fun startDebug(key: String, start: (() -> Unit)? = null, error: (() -> Unit)? = null) {
webBook?.let { execute {
Debug.callback = this@BookSourceDebugModel
Debug.startDebug(this, webBook!!, key)
}.onStart {
start?.invoke() start?.invoke()
Debug.callback = this }.onError {
Debug.startDebug(it, key) error?.invoke()
} ?: error?.invoke() }
} }
override fun printLog(state: Int, msg: String) { override fun printLog(state: Int, msg: String) {

@ -75,7 +75,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
} }
App.db.bookSourceDao.getBookSource(book.origin)?.let { bookSource -> App.db.bookSourceDao.getBookSource(book.origin)?.let { bookSource ->
val webBook = WebBook(bookSource) val webBook = WebBook(bookSource)
webBook.getChapterList(book, context = upTocPool) webBook.getChapterList(this, book, context = upTocPool)
.timeout(300000) .timeout(300000)
.onSuccess(IO) { .onSuccess(IO) {
App.db.bookDao.update(book) App.db.bookDao.update(book)
@ -115,7 +115,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
var addToCache = false var addToCache = false
while (!addToCache) { while (!addToCache) {
if (CacheBook.downloadCount() < 5) { if (CacheBook.downloadCount() < 5) {
CacheBook.download(webBook, book, chapter) CacheBook.download(this, webBook, book, chapter)
addToCache = true addToCache = true
} else { } else {
delay(1000) delay(1000)

@ -43,7 +43,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
origin = bookSource.bookSourceUrl, origin = bookSource.bookSourceUrl,
originName = bookSource.bookSourceName originName = bookSource.bookSourceName
) )
WebBook(bookSource).getBookInfo(book, this) WebBook(bookSource).getBookInfo(this, book)
.onSuccess(IO) { .onSuccess(IO) {
it.order = App.db.bookDao.maxOrder + 1 it.order = App.db.bookDao.maxOrder + 1
App.db.bookDao.insert(it) App.db.bookDao.insert(it)
@ -71,13 +71,6 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
val bookMap = hashMapOf<String, String?>() val bookMap = hashMapOf<String, String?>()
bookMap["name"] = it.name bookMap["name"] = it.name
bookMap["author"] = it.author bookMap["author"] = it.author
bookMap["bookUrl"] = it.bookUrl
bookMap["coverUrl"] = it.coverUrl
bookMap["tocUrl"] = it.tocUrl
bookMap["kind"] = it.kind
bookMap["intro"] = it.getDisplayIntro()
bookMap["origin"] = it.origin
bookMap["originName"] = it.originName
exportList.add(bookMap) exportList.add(bookMap)
} }
GSON.toJson(exportList) GSON.toJson(exportList)
@ -96,23 +89,20 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
} }
} }
text.isJsonArray() -> { text.isJsonArray() -> {
val bookSources = App.db.bookSourceDao.allEnabled
GSON.fromJsonArray<Map<String, String?>>(text)?.forEach { GSON.fromJsonArray<Map<String, String?>>(text)?.forEach {
val book = Book( val name = it["name"] ?: ""
bookUrl = it["bookUrl"] ?: "", val author = it["author"] ?: ""
name = it["name"] ?: "", bookSources.forEach { bookSource ->
author = it["author"] ?: "", runCatching {
coverUrl = it["coverUrl"], val webBook = WebBook(bookSource)
tocUrl = it["tocUrl"] ?: "", val searchBooks = webBook.searchBookAwait(this, name)
kind = it["kind"], val searchBook = searchBooks.firstOrNull()
intro = it["intro"] ?: "", if (searchBook != null && searchBook.name == name && searchBook.author == author) {
origin = it["origin"] ?: "", val book = webBook.getBookInfoAwait(this, searchBook.toBook())
originName = it["originName"] ?: "" App.db.bookDao.insert(book)
) }
if (groupId > 0) { }
book.group = groupId
}
if (App.db.bookDao.getBook(book.name, book.author) == null) {
App.db.bookDao.insert(book)
} }
} }
} }

@ -57,7 +57,7 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) :
} }
App.db.bookSourceDao.getBookSource(tag)?.let { App.db.bookSourceDao.getBookSource(tag)?.let {
Debug.callback = this Debug.callback = this
Debug.startDebug(WebBook(it), key) Debug.startDebug(this, WebBook(it), key)
} }
} }
} }

Loading…
Cancel
Save