feat(Cache/Export): 新增 开启自定义导出 菜单与对话框

This commit is contained in:
Discut
2023-05-22 21:00:37 +08:00
parent ab46460f93
commit d8c5d79d2d
8 changed files with 268 additions and 0 deletions
@@ -54,6 +54,7 @@ object PreferKey {
const val webDavAccount = "web_dav_account"
const val webDavPassword = "web_dav_password"
const val webDavDir = "webDavDir"
const val enableCustomExport = "enableCustomExport"
const val exportToWebDav = "webDavCacheBackup"
const val exportNoChapterName = "exportNoChapterName"
const val exportType = "exportType"
@@ -268,6 +268,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
set(value) {
appCtx.putPrefBoolean(PreferKey.exportNoChapterName, value)
}
var enableCustomExport: Boolean
get() = appCtx.getPrefBoolean(PreferKey.enableCustomExport)
set(value) {
appCtx.putPrefBoolean(PreferKey.enableCustomExport, value)
}
var exportType: Int
get() = appCtx.getPrefInt(PreferKey.exportType)
set(value) {
@@ -4,7 +4,9 @@ import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
@@ -17,6 +19,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookGroup
import io.legado.app.databinding.ActivityCacheBookBinding
import io.legado.app.databinding.DialogEditTextBinding
import io.legado.app.databinding.DialogSelectSectionExportBinding
import io.legado.app.help.book.isAudio
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.dialogs.SelectItem
@@ -32,7 +35,11 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import androidx.core.widget.doOnTextChanged
/**
* cache/download 缓存界面
*/
class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>(),
CacheAdapter.CallBack {
@@ -88,6 +95,8 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
menu.findItem(R.id.menu_enable_replace)?.isChecked = AppConfig.exportUseReplace
// 菜单打开时读取状态[enableCustomExport]
menu.findItem(R.id.menu_enable_custom_export)?.isChecked = AppConfig.enableCustomExport
menu.findItem(R.id.menu_export_no_chapter_name)?.isChecked = AppConfig.exportNoChapterName
menu.findItem(R.id.menu_export_web_dav)?.isChecked = AppConfig.exportToWebDav
menu.findItem(R.id.menu_export_pics_file)?.isChecked = AppConfig.exportPictureFile
@@ -108,6 +117,9 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
}
}
/**
* 菜单按下回调
*/
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_download -> {
@@ -126,6 +138,8 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
}
R.id.menu_export_all -> exportAll()
R.id.menu_enable_replace -> AppConfig.exportUseReplace = !item.isChecked
// 更改菜单状态[enableCustomExport]
R.id.menu_enable_custom_export -> AppConfig.enableCustomExport = !item.isChecked
R.id.menu_export_no_chapter_name -> AppConfig.exportNoChapterName = !item.isChecked
R.id.menu_export_web_dav -> AppConfig.exportToWebDav = !item.isChecked
R.id.menu_export_pics_file -> AppConfig.exportPictureFile = !item.isChecked
@@ -233,6 +247,8 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
val path = ACache.get().getAsString(exportBookPathKey)
if (path.isNullOrEmpty()) {
selectExportFolder(position)
} else if (AppConfig.enableCustomExport) {// 启用自定义导出
configExportSection(path, position);
} else {
startExport(path, position)
}
@@ -247,6 +263,102 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
}
}
/**
* 配置自定义导出对话框
*
* @param path 导出路径
* @param position book位置
* @author Discut
* @since 1.0.0
*/
private fun configExportSection(path: String, position: Int) {
if (AppConfig.enableCustomExport) {
val alertBinding = DialogSelectSectionExportBinding.inflate(layoutInflater)
.apply {
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()
}
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())
}
}
}
}
}
/**
* 验证 输入的范围 是否正确
*
* @since 1.0.0
* @author Discut
* @param text 输入的范围 字符串
* @return 是否正确
*/
private fun verificationField(text: String): Boolean {
return text.matches(Regex("\\d+(-\\d+)?(,\\d+(-\\d+)?)*"))
}
private fun selectExportFolder(exportPosition: Int) {
val default = arrayListOf<SelectItem<Int>>()
val path = ACache.get().getAsString(exportBookPathKey)
@@ -259,6 +371,18 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
}
}
private fun startExport(path: String, exportPosition: Int, size: Int, scope: String) {
if (exportPosition >= 0) {
adapter.getItem(exportPosition)?.let { book ->
when (AppConfig.exportType) {
1 -> viewModel.exportEPUB(path, book, size, scope)
// 目前仅支持 epub
//else -> viewModel.export(path, book)
}
}
}
}
private fun startExport(path: String, exportPosition: Int) {
if (exportPosition == -10) {
if (adapter.getItems().isNotEmpty()) {
@@ -0,0 +1,16 @@
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")
}
}