pull/32/head
kunfei 5 years ago
parent 1b1ea37679
commit 56671dab43
  1. 24
      app/src/main/java/io/legado/app/model/WebBook.kt
  2. 4
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt
  3. 3
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt
  4. 68
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt

@ -17,7 +17,12 @@ class WebBook(private val bookSource: BookSource) {
val sourceUrl: String
get() = bookSource.bookSourceUrl
fun searchBook(key: String, page: Int?, isSearch: Boolean = true, scope: CoroutineScope = Coroutine.DEFAULT): Coroutine<List<SearchBook>> {
fun searchBook(
key: String,
page: Int? = 1,
isSearch: Boolean = true,
scope: CoroutineScope = Coroutine.DEFAULT
): Coroutine<List<SearchBook>> {
return Coroutine.async(scope) {
bookSource.getSearchRule().searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl(searchUrl, key, page, baseUrl = sourceUrl)
@ -27,8 +32,8 @@ class WebBook(private val bookSource: BookSource) {
}
}
fun getBookInfo(book: Book): Coroutine<Book> {
return Coroutine.async {
fun getBookInfo(book: Book, scope: CoroutineScope = Coroutine.DEFAULT): Coroutine<Book> {
return Coroutine.async(scope) {
val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.bookUrl, baseUrl = sourceUrl)
val response = analyzeUrl.getResponseAsync().await()
BookInfo.analyzeBookInfo(book, response.body(), bookSource, analyzeUrl)
@ -36,16 +41,21 @@ class WebBook(private val bookSource: BookSource) {
}
}
fun getChapterList(book: Book): Coroutine<List<BookChapter>> {
return Coroutine.async {
fun getChapterList(book: Book, scope: CoroutineScope = Coroutine.DEFAULT): Coroutine<List<BookChapter>> {
return Coroutine.async(scope) {
val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.tocUrl, baseUrl = book.bookUrl)
val response = analyzeUrl.getResponseAsync().await()
BookChapterList.analyzeChapterList(this, book, response, bookSource, analyzeUrl)
}
}
fun getContent(book: Book, bookChapter: BookChapter, nextChapterUrl: String? = null): Coroutine<String> {
return Coroutine.async {
fun getContent(
book: Book,
bookChapter: BookChapter,
nextChapterUrl: String? = null,
scope: CoroutineScope = Coroutine.DEFAULT
): Coroutine<String> {
return Coroutine.async(scope) {
val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = bookChapter.url, baseUrl = book.tocUrl)
val response = analyzeUrl.getResponseAsync().await()
BookContent.analyzeContent(this, response, book, bookChapter, bookSource, nextChapterUrl)

@ -48,7 +48,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fun loadBookInfo(book: Book) {
isLoadingData.postValue(true)
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getBookInfo(book)
WebBook(bookSource).getBookInfo(book, this)
.onSuccess {
it?.let { loadChapter(it) }
}.onError {
@ -60,7 +60,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fun loadChapter(book: Book) {
isLoadingData.postValue(true)
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getChapterList(book)
WebBook(bookSource).getChapterList(book, this)
.onSuccess(IO) {
it?.let {
if (it.isNotEmpty()) {

@ -60,7 +60,7 @@ class ChangeSourceDialog : DialogFragment() {
showTitle()
initRecyclerView()
initSearchView()
viewModel.startSearch()
viewModel.initData()
}
override fun onStart() {
@ -112,6 +112,7 @@ class ChangeSourceDialog : DialogFragment() {
}
override fun onQueryTextChange(newText: String?): Boolean {
viewModel.screen(newText)
return false
}

@ -3,20 +3,84 @@ package io.legado.app.ui.changesource
import android.app.Application
import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook
import io.legado.app.model.WebBook
import kotlinx.coroutines.Dispatchers
class ChangeSourceViewModel(application: Application) : BaseViewModel(application) {
var curBookUrl = ""
var name: String = ""
var author: String = ""
val searchBookData = MutableLiveData<List<SearchBook>>()
private val searchBooks = arrayListOf<SearchBook>()
fun startSearch() {
fun initData() {
execute {
App.db.searchBookDao().getByNameAuthorEnable(name, author).let {
searchBookData.postValue(it)
searchBooks.addAll(it)
searchBookData.postValue(searchBooks)
}
}
}
fun search() {
execute {
val bookSourceList = App.db.bookSourceDao().allEnabled
for (item in bookSourceList) {
//task取消时自动取消 by (scope = this@execute)
WebBook(item).searchBook(name, scope = this@execute)
.timeout(30000L)
.onSuccess(Dispatchers.IO) {
it?.let { list ->
list.map { searchBook ->
if (searchBook.name == name && searchBook.author == author) {
if (searchBook.tocUrl.isEmpty()) {
loadBookInfo(searchBook.toBook())
} else {
loadChapter(searchBook.toBook())
}
}
}
}
}
}
}
}
private fun loadBookInfo(book: Book) {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getBookInfo(book, this)
.onSuccess {
it?.let { loadChapter(it) }
}.onError {
toast(R.string.error_get_book_info)
}
} ?: toast(R.string.error_no_source)
}
private fun loadChapter(book: Book) {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getChapterList(book, this)
.onSuccess(Dispatchers.IO) {
it?.let {
if (it.isNotEmpty()) {
}
}
}.onError {
toast(R.string.error_get_chapter_list)
}
} ?: toast(R.string.error_no_source)
}
fun screen(key: String?) {
if (key.isNullOrEmpty()) {
searchBookData.postValue(searchBooks)
} else {
}
}
}
Loading…
Cancel
Save