添加导出epub

pull/931/head
gedoor 4 years ago
parent e8a6714b16
commit c37e00f2fd
  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. 15
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  4. 10
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  5. 5
      app/src/main/res/menu/book_cache.xml
  6. 1
      app/src/main/res/values-zh-rHK/strings.xml
  7. 1
      app/src/main/res/values-zh-rTW/strings.xml
  8. 1
      app/src/main/res/values-zh/strings.xml
  9. 1
      app/src/main/res/values/strings.xml

@ -40,6 +40,7 @@ object PreferKey {
const val webDavPassword = "web_dav_password" const val webDavPassword = "web_dav_password"
const val webDavCreateDir = "webDavCreateDir" const val webDavCreateDir = "webDavCreateDir"
const val exportToWebDav = "webDavCacheBackup" const val exportToWebDav = "webDavCacheBackup"
const val exportType = "exportType"
const val changeSourceLoadToc = "changeSourceLoadToc" const val changeSourceLoadToc = "changeSourceLoadToc"
const val changeSourceLoadInfo = "changeSourceLoadInfo" const val changeSourceLoadInfo = "changeSourceLoadInfo"
const val chineseConverterType = "chineseConverterType" const val chineseConverterType = "chineseConverterType"

@ -161,6 +161,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
appCtx.putPrefBoolean(PreferKey.exportToWebDav, value) appCtx.putPrefBoolean(PreferKey.exportToWebDav, value)
} }
var exportType: Int
get() = appCtx.getPrefInt(PreferKey.exportType)
set(value) {
appCtx.putPrefInt(PreferKey.exportType, value)
}
val autoChangeSource: Boolean val autoChangeSource: Boolean
get() = appCtx.getPrefBoolean(PreferKey.autoChangeSource, true) get() = appCtx.getPrefBoolean(PreferKey.autoChangeSource, true)

@ -23,6 +23,7 @@ import io.legado.app.databinding.DialogEditTextBinding
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.selector
import io.legado.app.service.help.CacheBook import io.legado.app.service.help.CacheBook
import io.legado.app.ui.document.FilePicker import io.legado.app.ui.document.FilePicker
import io.legado.app.ui.document.FilePickerParam import io.legado.app.ui.document.FilePickerParam
@ -122,6 +123,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
R.id.menu_enable_replace -> AppConfig.exportUseReplace = !item.isChecked R.id.menu_enable_replace -> AppConfig.exportUseReplace = !item.isChecked
R.id.menu_export_web_dav -> AppConfig.exportToWebDav = !item.isChecked R.id.menu_export_web_dav -> AppConfig.exportToWebDav = !item.isChecked
R.id.menu_export_folder -> export(-1) R.id.menu_export_folder -> export(-1)
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 ->
TextListDialog.show(supportFragmentManager, getString(R.string.log), CacheBook.logs) TextListDialog.show(supportFragmentManager, getString(R.string.log), CacheBook.logs)
@ -239,9 +241,20 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
adapter.getItem(exportPosition)?.let { book -> adapter.getItem(exportPosition)?.let { book ->
Snackbar.make(binding.titleBar, R.string.exporting, Snackbar.LENGTH_INDEFINITE) Snackbar.make(binding.titleBar, R.string.exporting, Snackbar.LENGTH_INDEFINITE)
.show() .show()
viewModel.exportEPUB(path, book) { when (AppConfig.exportType) {
1 -> viewModel.exportEPUB(path, book) {
binding.titleBar.snackbar(it) binding.titleBar.snackbar(it)
} }
else -> viewModel.export(path, book) {
binding.titleBar.snackbar(it)
}
}
}
}
private fun showExportTypeConfig() {
selector(R.string.export_type, arrayListOf("txt", "epub")) { _, i ->
AppConfig.exportType = i
} }
} }

@ -106,7 +106,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
} }
} }
private suspend fun getAllContents(book: Book, append: (text: String) -> Unit) { private fun getAllContents(book: Book, append: (text: String) -> Unit) {
val useReplace = AppConfig.exportUseReplace val useReplace = AppConfig.exportUseReplace
val contentProcessor = ContentProcessor(book.name, book.origin) val contentProcessor = ContentProcessor(book.name, book.origin)
append("${book.name}\n${context.getString(R.string.author_show, book.author)}") append("${book.name}\n${context.getString(R.string.author_show, book.author)}")
@ -159,13 +159,13 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
} }
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
private suspend fun exportEpub(doc: DocumentFile, book: Book) { private fun exportEpub(doc: DocumentFile, book: Book) {
val filename = "${book.name} by ${book.author}.epub" val filename = "${book.name} by ${book.author}.epub"
DocumentUtils.delete(doc, filename) DocumentUtils.delete(doc, filename)
val epubBook = EpubBook() val epubBook = EpubBook()
epubBook.version = "2.0" epubBook.version = "2.0"
//set metadata //set metadata
setEpubMetadata(book,epubBook) setEpubMetadata(book, epubBook)
//set cover //set cover
setCover(book, epubBook) setCover(book, epubBook)
@ -187,12 +187,12 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
} }
} }
private suspend fun exportEpub(file: File, book: Book) { private fun exportEpub(file: File, book: Book) {
val filename = "${book.name} by ${book.author}.epub" val filename = "${book.name} by ${book.author}.epub"
val epubBook = EpubBook() val epubBook = EpubBook()
epubBook.version = "2.0" epubBook.version = "2.0"
//set metadata //set metadata
setEpubMetadata(book,epubBook) setEpubMetadata(book, epubBook)
//set cover //set cover
setCover(book, epubBook) setCover(book, epubBook)

@ -37,6 +37,11 @@
android:title="@string/export_folder" android:title="@string/export_folder"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/menu_export_type"
android:title="@string/export_type"
app:showAsAction="never" />
<item <item
android:id="@+id/menu_export_charset" android:id="@+id/menu_export_charset"
android:title="@string/export_charset" android:title="@string/export_charset"

@ -811,5 +811,6 @@
<string name="use_zh_layout">使用自定义中文分行</string> <string name="use_zh_layout">使用自定义中文分行</string>
<string name="image_style">图片样式</string> <string name="image_style">图片样式</string>
<string name="system_tts">系统TTS</string> <string name="system_tts">系统TTS</string>
<string name="export_type">导出格式</string>
</resources> </resources>

@ -813,5 +813,6 @@
<string name="debug">调试</string> <string name="debug">调试</string>
<string name="image_style">图片样式</string> <string name="image_style">图片样式</string>
<string name="system_tts">系统TTS</string> <string name="system_tts">系统TTS</string>
<string name="export_type">导出格式</string>
</resources> </resources>

@ -815,5 +815,6 @@
<string name="use_zh_layout">使用自定义中文分行</string> <string name="use_zh_layout">使用自定义中文分行</string>
<string name="image_style">图片样式</string> <string name="image_style">图片样式</string>
<string name="system_tts">系统TTS</string> <string name="system_tts">系统TTS</string>
<string name="export_type">导出格式</string>
</resources> </resources>

@ -818,5 +818,6 @@
<string name="use_zh_layout">使用自定义中文分行</string> <string name="use_zh_layout">使用自定义中文分行</string>
<string name="image_style">Image style</string> <string name="image_style">Image style</string>
<string name="system_tts">System TTS</string> <string name="system_tts">System TTS</string>
<string name="export_type">导出格式</string>
</resources> </resources>

Loading…
Cancel
Save