pull/467/head
gedoor 4 years ago
parent be9a2d3df5
commit 580afb13de
  1. 37
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt

@ -23,7 +23,6 @@ import kotlinx.coroutines.asCoroutineDispatcher
import org.jetbrains.anko.debug
import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.Executors
import kotlin.math.min
class ChangeSourceViewModel(application: Application) : BaseViewModel(application) {
private val threadCount = AppConfig.threadCount
@ -39,6 +38,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
private val searchBooks = CopyOnWriteArraySet<SearchBook>()
private var postTime = 0L
private val sendRunnable = Runnable { upAdapter() }
@Volatile
private var searchIndex = -1
@ -113,24 +113,28 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
return
}
searchIndex++
}
val source = bookSourceList[searchIndex]
val variableBook = SearchBook()
val task = WebBook(source)
val webBook = WebBook(source)
val task = webBook
.searchBook(name, variableBook = variableBook, scope = this, context = searchPool!!)
.timeout(60000L)
.onSuccess(IO) {
it.forEach { searchBook ->
if (searchBook.name == name && searchBook.author == author) {
if (searchBook.latestChapterTitle.isNullOrEmpty()) {
if (context.getPrefBoolean(PreferKey.changeSourceLoadInfo) || context.getPrefBoolean(PreferKey.changeSourceLoadToc)) {
loadBookInfo(searchBook.toBook())
if (context.getPrefBoolean(PreferKey.changeSourceLoadInfo)
|| context.getPrefBoolean(PreferKey.changeSourceLoadToc)
) {
loadBookInfo(webBook, searchBook.toBook())
} else {
searchFinish(searchBook)
}
} else {
searchFinish(searchBook)
}
return@forEach
return@onSuccess
}
}
}
@ -141,8 +145,8 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
} else {
searchIndex++
}
if (searchIndex >= bookSourceList.lastIndex + min(bookSourceList.size,
threadCount)
if (searchIndex >= bookSourceList.lastIndex + bookSourceList.size
|| searchIndex >= bookSourceList.lastIndex + threadCount
) {
searchStateData.postValue(false)
}
@ -150,15 +154,12 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
}
tasks.add(task)
}
}
private fun loadBookInfo(book: Book) {
execute {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getBookInfo(book, this)
private fun loadBookInfo(webBook: WebBook, book: Book) {
webBook.getBookInfo(book, this)
.onSuccess {
if (context.getPrefBoolean(PreferKey.changeSourceLoadToc)) {
loadChapter(it)
loadChapter(webBook, book)
} else {
//从详情页里获取最新章节
book.latestChapterTitle = it.latestChapterTitle
@ -168,14 +169,10 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
}.onError {
debug { context.getString(R.string.error_get_book_info) }
}
} ?: debug { context.getString(R.string.error_no_source) }
}
}
private fun loadChapter(book: Book) {
execute {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getChapterList(book, this)
private fun loadChapter(webBook: WebBook, book: Book) {
webBook.getChapterList(book, this)
.onSuccess(IO) { chapters ->
if (chapters.isNotEmpty()) {
book.latestChapterTitle = chapters.last().title
@ -185,8 +182,6 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
}.onError {
debug { context.getString(R.string.error_get_chapter_list) }
}
} ?: debug { R.string.error_no_source }
}
}
/**

Loading…
Cancel
Save