From ee0e085519d4493400bf5a0f0bf10cc086360db6 Mon Sep 17 00:00:00 2001 From: Discut Date: Tue, 23 May 2023 13:11:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(CustomExport):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E7=AB=A0=E8=8A=82=E5=88=97=E8=A1=A8=E8=B6=8A?= =?UTF-8?q?=E7=95=8C=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/book/BookExtensions.kt | 21 ++++++++++++++ .../app/ui/book/cache/CacheViewModel.kt | 29 ++++++++++++++----- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/book/BookExtensions.kt b/app/src/main/java/io/legado/app/help/book/BookExtensions.kt index d7a820246..a5a95db19 100644 --- a/app/src/main/java/io/legado/app/help/book/BookExtensions.kt +++ b/app/src/main/java/io/legado/app/help/book/BookExtensions.kt @@ -243,4 +243,25 @@ fun Book.getExportFileName(suffix: String): String { }.onFailure { AppLog.put("导出书名规则错误,使用默认规则\n${it.localizedMessage}", it) }.getOrDefault("${name} 作者:${getRealAuthor()}.$suffix") +} + +/** + * 获取分割文件后的文件名 + */ +fun Book.getExportFileName(suffix: String, epubIndex: Int): String { + val jsStr = AppConfig.bookExportFileName + // 默认规则 + val default = "$name 作者:${getRealAuthor()} [${epubIndex}].$suffix" + if (jsStr.isNullOrBlank()) { + return default + } + val bindings = SimpleBindings() + bindings["name"] = name + bindings["author"] = getRealAuthor() + bindings["epubIndex"] = epubIndex + return kotlin.runCatching { + RhinoScriptEngine.eval(jsStr, bindings).toString() + "." + suffix + }.onFailure { + AppLog.put("导出书名规则错误,使用默认规则\n${it.localizedMessage}", it) + }.getOrDefault(default) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index c4d378964..8d13e2176 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -689,10 +689,11 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { */ private suspend fun exportEpub(doc: DocumentFile, book: Book) { val (contentModel, epubList) = createEpubs(doc, book) - epubList.forEachIndexed { index, epubBook -> + epubList.forEachIndexed { index, ep -> + val (filename, epubBook) = ep //设置正文 this.setEpubContent(contentModel, book, epubBook, index) - save2Drive(book.name + index + ".epub", epubBook, doc) + save2Drive(filename, epubBook, doc) } } @@ -717,13 +718,17 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { if (scope.indexOf(index) >= 0) { chapterList.add(chapter) } + if (scope.size == chapterList.size) { + return@forEachIndexed + } } val totalChapterNum = book.totalChapterNum / scope.size - chapterList = chapterList.subList(epubBookIndex * size, (epubBookIndex + 1) * size) + chapterList = chapterList.subList(epubBookIndex * size, if((epubBookIndex + 1) * size > scope.size) scope.size else (epubBookIndex + 1) * size) chapterList.forEachIndexed { index, chapter -> coroutineContext.ensureActive() context.upAdapterLiveData.postValue(book.bookUrl) - context.exportProgress[book.bookUrl] = totalChapterNum * (epubBookIndex * size + index) + context.exportProgress[book.bookUrl] = + totalChapterNum * (epubBookIndex * size + index) BookHelp.getContent(book, chapter).let { content -> var content1 = context.fixPic( epubBook, @@ -764,13 +769,21 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { /** * 创建多个epub 对象 + * + * @param doc 导出文档 + * @param book 书籍 + * + * @return <内容模板字符串, > */ - private fun createEpubs(doc: DocumentFile, book: Book): Pair> { + private fun createEpubs( + doc: DocumentFile, + book: Book + ): Pair>> { val paresNumOfEpub = paresNumOfEpub(scope.size, size) - val result: MutableList = ArrayList(paresNumOfEpub) + val result: MutableList> = ArrayList(paresNumOfEpub) var contentModel = "" for (i in 1..paresNumOfEpub) { - val filename = book.getExportFileName("epub") + val filename = book.getExportFileName("epub", i) DocumentUtils.delete(doc, filename) val epubBook = EpubBook() epubBook.version = "2.0" @@ -782,7 +795,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { contentModel = context.setAssets(doc, book, epubBook) // add epubBook - result.add(epubBook) + result.add(Pair(filename, epubBook)) } return Pair(contentModel, result) }