diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index 02ec43c73..16246645c 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -35,7 +35,6 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import androidx.core.widget.doOnTextChanged /** * cache/download 缓存界面 @@ -136,6 +135,7 @@ class CacheActivity : VMBaseActivity() CacheBook.stop(this@CacheActivity) } } + R.id.menu_export_all -> exportAll() R.id.menu_enable_replace -> AppConfig.exportUseReplace = !item.isChecked // 更改菜单状态[enableCustomExport] @@ -147,6 +147,7 @@ class CacheActivity : VMBaseActivity() R.id.menu_export_folder -> { selectExportFolder(-1) } + R.id.menu_export_file_name -> alertExportFileName() R.id.menu_export_type -> showExportTypeConfig() R.id.menu_export_charset -> showCharsetConfig() @@ -184,6 +185,7 @@ class CacheActivity : VMBaseActivity() 2 -> booksDownload.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) } + 3 -> booksDownload.sortedBy { it.order } else -> booksDownload.sortedByDescending { it.durChapterTime } } @@ -247,8 +249,8 @@ class CacheActivity : VMBaseActivity() val path = ACache.get().getAsString(exportBookPathKey) if (path.isNullOrEmpty()) { selectExportFolder(position) - } else if (AppConfig.enableCustomExport) {// 启用自定义导出 - configExportSection(path, position); + } else if (AppConfig.enableCustomExport && AppConfig.exportType == 1) {// 启用自定义导出 and 导出类型为Epub + configExportSection(path, position) } else { startExport(path, position) } @@ -272,76 +274,63 @@ class CacheActivity : VMBaseActivity() * @since 1.0.0 */ private fun configExportSection(path: String, position: Int) { - if (AppConfig.enableCustomExport) { - val alertBinding = DialogSelectSectionExportBinding.inflate(layoutInflater) - .apply { + val alertBinding = DialogSelectSectionExportBinding.inflate(layoutInflater) + .apply { + cbAllExport.isChecked = true + cbSelectExport.isChecked = false + etEpubSize.isEnabled = false + etInputScope.isEnabled = false + tvAllExport.setOnClickListener { cbAllExport.isChecked = true - cbSelectExport.isChecked = false - etEpubSize.isEnabled = false - etInputScope.isEnabled = false - tvAllExport.setOnClickListener { - cbAllExport.isChecked = true - } - tvSelectExport.setOnClickListener { - cbSelectExport.isChecked = true - } - cbSelectExport.onCheckedChangeListener = { _, isChecked -> - if (isChecked) { - etEpubSize.isEnabled = true - etInputScope.isEnabled = true - cbAllExport.isChecked = false - } - } - cbAllExport.onCheckedChangeListener = { _, isChecked -> - if (isChecked) { - etEpubSize.isEnabled = false - etInputScope.isEnabled = false - cbSelectExport.isChecked = false - } - } - - etInputScope.onFocusChangeListener = - View.OnFocusChangeListener { v, hasFocus -> - if (hasFocus) { - etInputScope.hint = "例如:1-5,8,10-18" - } else { - etInputScope.hint = "" - } - } - etInputScope.doOnTextChanged { text, start, before, count -> - var a: Int = 1 - a = 2 - } - } - val alertDialog = alert(titleResource = R.string.select_section_export) { - customView { alertBinding.root } - positiveButton("确认") - //okButton { -// alertBinding.apply { -// it.dismiss() -// if (cbAllExport.isChecked) { -// // export all sections of the book. -// startExport(path, position) -// } else if (cbSelectExport.isChecked) { -// -// } -// } - //} - cancelButton() + tvSelectExport.setOnClickListener { + cbSelectExport.isChecked = true + } + cbSelectExport.onCheckedChangeListener = { _, isChecked -> + if (isChecked) { + etEpubSize.isEnabled = true + etInputScope.isEnabled = true + cbAllExport.isChecked = false + } + } + cbAllExport.onCheckedChangeListener = { _, isChecked -> + if (isChecked) { + etEpubSize.isEnabled = false + etInputScope.isEnabled = false + cbSelectExport.isChecked = false + } + } + + etInputScope.onFocusChangeListener = + View.OnFocusChangeListener { _, hasFocus -> + if (hasFocus) { + etInputScope.hint = "例如:1-5,8,10-18" + } else { + etInputScope.hint = "" + } + } } - alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { - alertBinding.apply { - val text = etInputScope.text - if (!verificationField(text.toString())) { - etInputScope.error = "请输入正确的范围" - } else { - etInputScope.error = null - val toInt = etEpubSize.text.toString().toInt() - startExport(path, position, toInt, text.toString()) - alertDialog.hide(); - } + val alertDialog = alert(titleResource = R.string.select_section_export) { + customView { alertBinding.root } + positiveButton("确认") + cancelButton() + } + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { + alertBinding.apply { + if (cbAllExport.isChecked) { + startExport(path, position) + alertDialog.hide() + return@apply } + val text = etInputScope.text + if (!verificationField(text.toString())) { + etInputScope.error = "请输入正确的范围" + return@apply + } + etInputScope.error = null + val toInt = etEpubSize.text.toString().toInt() + startExport(path, position, toInt, text.toString()) + alertDialog.hide() } @@ -376,7 +365,7 @@ class CacheActivity : VMBaseActivity() if (exportPosition >= 0) { adapter.getItem(exportPosition)?.let { book -> when (AppConfig.exportType) { - 1 -> viewModel.exportEPUB(path, book, size, scope) + 1 -> viewModel.exportEPUBs(path, book, size, scope) // 目前仅支持 epub //else -> viewModel.export(path, book) } 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 8fbd74c7d..c4d378964 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 @@ -268,8 +268,8 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { result.add(s.toInt() - 1) continue } - val left = v[0].toInt(); - val right = v[1].toInt(); + val left = v[0].toInt() + val right = v[1].toInt() if (left > right) return IntArray(0) for (i in left..right) @@ -290,7 +290,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { * @author Discut * @since 2023/5/22 */ - fun exportEPUB(path: String, book: Book, size: Int = 1, scope: String) { + fun exportEPUBs(path: String, book: Book, size: Int = 1, scope: String) { if (exportProgress.contains(book.bookUrl)) return CustomExporter(this).let { it.scope = paresScope(scope) @@ -627,13 +627,23 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { //////end of EPUB + //////start of custom exporter /** * 自定义Exporter + * + * @since 2023/5/23 */ class CustomExporter(private val context: CacheViewModel) { - var scope: IntArray = IntArray(0); - var size: Int = 1; + var scope: IntArray = IntArray(0) + var size: Int = 1 + /** + * 导出Epub + * + * from [io.legado.app.ui.book.cache.CacheViewModel.exportEPUB] + * @param path 导出的路径 + * @param book 书籍 + */ fun exportEPUB( path: String, book: Book @@ -653,7 +663,6 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { val doc = DocumentFile.fromTreeUri(context.context, uri) ?: throw NoStackTraceException("获取导出文档失败") book.totalChapterNum - // TODO exportEpub(doc, book) } else { context.exportEpub(File(path).createFolderIfNotExist(), book) @@ -683,7 +692,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { epubList.forEachIndexed { index, epubBook -> //设置正文 this.setEpubContent(contentModel, book, epubBook, index) - save2Drive(book.name+index+".epub", epubBook, doc) + save2Drive(book.name + index + ".epub", epubBook, doc) } } @@ -703,17 +712,18 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { //正文 val useReplace = AppConfig.exportUseReplace && book.getUseReplaceRule() val contentProcessor = ContentProcessor.get(book.name, book.origin) - var chapterList: MutableList = ArrayList(); + var chapterList: MutableList = ArrayList() appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter -> if (scope.indexOf(index) >= 0) { chapterList.add(chapter) } } + val totalChapterNum = book.totalChapterNum / scope.size chapterList = chapterList.subList(epubBookIndex * size, (epubBookIndex + 1) * size) chapterList.forEachIndexed { index, chapter -> coroutineContext.ensureActive() context.upAdapterLiveData.postValue(book.bookUrl) - context.exportProgress[book.bookUrl] = index + context.exportProgress[book.bookUrl] = totalChapterNum * (epubBookIndex * size + index) BookHelp.getContent(book, chapter).let { content -> var content1 = context.fixPic( epubBook, @@ -758,7 +768,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { private fun createEpubs(doc: DocumentFile, book: Book): Pair> { val paresNumOfEpub = paresNumOfEpub(scope.size, size) val result: MutableList = ArrayList(paresNumOfEpub) - var contentModel = ""; + var contentModel = "" for (i in 1..paresNumOfEpub) { val filename = book.getExportFileName("epub") DocumentUtils.delete(doc, filename) @@ -798,11 +808,11 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { */ private fun paresNumOfEpub(total: Int, size: Int): Int { val i = total % size - var result = total / size; + var result = total / size if (i > 0) { - result++; + result++ } - return result; + return result } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CustomExportSectionDialog.kt b/app/src/main/java/io/legado/app/ui/book/cache/CustomExportSectionDialog.kt deleted file mode 100644 index b26f54b43..000000000 --- a/app/src/main/java/io/legado/app/ui/book/cache/CustomExportSectionDialog.kt +++ /dev/null @@ -1,16 +0,0 @@ -package io.legado.app.ui.book.cache - -import android.os.Bundle -import android.view.View -import io.legado.app.R -import io.legado.app.base.BaseDialogFragment -import io.legado.app.databinding.DialogSelectSectionExportBinding -import io.legado.app.utils.viewbindingdelegate.viewBinding - -class CustomExportSectionDialog() : BaseDialogFragment(R.layout.dialog_select_section_export) { - - private val binding by viewBinding(DialogSelectSectionExportBinding::bind) - override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { - TODO("Not yet implemented") - } -} \ No newline at end of file diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 9020237a3..8435a625e 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -31,7 +31,7 @@ 删除所有 替换 替换净化 - 自定义导出章节 + 自定义Epub导出章节 配置替换净化规则 暂无 启用 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 18df6b071..d4227405a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,7 +30,7 @@ Edit Delete Delete all - Custom export chapter + Custom export chapter of epub Replace Replacement Configure replacement rules