添加自定义导出文件名

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 bookshelfLayout = "bookshelfLayout"
const val bookshelfSort = "bookshelfSort"
const val bookExportFileName = "bookExportFileName"
const val recordLog = "recordLog"
const val processText = "process_text"
const val cleanCache = "cleanCache"

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

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

@ -127,6 +127,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
exportPosition = -1
selectExportFolder()
}
R.id.menu_export_file_name -> alertExportFileName()
R.id.menu_export_type -> showExportTypeConfig()
R.id.menu_export_charset -> showCharsetConfig()
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() {
selector(R.string.export_type, arrayListOf("txt", "epub")) { _, i ->
AppConfig.exportType = i

@ -10,6 +10,7 @@ import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import io.legado.app.R
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppPattern
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
@ -26,10 +27,21 @@ import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.nio.charset.Charset
import javax.script.SimpleBindings
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) {
execute {
@ -50,7 +62,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
@Suppress("BlockingMethodInNonBlockingContext")
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.createFileIfNotExist(doc, filename)?.let { bookDoc ->
val stringBuilder = StringBuilder()
@ -80,7 +92,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
}
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 bookFile = FileUtils.createFileWithReplace(bookPath)
val stringBuilder = StringBuilder()
@ -161,7 +173,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
@Suppress("BlockingMethodInNonBlockingContext")
private fun exportEpub(doc: DocumentFile, book: Book) {
val filename = "${book.name} by ${book.author}.epub"
val filename = "${getExportFileName(book)}.epub"
DocumentUtils.delete(doc, filename)
val epubBook = EpubBook()
epubBook.version = "2.0"
@ -185,7 +197,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
private fun exportEpub(file: File, book: Book) {
val filename = "${book.name} by ${book.author}.epub"
val filename = "${getExportFileName(book)}.epub"
val epubBook = EpubBook()
epubBook.version = "2.0"
//set metadata

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

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

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

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

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

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

@ -808,7 +808,7 @@
<string name="background_image">Background image</string>
<string name="copy_book_url">Copy book 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_to_web_dav">Export to WebDav</string>
<string name="reverse_content">Reverse content</string>
@ -842,5 +842,6 @@
<string name="show_discovery">Show</string>
<string name="style">Style</string>
<string name="group_style">Group style</string>
<string name="export_file_name">Export file name</string>
</resources>

Loading…
Cancel
Save