From 9588c1a62213a9ae9900752c291690f5f469abfe Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 15 Mar 2020 18:48:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/BookSourceDao.kt | 5 ++- .../main/java/io/legado/app/model/WebBook.kt | 42 +++++++++++-------- .../io/legado/app/service/help/ReadBook.kt | 15 +++---- .../app/ui/book/read/ReadBookViewModel.kt | 31 ++++++++++---- .../app/ui/book/read/page/TextPageFactory.kt | 9 ++++ 5 files changed, 69 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index 9ce5c65be..55d5ada4c 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -50,9 +50,12 @@ interface BookSourceDao { @get:Query("select * from book_sources order by customOrder asc") val all: List - @get:Query("select * from book_sources where enabled = 1 order by customOrder asc") + @get:Query("select * from book_sources where enabled = 1 order by customOrder") val allEnabled: List + @get:Query("select * from book_sources where enabled = 1 and bookSourceType = 0 order by customOrder") + val allTextEnabled: List + @Query("select * from book_sources where bookSourceUrl = :key") fun getBookSource(key: String): BookSource? 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 69dfb943c..7d14ea8bc 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -29,26 +29,34 @@ class WebBook(val bookSource: BookSource) { context: CoroutineContext = Dispatchers.IO ): Coroutine> { return Coroutine.async(scope, context) { - bookSource.searchUrl?.let { searchUrl -> - val analyzeUrl = AnalyzeUrl( - ruleUrl = searchUrl, - key = key, - page = page, - baseUrl = sourceUrl, - headerMapF = bookSource.getHeaderMap() - ) - val res = analyzeUrl.getResponseAwait(bookSource.bookSourceUrl) - BookList.analyzeBookList( - res.body, - bookSource, - analyzeUrl, - res.url, - true - ) - } ?: arrayListOf() + searchBookSuspend(key, page) } } + suspend fun searchBookSuspend( + key: String, + page: Int? = 1 + ): ArrayList { + bookSource.searchUrl?.let { searchUrl -> + val analyzeUrl = AnalyzeUrl( + ruleUrl = searchUrl, + key = key, + page = page, + baseUrl = sourceUrl, + headerMapF = bookSource.getHeaderMap() + ) + val res = analyzeUrl.getResponseAwait(bookSource.bookSourceUrl) + return BookList.analyzeBookList( + res.body, + bookSource, + analyzeUrl, + res.url, + true + ) + } + return arrayListOf() + } + /** * 发现 */ diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index 7b61ca628..a05009dc8 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -21,7 +21,6 @@ import org.jetbrains.anko.toast object ReadBook { - var titleDate = MutableLiveData() var book: Book? = null var inBookshelf = false @@ -34,9 +33,10 @@ object ReadBook { var curTextChapter: TextChapter? = null var nextTextChapter: TextChapter? = null var webBook: WebBook? = null + var msg: String? = null private val loadingChapters = arrayListOf() - fun resetData(book: Book, noSource: () -> Unit) { + fun resetData(book: Book, noSource: (name: String, author: String) -> Unit) { this.book = book titleDate.postValue(book.name) durChapterIndex = book.durChapterIndex @@ -46,19 +46,20 @@ object ReadBook { prevTextChapter = null curTextChapter = null nextTextChapter = null - upWebBook(book.origin, noSource) + upWebBook(book, noSource) } - fun upWebBook(origin: String, noSource: () -> Unit) { - if (origin == BookType.local) { + fun upWebBook(book: Book?, noSource: (name: String, author: String) -> Unit) { + book ?: return + if (book.origin == BookType.local) { webBook = null } else { - val bookSource = App.db.bookSourceDao().getBookSource(origin) + val bookSource = App.db.bookSourceDao().getBookSource(book.origin) if (bookSource != null) { webBook = WebBook(bookSource) } else { webBook = null - noSource.invoke() + noSource.invoke(book.name, book.author) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 2ce667b14..3519f3b08 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -39,8 +39,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { private fun initBook(book: Book) { if (ReadBook.book?.bookUrl != book.bookUrl) { - ReadBook.resetData(book) { - autoChangeSource() + ReadBook.resetData(book) { name, author -> + autoChangeSource(name, author) } isInitFinish = true ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl) @@ -62,8 +62,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } else { isInitFinish = true ReadBook.titleDate.postValue(book.name) - ReadBook.upWebBook(book.origin) { - autoChangeSource() + ReadBook.upWebBook(book) { name, author -> + autoChangeSource(name, author) } ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl) if (ReadBook.chapterSize == 0) { @@ -126,7 +126,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } }?.onError { toast(R.string.error_load_toc) - } ?: autoChangeSource() + } ?: autoChangeSource(book.name, book.author) } } } @@ -155,8 +155,23 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } - private fun autoChangeSource() { + private fun autoChangeSource(name: String, author: String) { + execute { + App.db.bookSourceDao().allTextEnabled.forEach { + try { + val searchBooks = WebBook(it).searchBookSuspend(name) + + } catch (e: Exception) { + } + } + }.onStart { + ReadBook.msg = "正在自动换源" + ReadBook.callBack?.upContent() + }.onFinally { + ReadBook.msg = null + ReadBook.callBack?.upContent() + } } private fun upChangeDurChapterIndex(book: Book, chapters: List) { @@ -212,9 +227,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { execute { App.db.bookChapterDao().getChapter(book.bookUrl, ReadBook.durChapterIndex) ?.let { chapter -> - BookHelp.delContent(book, chapter) + BookHelp.delContent(book, chapter) ReadBook.loadContent(ReadBook.durChapterIndex) - } + } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt index a867daf3d..27a407cd6 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt @@ -59,6 +59,9 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource override val currentPage: TextPage get() = with(dataSource) { + ReadBook.msg?.let { + return@with TextPage(text = it).format() + } currentChapter?.let { return@with it.page(pageIndex) ?: TextPage(title = it.title).format() @@ -68,6 +71,9 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource override val nextPage: TextPage get() = with(dataSource) { + ReadBook.msg?.let { + return@with TextPage(text = it).format() + } currentChapter?.let { if (pageIndex < it.pageSize() - 1) { return@with it.page(pageIndex + 1)?.removePageAloudSpan() @@ -86,6 +92,9 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource override val prevPage: TextPage get() = with(dataSource) { + ReadBook.msg?.let { + return@with TextPage(text = it).format() + } if (pageIndex > 0) { currentChapter?.let { return@with it.page(pageIndex - 1)?.removePageAloudSpan()