pull/1751/head
kunfei 3 years ago
parent abe1c5516d
commit 0848064332
  1. 18
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt
  2. 25
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt

@ -33,6 +33,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import splitties.init.appCtx
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import kotlin.math.min
@ -48,6 +49,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
private var screenKey: String = ""
private var bookSourceList = arrayListOf<BookSource>()
private val searchBooks = Collections.synchronizedList(arrayListOf<SearchBook>())
private val tocMap = ConcurrentHashMap<String, List<BookChapter>>()
private val searchGroup get() = appCtx.getPrefString("searchGroup") ?: ""
private var searchCallback: SourceCallback? = null
val searchDataFlow = callbackFlow {
@ -196,6 +198,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
private suspend fun loadBookToc(scope: CoroutineScope, source: BookSource, book: Book) {
val chapters = WebBook.getChapterListAwait(scope, source, book)
tocMap[book.bookUrl] = chapters
book.latestChapterTitle = chapters.last().title
val searchBook: SearchBook = book.toSearchBook()
searchCallback?.searchSuccess(searchBook)
@ -262,17 +265,24 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
searchStateData.postValue(false)
}
open fun getToc(
fun getToc(
book: Book,
onError: (msg: String) -> Unit,
onSuccess: (toc: List<BookChapter>, source: BookSource) -> Unit
) {
execute {
getToc(book).getOrThrow()
}.onError {
onError.invoke(it.localizedMessage ?: "加载目录错误")
val toc = tocMap[book.bookUrl]
if (toc != null) {
val source = appDb.bookSourceDao.getBookSource(book.origin)
return@execute Pair(toc, source!!)
}
val result = getToc(book).getOrThrow()
tocMap[book.bookUrl] = result.first
return@execute result
}.onSuccess {
onSuccess.invoke(it.first, it.second)
}.onError {
onError.invoke(it.localizedMessage ?: "获取目录出错")
}
}

@ -5,10 +5,8 @@ import android.os.Bundle
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource
import io.legado.app.exception.NoStackTraceException
import io.legado.app.model.webBook.WebBook
import java.util.concurrent.ConcurrentHashMap
@Suppress("MemberVisibilityCanBePrivate")
class ChangeChapterSourceViewModel(application: Application) :
@ -17,8 +15,6 @@ class ChangeChapterSourceViewModel(application: Application) :
var chapterIndex: Int = 0
var chapterTitle: String = ""
private val tocMap = ConcurrentHashMap<String, List<BookChapter>>()
override fun initData(arguments: Bundle?) {
super.initData(arguments)
arguments?.let { bundle ->
@ -29,27 +25,6 @@ class ChangeChapterSourceViewModel(application: Application) :
}
}
override fun getToc(
book: Book,
onError: (msg: String) -> Unit,
onSuccess: (toc: List<BookChapter>, source: BookSource) -> Unit
) {
execute {
val toc = tocMap[book.bookUrl]
if (toc != null) {
val source = appDb.bookSourceDao.getBookSource(book.origin)
return@execute Pair(toc, source!!)
}
val result = getToc(book).getOrThrow()
tocMap[book.bookUrl] = result.first
return@execute result
}.onSuccess {
onSuccess.invoke(it.first, it.second)
}.onError {
onError.invoke(it.localizedMessage ?: "获取目录出错")
}
}
fun getContent(
book: Book,
chapter: BookChapter,

Loading…
Cancel
Save