From 56671dab43c84feb07b95c43c9b0845b8e12e8cd Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 18 Aug 2019 15:50:26 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/model/WebBook.kt | 24 +++++-- .../app/ui/bookinfo/BookInfoViewModel.kt | 4 +- .../app/ui/changesource/ChangeSourceDialog.kt | 3 +- .../ui/changesource/ChangeSourceViewModel.kt | 68 ++++++++++++++++++- 4 files changed, 87 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index f8b7148c9..972ec4b87 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.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> { + fun searchBook( + key: String, + page: Int? = 1, + isSearch: Boolean = true, + scope: CoroutineScope = Coroutine.DEFAULT + ): Coroutine> { 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 { - return Coroutine.async { + fun getBookInfo(book: Book, scope: CoroutineScope = Coroutine.DEFAULT): Coroutine { + 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> { - return Coroutine.async { + fun getChapterList(book: Book, scope: CoroutineScope = Coroutine.DEFAULT): Coroutine> { + 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 { - return Coroutine.async { + fun getContent( + book: Book, + bookChapter: BookChapter, + nextChapterUrl: String? = null, + scope: CoroutineScope = Coroutine.DEFAULT + ): Coroutine { + 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) diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt index 6479ce005..43c409212 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt @@ -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()) { diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt index 9646a922d..775b1e33b 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt @@ -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 } diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt index 579d79bb2..49d602256 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt @@ -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>() + private val searchBooks = arrayListOf() - 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 { + + } + } } \ No newline at end of file