From 878635559fcc50f51532f8ca17105c13576dd6e5 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 14 Sep 2021 14:17:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/model/Exceptions.kt | 2 + .../legado/app/model/localBook/LocalBook.kt | 8 +- .../app/model/webBook/BookChapterList.kt | 4 + .../io/legado/app/model/webBook/WebBook.kt | 18 +-- .../app/ui/book/audio/AudioPlayActivity.kt | 5 +- .../app/ui/book/audio/AudioPlayViewModel.kt | 21 ++-- .../book/changesource/ChangeSourceDialog.kt | 18 ++- .../app/ui/book/info/BookInfoActivity.kt | 5 +- .../app/ui/book/info/BookInfoViewModel.kt | 18 ++- .../app/ui/book/read/ReadBookActivity.kt | 5 +- .../app/ui/book/read/ReadBookViewModel.kt | 114 +++++++----------- .../ui/main/bookshelf/BookshelfViewModel.kt | 2 +- 12 files changed, 105 insertions(+), 115 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/Exceptions.kt b/app/src/main/java/io/legado/app/model/Exceptions.kt index 827ed4a36..e56a1fbdf 100644 --- a/app/src/main/java/io/legado/app/model/Exceptions.kt +++ b/app/src/main/java/io/legado/app/model/Exceptions.kt @@ -1,5 +1,7 @@ package io.legado.app.model +class AppException(msg: String) : Exception(msg) + /** * 内容为空 */ diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index 7739c3e52..9c566e957 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -9,6 +9,7 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp +import io.legado.app.model.AppException import io.legado.app.utils.* import splitties.init.appCtx import java.io.File @@ -22,8 +23,9 @@ object LocalBook { FileUtils.createFolderIfNotExist(appCtx.externalFiles, folderName) } + @Throws(Exception::class) fun getChapterList(book: Book): ArrayList { - return when { + val chapters = when { book.isEpub() -> { EpubFile.getChapterList(book) } @@ -34,6 +36,10 @@ object LocalBook { TextFile().analyze(book) } } + if (chapters.isEmpty()) { + throw AppException("目录为空") + } + return chapters } fun getContext(book: Book, chapter: BookChapter): String? { diff --git a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt index 0eda36865..59a63a9be 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt @@ -6,6 +6,7 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.TocRule +import io.legado.app.model.AppException import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -97,6 +98,9 @@ object BookChapterList { } } } + if (chapterList.isEmpty()) { + throw AppException("目录为空") + } //去重 if (!reverse) { chapterList.reverse() diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index 4f904c820..76b8767a2 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -23,19 +23,19 @@ object WebBook { bookSources: List, name: String, author: String - ): Book? { - bookSources.forEach { bookSource -> + ): Pair? { + bookSources.forEach { source -> kotlin.runCatching { if (!scope.isActive) return null - searchBookAwait(scope, bookSource, name).firstOrNull { + searchBookAwait(scope, source, name).firstOrNull { it.name == name && it.author == author - }?.let { - return if (it.tocUrl.isBlank()) { - if (!scope.isActive) return null - getBookInfoAwait(scope, bookSource, it.toBook()) - } else { - it.toBook() + }?.let { searchBook -> + if (!scope.isActive) return null + var book = searchBook.toBook() + if (book.tocUrl.isBlank()) { + book = getBookInfoAwait(scope, source, book) } + return Pair(source, book) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt index 7dfcab3b5..a5c779bf9 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt @@ -18,6 +18,7 @@ import io.legado.app.constant.EventBus import io.legado.app.constant.Status import io.legado.app.constant.Theme import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookSource import io.legado.app.databinding.ActivityAudioPlayBinding import io.legado.app.help.BlurTransformation import io.legado.app.help.glide.ImageLoader @@ -193,8 +194,8 @@ class AudioPlayActivity : override val oldBook: Book? get() = AudioPlay.book - override fun changeTo(book: Book) { - viewModel.changeTo(book) + override fun changeTo(source: BookSource, book: Book) { + viewModel.changeTo(source, book) } override fun finish() { diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt index af933a8fa..7e39f94bb 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt @@ -8,6 +8,7 @@ import io.legado.app.constant.EventBus import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter +import io.legado.app.data.entities.BookSource import io.legado.app.help.BookHelp import io.legado.app.model.AudioPlay import io.legado.app.model.webBook.WebBook @@ -90,24 +91,24 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } - fun changeTo(book1: Book) { + fun changeTo(source: BookSource, book: Book) { execute { - var oldTocSize: Int = book1.totalChapterNum + var oldTocSize: Int = book.totalChapterNum AudioPlay.book?.let { oldTocSize = it.totalChapterNum - book1.order = it.order + book.order = it.order appDb.bookDao.delete(it) } - appDb.bookDao.insert(book1) - AudioPlay.book = book1 - AudioPlay.bookSource = appDb.bookSourceDao.getBookSource(book1.origin) - if (book1.tocUrl.isEmpty()) { - loadBookInfo(book1) { upChangeDurChapterIndex(book1, oldTocSize, it) } + appDb.bookDao.insert(book) + AudioPlay.book = book + AudioPlay.bookSource = source + if (book.tocUrl.isEmpty()) { + loadBookInfo(book) { upChangeDurChapterIndex(book, oldTocSize, it) } } else { - loadChapterList(book1) { upChangeDurChapterIndex(book1, oldTocSize, it) } + loadChapterList(book) { upChangeDurChapterIndex(book, oldTocSize, it) } } }.onFinally { - postEvent(EventBus.SOURCE_CHANGED, book1.bookUrl) + postEvent(EventBus.SOURCE_CHANGED, book.bookUrl) } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt index 2c2345145..3b4068e96 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt @@ -16,6 +16,7 @@ import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.data.appDb import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook import io.legado.app.databinding.DialogChangeSourceBinding import io.legado.app.help.AppConfig @@ -228,11 +229,16 @@ class ChangeSourceDialog : BaseDialogFragment(), } private fun changeSource(searchBook: SearchBook) { - val book = searchBook.toBook() - book.upInfoFromOld(callBack?.oldBook) - callBack?.changeTo(book) - searchBook.time = System.currentTimeMillis() - viewModel.updateSource(searchBook) + try { + val book = searchBook.toBook() + book.upInfoFromOld(callBack?.oldBook) + val source = appDb.bookSourceDao.getBookSource(book.origin) + callBack?.changeTo(source!!, book) + searchBook.time = System.currentTimeMillis() + viewModel.updateSource(searchBook) + } catch (e: Exception) { + toastOnUi("换源失败\n${e.localizedMessage}") + } } /** @@ -272,7 +278,7 @@ class ChangeSourceDialog : BaseDialogFragment(), interface CallBack { val oldBook: Book? - fun changeTo(book: Book) + fun changeTo(source: BookSource, book: Book) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index b39626e94..d41f78aa0 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -20,6 +20,7 @@ import io.legado.app.constant.Theme import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter +import io.legado.app.data.entities.BookSource import io.legado.app.databinding.ActivityBookInfoBinding import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.help.BlurTransformation @@ -435,9 +436,9 @@ class BookInfoActivity : override val oldBook: Book? get() = viewModel.bookData.value - override fun changeTo(book: Book) { + override fun changeTo(source: BookSource, book: Book) { upLoading(true) - viewModel.changeTo(book) + viewModel.changeTo(source, book) } override fun coverChangeTo(coverUrl: String) { diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index 3f3243d6b..214bf10c3 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -51,7 +51,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { private fun setBook(book: Book) { durChapterIndex = book.durChapterIndex bookData.postValue(book) - initBookSource(book) + bookSource = if (book.isLocalBook()) { + null + } else { + appDb.bookSourceDao.getBookSource(book.origin) + } if (book.tocUrl.isEmpty()) { loadBookInfo(book) } else { @@ -64,14 +68,6 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } - private fun initBookSource(book: Book) { - bookSource = if (book.isLocalBook()) { - null - } else { - appDb.bookSourceDao.getBookSource(book.origin) - } - } - fun loadBookInfo( book: Book, canReName: Boolean = true, changeDruChapterIndex: ((chapters: List) -> Unit)? = null, @@ -149,7 +145,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } - fun changeTo(newBook: Book) { + fun changeTo(source: BookSource, newBook: Book) { execute { var oldTocSize: Int = newBook.totalChapterNum if (inBookshelf) { @@ -159,7 +155,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } bookData.postValue(newBook) - initBookSource(newBook) + bookSource = source if (newBook.tocUrl.isEmpty()) { loadBookInfo(newBook, false) { upChangeDurChapterIndex(newBook, oldTocSize, it) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 5a6344804..b7483affa 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -20,6 +20,7 @@ import io.legado.app.constant.Status import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookProgress +import io.legado.app.data.entities.BookSource import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadTipConfig import io.legado.app.help.storage.AppWebDav @@ -642,8 +643,8 @@ class ReadBookActivity : ReadBookBaseActivity(), override val oldBook: Book? get() = ReadBook.book - override fun changeTo(book: Book) { - viewModel.changeTo(book) + override fun changeTo(source: BookSource, book: Book) { + viewModel.changeTo(source, book) } override fun showActionMenu() { 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 3f847493e..01ea3aad7 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 @@ -8,8 +8,8 @@ import io.legado.app.base.BaseViewModel import io.legado.app.constant.EventBus import io.legado.app.data.appDb import io.legado.app.data.entities.Book -import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookProgress +import io.legado.app.data.entities.BookSource import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp import io.legado.app.help.ContentProcessor @@ -101,26 +101,22 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } - private fun loadBookInfo( - book: Book, - changeDruChapterIndex: ((chapters: List) -> Unit)? = null, - ) { + private fun loadBookInfo(book: Book) { if (book.isLocalBook()) { - loadChapterList(book, changeDruChapterIndex) + loadChapterList(book) } else { - ReadBook.bookSource?.let { - WebBook.getBookInfo(viewModelScope, it, book, canReName = false) + ReadBook.bookSource?.let { source -> + WebBook.getBookInfo(viewModelScope, source, book, canReName = false) .onSuccess { - loadChapterList(book, changeDruChapterIndex) + loadChapterList(book) + }.onError { + ReadBook.upMsg("详情页出错: ${it.localizedMessage}") } } } } - fun loadChapterList( - book: Book, - changeDruChapterIndex: ((chapters: List) -> Unit)? = null, - ) { + fun loadChapterList(book: Book) { if (book.isLocalBook()) { execute { LocalBook.getChapterList(book).let { @@ -128,12 +124,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { appDb.bookChapterDao.insert(*it.toTypedArray()) appDb.bookDao.update(book) ReadBook.chapterSize = it.size - if (it.isEmpty()) { - ReadBook.upMsg(context.getString(R.string.error_load_toc)) - } else { - ReadBook.upMsg(null) - ReadBook.loadContent(resetPageOffset = true) - } + ReadBook.upMsg(null) + ReadBook.loadContent(resetPageOffset = true) } }.onError { ReadBook.upMsg("LoadTocError:${it.localizedMessage}") @@ -142,19 +134,11 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ReadBook.bookSource?.let { WebBook.getChapterList(viewModelScope, it, book) .onSuccess(IO) { cList -> - if (cList.isNotEmpty()) { - if (changeDruChapterIndex == null) { - appDb.bookChapterDao.insert(*cList.toTypedArray()) - appDb.bookDao.update(book) - ReadBook.chapterSize = cList.size - ReadBook.upMsg(null) - ReadBook.loadContent(resetPageOffset = true) - } else { - changeDruChapterIndex(cList) - } - } else { - ReadBook.upMsg(context.getString(R.string.error_load_toc)) - } + appDb.bookChapterDao.insert(*cList.toTypedArray()) + appDb.bookDao.update(book) + ReadBook.chapterSize = cList.size + ReadBook.upMsg(null) + ReadBook.loadContent(resetPageOffset = true) }.onError { ReadBook.upMsg(context.getString(R.string.error_load_toc)) } @@ -183,23 +167,32 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } - fun changeTo(newBook: Book) { + fun changeTo(source: BookSource, book: Book) { execute { - val oldTocSize: Int = ReadBook.book?.totalChapterNum ?: newBook.totalChapterNum - ReadBook.upMsg(null) - ReadBook.resetData(newBook) - ReadBook.callBack?.upContent() - if (newBook.tocUrl.isEmpty()) { - loadBookInfo(newBook) { - upChangeDurChapterIndex(newBook, oldTocSize, it) - } - } else { - loadChapterList(newBook) { - upChangeDurChapterIndex(newBook, oldTocSize, it) - } + ReadBook.upMsg(context.getString(R.string.loading)) + if (book.tocUrl.isEmpty()) { + WebBook.getBookInfoAwait(this, source, book) + } + val chapters = WebBook.getChapterListAwait(this, source, book) + ReadBook.book!!.let { oldBook -> + book.durChapterIndex = BookHelp.getDurChapter( + oldBook.durChapterIndex, + oldBook.totalChapterNum, + oldBook.durChapterTitle, + chapters + ) + book.durChapterTitle = chapters[ReadBook.durChapterIndex].title + oldBook.changeTo(book) } + appDb.bookChapterDao.insert(*chapters.toTypedArray()) + ReadBook.resetData(book) + ReadBook.upMsg(null) + ReadBook.loadContent(resetPageOffset = true) + }.onError { + context.toastOnUi("换源失败\n${it.localizedMessage}") + ReadBook.upMsg(null) }.onFinally { - postEvent(EventBus.SOURCE_CHANGED, newBook.bookUrl) + postEvent(EventBus.SOURCE_CHANGED, book.bookUrl) } } @@ -207,13 +200,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (!AppConfig.autoChangeSource) return execute { val sources = appDb.bookSourceDao.allTextEnabled - val book = WebBook.preciseSearch(this, sources, name, author) - if (book != null) { - book.upInfoFromOld(ReadBook.book) - changeTo(book) - } else { - throw Exception("自动换源失败") - } + WebBook.preciseSearch(this, sources, name, author)?.let { + it.second.upInfoFromOld(ReadBook.book) + changeTo(it.first, it.second) + } ?: throw Exception("自动换源失败") }.onStart { ReadBook.upMsg(context.getString(R.string.source_auto_changing)) }.onError { @@ -223,24 +213,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } - private fun upChangeDurChapterIndex(book: Book, oldTocSize: Int, chapters: List) { - execute { - ReadBook.durChapterIndex = BookHelp.getDurChapter( - book.durChapterIndex, - oldTocSize, - book.durChapterTitle, - chapters - ) - book.durChapterIndex = ReadBook.durChapterIndex - book.durChapterTitle = chapters[ReadBook.durChapterIndex].title - appDb.bookDao.update(book) - appDb.bookChapterDao.insert(*chapters.toTypedArray()) - ReadBook.chapterSize = chapters.size - ReadBook.upMsg(null) - ReadBook.loadContent(resetPageOffset = true) - } - } - fun openChapter(index: Int, durChapterPos: Int = 0, success: (() -> Unit)? = null) { ReadBook.clearTextChapter() ReadBook.callBack?.upContent() diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index 35c581adc..9ae0066c7 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -112,7 +112,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) val name = it["name"] ?: "" val author = it["author"] ?: "" if (name.isNotEmpty() && appDb.bookDao.getBook(name, author) == null) { - val book = WebBook.preciseSearch(this, bookSources, name, author) + val book = WebBook.preciseSearch(this, bookSources, name, author)?.second book?.let { if (groupId > 0) { book.group = groupId