fix(CustomExport): 修复导出章节列表越界的问题,优化导出逻辑

This commit is contained in:
Discut
2023-05-23 13:11:20 +08:00
parent 4bda1c400e
commit ee0e085519
2 changed files with 42 additions and 8 deletions
@@ -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)
}
@@ -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 <内容模板字符串, <epub文件名, epub对象>>
*/
private fun createEpubs(doc: DocumentFile, book: Book): Pair<String, List<EpubBook>> {
private fun createEpubs(
doc: DocumentFile,
book: Book
): Pair<String, List<Pair<String, EpubBook>>> {
val paresNumOfEpub = paresNumOfEpub(scope.size, size)
val result: MutableList<EpubBook> = ArrayList(paresNumOfEpub)
val result: MutableList<Pair<String, EpubBook>> = 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)
}