pull/467/head
gedoor 4 years ago
parent be9a2d3df5
commit 580afb13de
  1. 121
      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,80 +113,75 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
return return
} }
searchIndex++ searchIndex++
val source = bookSourceList[searchIndex] }
val variableBook = SearchBook() val source = bookSourceList[searchIndex]
val task = WebBook(source) val variableBook = SearchBook()
.searchBook(name, variableBook = variableBook, scope = this, context = searchPool!!) val webBook = WebBook(source)
.timeout(60000L) val task = webBook
.onSuccess(IO) { .searchBook(name, variableBook = variableBook, scope = this, context = searchPool!!)
it.forEach { searchBook -> .timeout(60000L)
if (searchBook.name == name && searchBook.author == author) { .onSuccess(IO) {
if (searchBook.latestChapterTitle.isNullOrEmpty()) { it.forEach { searchBook ->
if (context.getPrefBoolean(PreferKey.changeSourceLoadInfo) || context.getPrefBoolean(PreferKey.changeSourceLoadToc)) { if (searchBook.name == name && searchBook.author == author) {
loadBookInfo(searchBook.toBook()) if (searchBook.latestChapterTitle.isNullOrEmpty()) {
} else { if (context.getPrefBoolean(PreferKey.changeSourceLoadInfo)
searchFinish(searchBook) || context.getPrefBoolean(PreferKey.changeSourceLoadToc)
} ) {
loadBookInfo(webBook, searchBook.toBook())
} else { } else {
searchFinish(searchBook) searchFinish(searchBook)
} }
return@forEach } else {
searchFinish(searchBook)
} }
return@onSuccess
} }
} }
.onFinally { }
synchronized(this) { .onFinally {
if (searchIndex < bookSourceList.lastIndex) { synchronized(this) {
search() if (searchIndex < bookSourceList.lastIndex) {
} else { search()
searchIndex++ } else {
} searchIndex++
if (searchIndex >= bookSourceList.lastIndex + min(bookSourceList.size, }
threadCount) if (searchIndex >= bookSourceList.lastIndex + bookSourceList.size
) { || searchIndex >= bookSourceList.lastIndex + threadCount
searchStateData.postValue(false) ) {
} searchStateData.postValue(false)
} }
} }
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 -> .onSuccess {
WebBook(bookSource).getBookInfo(book, this) if (context.getPrefBoolean(PreferKey.changeSourceLoadToc)) {
.onSuccess { loadChapter(webBook, book)
if (context.getPrefBoolean(PreferKey.changeSourceLoadToc)) { } else {
loadChapter(it) //从详情页里获取最新章节
} else { book.latestChapterTitle = it.latestChapterTitle
//从详情页里获取最新章节 val searchBook = book.toSearchBook()
book.latestChapterTitle = it.latestChapterTitle searchFinish(searchBook)
val searchBook = book.toSearchBook() }
searchFinish(searchBook) }.onError {
} debug { context.getString(R.string.error_get_book_info) }
}.onError { }
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 -> .onSuccess(IO) { chapters ->
WebBook(bookSource).getChapterList(book, this) if (chapters.isNotEmpty()) {
.onSuccess(IO) { chapters -> book.latestChapterTitle = chapters.last().title
if (chapters.isNotEmpty()) { val searchBook: SearchBook = book.toSearchBook()
book.latestChapterTitle = chapters.last().title searchFinish(searchBook)
val searchBook: SearchBook = book.toSearchBook() }
searchFinish(searchBook) }.onError {
} debug { context.getString(R.string.error_get_chapter_list) }
}.onError { }
debug { context.getString(R.string.error_get_chapter_list) }
}
} ?: debug { R.string.error_no_source }
}
} }
/** /**

Loading…
Cancel
Save