diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt index de8760317..4cb536198 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.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() private val searchBooks = Collections.synchronizedList(arrayListOf()) + private val tocMap = ConcurrentHashMap>() 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, 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 ?: "获取目录出错") } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt index 117cd26b5..a3ad2b0bc 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt @@ -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>() - 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, 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,