添加自定义导出文件名

pull/1023/head
gedoor 4 years ago
parent a8d6b38ba0
commit c0bcb32db5
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 6
      app/src/main/java/io/legado/app/help/AppConfig.kt
  3. 4
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  4. 14
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  5. 20
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  6. 5
      app/src/main/res/menu/book_cache.xml
  7. 1
      app/src/main/res/values-ja-rJP/strings.xml
  8. 1
      app/src/main/res/values-pt-rBR/strings.xml
  9. 1
      app/src/main/res/values-zh-rHK/strings.xml
  10. 1
      app/src/main/res/values-zh-rTW/strings.xml
  11. 1
      app/src/main/res/values-zh/strings.xml
  12. 3
      app/src/main/res/values/strings.xml

@ -28,6 +28,7 @@ object PreferKey {
const val showRss = "showRss" const val showRss = "showRss"
const val bookshelfLayout = "bookshelfLayout" const val bookshelfLayout = "bookshelfLayout"
const val bookshelfSort = "bookshelfSort" const val bookshelfSort = "bookshelfSort"
const val bookExportFileName = "bookExportFileName"
const val recordLog = "recordLog" const val recordLog = "recordLog"
const val processText = "process_text" const val processText = "process_text"
const val cleanCache = "cleanCache" const val cleanCache = "cleanCache"

@ -96,6 +96,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
appCtx.putPrefInt(PreferKey.bookGroupStyle, value) appCtx.putPrefInt(PreferKey.bookGroupStyle, value)
} }
var bookExportFileName: String?
get() = appCtx.getPrefString(PreferKey.bookExportFileName)
set(value) {
appCtx.putPrefString(PreferKey.bookExportFileName, value)
}
var backupPath: String? var backupPath: String?
get() = appCtx.getPrefString(PreferKey.backupPath) get() = appCtx.getPrefString(PreferKey.backupPath)
set(value) { set(value) {

@ -650,9 +650,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions {
bindings["title"] = chapter?.title bindings["title"] = chapter?.title
bindings["src"] = content bindings["src"] = content
bindings["nextChapterUrl"] = nextChapterUrl bindings["nextChapterUrl"] = nextChapterUrl
return runBlocking { return SCRIPT_ENGINE.eval(jsStr, bindings)
SCRIPT_ENGINE.eval(jsStr, bindings)
}
} }
/** /**

@ -127,6 +127,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
exportPosition = -1 exportPosition = -1
selectExportFolder() selectExportFolder()
} }
R.id.menu_export_file_name -> alertExportFileName()
R.id.menu_export_type -> showExportTypeConfig() R.id.menu_export_type -> showExportTypeConfig()
R.id.menu_export_charset -> showCharsetConfig() R.id.menu_export_charset -> showCharsetConfig()
R.id.menu_log -> R.id.menu_log ->
@ -294,6 +295,19 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
} }
} }
private fun alertExportFileName() {
alert(R.string.export_file_name) {
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editView.setText(AppConfig.bookExportFileName)
}
customView { alertBinding.root }
okButton {
AppConfig.bookExportFileName = alertBinding.editView.text?.toString()
}
cancelButton()
}.show()
}
private fun showExportTypeConfig() { private fun showExportTypeConfig() {
selector(R.string.export_type, arrayListOf("txt", "epub")) { _, i -> selector(R.string.export_type, arrayListOf("txt", "epub")) { _, i ->
AppConfig.exportType = i AppConfig.exportType = i

@ -10,6 +10,7 @@ import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition import com.bumptech.glide.request.transition.Transition
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
@ -26,10 +27,21 @@ import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.nio.charset.Charset import java.nio.charset.Charset
import javax.script.SimpleBindings
class CacheViewModel(application: Application) : BaseViewModel(application) { class CacheViewModel(application: Application) : BaseViewModel(application) {
fun getExportFileName(book: Book): String {
val jsStr = AppConfig.bookExportFileName
if (jsStr.isNullOrBlank()) {
return "${book.name} by ${book.author}"
}
val bindings = SimpleBindings()
bindings["name"] = book.name
bindings["author"] = book.author
return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings).toString()
}
fun export(path: String, book: Book, finally: (msg: String) -> Unit) { fun export(path: String, book: Book, finally: (msg: String) -> Unit) {
execute { execute {
@ -50,7 +62,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
private suspend fun export(doc: DocumentFile, book: Book) { private suspend fun export(doc: DocumentFile, book: Book) {
val filename = "${book.name} by ${book.author}.txt" val filename = "${getExportFileName(book)}.txt"
DocumentUtils.delete(doc, filename) DocumentUtils.delete(doc, filename)
DocumentUtils.createFileIfNotExist(doc, filename)?.let { bookDoc -> DocumentUtils.createFileIfNotExist(doc, filename)?.let { bookDoc ->
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
@ -80,7 +92,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
} }
private suspend fun export(file: File, book: Book) { private suspend fun export(file: File, book: Book) {
val filename = "${book.name} by ${book.author}.txt" val filename = "${getExportFileName(book)}.txt"
val bookPath = FileUtils.getPath(file, filename) val bookPath = FileUtils.getPath(file, filename)
val bookFile = FileUtils.createFileWithReplace(bookPath) val bookFile = FileUtils.createFileWithReplace(bookPath)
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
@ -161,7 +173,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
private fun exportEpub(doc: DocumentFile, book: Book) { private fun exportEpub(doc: DocumentFile, book: Book) {
val filename = "${book.name} by ${book.author}.epub" val filename = "${getExportFileName(book)}.epub"
DocumentUtils.delete(doc, filename) DocumentUtils.delete(doc, filename)
val epubBook = EpubBook() val epubBook = EpubBook()
epubBook.version = "2.0" epubBook.version = "2.0"
@ -185,7 +197,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
private fun exportEpub(file: File, book: Book) { private fun exportEpub(file: File, book: Book) {
val filename = "${book.name} by ${book.author}.epub" val filename = "${getExportFileName(book)}.epub"
val epubBook = EpubBook() val epubBook = EpubBook()
epubBook.version = "2.0" epubBook.version = "2.0"
//set metadata //set metadata

@ -42,6 +42,11 @@
android:title="@string/export_folder" android:title="@string/export_folder"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/menu_export_file_name"
android:title="@string/export_file_name"
app:showAsAction="never" />
<item <item
android:id="@+id/menu_export_type" android:id="@+id/menu_export_type"
android:title="@string/export_type" android:title="@string/export_type"

@ -842,4 +842,5 @@
<string name="show_discovery">显示发现</string> <string name="show_discovery">显示发现</string>
<string name="style">样式</string> <string name="style">样式</string>
<string name="group_style">分组样式</string> <string name="group_style">分组样式</string>
<string name="export_file_name">导出文件名</string>
</resources> </resources>

@ -842,4 +842,5 @@
<string name="show_discovery">Mostrar</string> <string name="show_discovery">Mostrar</string>
<string name="style">样式</string> <string name="style">样式</string>
<string name="group_style">分组样式</string> <string name="group_style">分组样式</string>
<string name="export_file_name">导出文件名</string>
</resources> </resources>

@ -840,5 +840,6 @@
<string name="show_discovery">显示发现</string> <string name="show_discovery">显示发现</string>
<string name="style">样式</string> <string name="style">样式</string>
<string name="group_style">分组样式</string> <string name="group_style">分组样式</string>
<string name="export_file_name">导出文件名</string>
</resources> </resources>

@ -841,5 +841,6 @@
<string name="show_discovery">显示发现</string> <string name="show_discovery">显示发现</string>
<string name="style">样式</string> <string name="style">样式</string>
<string name="group_style">分组样式</string> <string name="group_style">分组样式</string>
<string name="export_file_name">导出文件名</string>
</resources> </resources>

@ -841,5 +841,6 @@
<string name="show_discovery">显示发现</string> <string name="show_discovery">显示发现</string>
<string name="style">样式</string> <string name="style">样式</string>
<string name="group_style">分组样式</string> <string name="group_style">分组样式</string>
<string name="export_file_name">导出文件名</string>
</resources> </resources>

@ -808,7 +808,7 @@
<string name="background_image">Background image</string> <string name="background_image">Background image</string>
<string name="copy_book_url">Copy book URL</string> <string name="copy_book_url">Copy book URL</string>
<string name="copy_toc_url">Copy chapters URL</string> <string name="copy_toc_url">Copy chapters URL</string>
<string name="export_folder">Export to a folder</string> <string name="export_folder">Export folder</string>
<string name="export_charset">Exported text coding</string> <string name="export_charset">Exported text coding</string>
<string name="export_to_web_dav">Export to WebDav</string> <string name="export_to_web_dav">Export to WebDav</string>
<string name="reverse_content">Reverse content</string> <string name="reverse_content">Reverse content</string>
@ -842,5 +842,6 @@
<string name="show_discovery">Show</string> <string name="show_discovery">Show</string>
<string name="style">Style</string> <string name="style">Style</string>
<string name="group_style">Group style</string> <string name="group_style">Group style</string>
<string name="export_file_name">Export file name</string>
</resources> </resources>

Loading…
Cancel
Save