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

Loading…
Cancel
Save