From 919bce443074dc011175e563af1db44dde4902a6 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 17 Sep 2021 22:52:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/lib/dialogs/SelectItem.kt | 10 +++------ .../legado/app/ui/book/cache/CacheActivity.kt | 2 +- .../ui/book/read/config/SpeakEngineDialog.kt | 22 +++++++++++++------ .../book/read/config/SpeakEngineViewModel.kt | 3 +++ .../app/ui/document/HandleFileActivity.kt | 15 +++++++------ .../app/ui/document/HandleFileContract.kt | 5 +++-- .../legado/app/ui/rss/read/ReadRssActivity.kt | 2 +- .../io/legado/app/utils/IntentExtensions.kt | 21 ++++++++++++++++++ 8 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/IntentExtensions.kt diff --git a/app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt b/app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt index dcee51afa..71a8f613a 100644 --- a/app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt +++ b/app/src/main/java/io/legado/app/lib/dialogs/SelectItem.kt @@ -1,14 +1,10 @@ package io.legado.app.lib.dialogs -import android.os.Parcelable -import kotlinx.parcelize.Parcelize - @Suppress("unused") -@Parcelize -data class SelectItem( +data class SelectItem( val title: String, - val id: Int -) : Parcelable { + val value: T +) { override fun toString(): String { return title 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 04b46b771..3b77a8e94 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 @@ -244,7 +244,7 @@ class CacheActivity : VMBaseActivity() } private fun selectExportFolder() { - val default = arrayListOf() + val default = arrayListOf>() val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey) if (!path.isNullOrEmpty()) { default.add(SelectItem(path, -1)) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt index 5739eb98c..05b4504ea 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt @@ -22,6 +22,7 @@ import io.legado.app.databinding.DialogHttpTtsEditBinding import io.legado.app.databinding.DialogRecyclerViewBinding import io.legado.app.databinding.ItemHttpTtsBinding import io.legado.app.help.DirectLinkUpload +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryColor @@ -95,8 +96,7 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener tvFooterLeft.setText(R.string.system_tts) tvFooterLeft.visible() tvFooterLeft.setOnClickListener { - removePref(PreferKey.speakEngine) - dismissAllowingStateLoss() + selectSysTts() } tvOk.visible() tvOk.setOnClickListener { @@ -144,6 +144,14 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener return true } + private fun selectSysTts() { + val ttsItems = viewModel.tts.engines.map { + SelectItem(it.label, 0) + } + removePref(PreferKey.speakEngine) + dismissAllowingStateLoss() + } + private fun importAlert() { val aCache = ACache.get(requireContext(), cacheDir = false) val cacheUrls: MutableList = aCache @@ -218,18 +226,18 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener } override fun registerListener(holder: ItemViewHolder, binding: ItemHttpTtsBinding) { - binding.apply { + binding.run { cbName.setOnClickListener { - getItem(holder.layoutPosition)?.let { httpTTS -> + getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS -> engineId = httpTTS.id - notifyItemRangeChanged(0, itemCount) + notifyItemRangeChanged(getHeaderCount(), itemCount) } } ivEdit.setOnClickListener { - editHttpTTS(getItem(holder.layoutPosition)) + editHttpTTS(getItemByLayoutPosition(holder.layoutPosition)) } ivMenuDelete.setOnClickListener { - getItem(holder.layoutPosition)?.let { httpTTS -> + getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS -> appDb.httpTTSDao.delete(httpTTS) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt index 9ad6d4690..04571491f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt @@ -2,6 +2,7 @@ package io.legado.app.ui.book.read.config import android.app.Application import android.net.Uri +import android.speech.tts.TextToSpeech import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.HttpTTS @@ -13,6 +14,8 @@ import io.legado.app.utils.* class SpeakEngineViewModel(application: Application) : BaseViewModel(application) { + val tts = TextToSpeech(context, null) + fun importDefault() { execute { DefaultData.importDefaultHttpTTS() diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt index 9c77c9f50..7f5f45ea9 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt @@ -16,6 +16,7 @@ import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.PermissionsCompat +import io.legado.app.utils.getJsonArray import io.legado.app.utils.isContentScheme import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -56,7 +57,7 @@ class HandleFileActivity : finish() } val allowExtensions = intent.getStringArrayExtra("allowExtensions") - val selectList = when (mode) { + val selectList: ArrayList> = when (mode) { HandleFileContract.DIR -> getDirActions() HandleFileContract.FILE -> getFileActions() HandleFileContract.EXPORT -> arrayListOf( @@ -66,7 +67,7 @@ class HandleFileActivity : } else -> arrayListOf() } - intent.getParcelableArrayListExtra("otherActions")?.let { + intent.getJsonArray>("otherActions")?.let { selectList.addAll(it) } val title = intent.getStringExtra("title") ?: let { @@ -78,7 +79,7 @@ class HandleFileActivity : } alert(title) { items(selectList) { _, item, _ -> - when (item.id) { + when (item.value) { HandleFileContract.DIR -> selectDocTree.launch(null) HandleFileContract.FILE -> selectDoc.launch(typesOfExtensions(allowExtensions)) 10 -> checkPermissions { @@ -104,9 +105,9 @@ class HandleFileActivity : else -> { val path = item.title val uri = if (path.isContentScheme()) { - Uri.fromFile(File(path)) - } else { Uri.parse(path) + } else { + Uri.fromFile(File(path)) } onResult(Intent().setData(uri)) } @@ -130,7 +131,7 @@ class HandleFileActivity : return null } - private fun getDirActions(): ArrayList { + private fun getDirActions(): ArrayList> { return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { arrayListOf( SelectItem(getString(R.string.sys_folder_picker), HandleFileContract.DIR), @@ -141,7 +142,7 @@ class HandleFileActivity : } } - private fun getFileActions(): ArrayList { + private fun getFileActions(): ArrayList> { return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { arrayListOf( SelectItem(getString(R.string.sys_file_picker), HandleFileContract.FILE), diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt index 6ac04153d..29a01662b 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileContract.kt @@ -7,6 +7,7 @@ import android.net.Uri import androidx.activity.result.contract.ActivityResultContract import io.legado.app.help.IntentDataHelp import io.legado.app.lib.dialogs.SelectItem +import io.legado.app.utils.putJson @Suppress("unused") class HandleFileContract : @@ -28,7 +29,7 @@ class HandleFileContract : intent.putExtra("mode", it.mode) intent.putExtra("title", it.title) intent.putExtra("allowExtensions", it.allowExtensions) - intent.putExtra("otherActions", it.otherActions) + intent.putJson("otherActions", it.otherActions) it.fileData?.let { fileData -> intent.putExtra("fileName", fileData.first) intent.putExtra("fileKey", IntentDataHelp.putData(fileData.second)) @@ -50,7 +51,7 @@ class HandleFileContract : var mode: Int = DIR, var title: String? = null, var allowExtensions: Array = arrayOf(), - var otherActions: ArrayList? = null, + var otherActions: ArrayList>? = null, var fileData: Triple? = null ) diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 2666e07ed..788e77aa3 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -174,7 +174,7 @@ class ReadRssActivity : VMBaseActivity } private fun selectSaveFolder() { - val default = arrayListOf() + val default = arrayListOf>() val path = ACache.get(this@ReadRssActivity).getAsString(imagePathKey) if (!path.isNullOrEmpty()) { default.add(SelectItem(path, -1)) diff --git a/app/src/main/java/io/legado/app/utils/IntentExtensions.kt b/app/src/main/java/io/legado/app/utils/IntentExtensions.kt new file mode 100644 index 000000000..f64dcf7b4 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/IntentExtensions.kt @@ -0,0 +1,21 @@ +@file:Suppress("unused") + +package io.legado.app.utils + +import android.content.Intent + +fun Intent.putJson(key: String, any: Any?) { + any?.let { + putExtra(key, GSON.toJson(any)) + } +} + +inline fun Intent.getJsonObject(key: String): T? { + val value = getStringExtra(key) + return GSON.fromJsonObject(value) +} + +inline fun Intent.getJsonArray(key: String): List? { + val value = getStringExtra(key) + return GSON.fromJsonArray(value) +} \ No newline at end of file