From eb47b3d6c1156c6f918a1f4be3329cd19373b34a Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 18 Sep 2021 09:51:15 +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 --- .../java/io/legado/app/constant/PreferKey.kt | 3 +-- .../java/io/legado/app/data/dao/HttpTTSDao.kt | 3 +++ .../main/java/io/legado/app/help/AppConfig.kt | 6 +++++ .../java/io/legado/app/model/ReadAloud.kt | 20 +++++++++------ .../book/read/config/ReadAloudConfigDialog.kt | 25 +++++++++++-------- .../ui/book/read/config/SpeakEngineDialog.kt | 16 ++++++------ 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 6a1e5f876..3f3bc30fd 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -19,9 +19,8 @@ object PreferKey { const val clickActionBR = "clickActionBottomRight" const val hideNavigationBar = "hideNavigationBar" const val precisionSearch = "precisionSearch" - const val speakEngine = "speakEngine" const val readAloudByPage = "readAloudByPage" - const val ttsEngine = "ttsEngine" + const val ttsEngine = "appTtsEngine" const val ttsSpeechRate = "ttsSpeechRate" const val prevKeys = "prevKeyCodes" const val nextKeys = "nextKeyCodes" diff --git a/app/src/main/java/io/legado/app/data/dao/HttpTTSDao.kt b/app/src/main/java/io/legado/app/data/dao/HttpTTSDao.kt index 70ff64717..b75fd290a 100644 --- a/app/src/main/java/io/legado/app/data/dao/HttpTTSDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/HttpTTSDao.kt @@ -19,6 +19,9 @@ interface HttpTTSDao { @Query("select * from httpTTS where id = :id") fun get(id: Long): HttpTTS? + @Query("select name from httpTTS where id = :id") + fun getName(id: Long): String? + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg httpTTS: HttpTTS) diff --git a/app/src/main/java/io/legado/app/help/AppConfig.kt b/app/src/main/java/io/legado/app/help/AppConfig.kt index b41980f86..1e1708c36 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -222,6 +222,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { appCtx.putPrefBoolean(PreferKey.changeSourceCheckAuthor, value) } + var ttsEngine: String? + get() = appCtx.getPrefString(PreferKey.ttsEngine) + set(value) { + appCtx.putPrefString(PreferKey.ttsEngine, value) + } + val autoChangeSource: Boolean get() = appCtx.getPrefBoolean(PreferKey.autoChangeSource, true) diff --git a/app/src/main/java/io/legado/app/model/ReadAloud.kt b/app/src/main/java/io/legado/app/model/ReadAloud.kt index a4dafbe4f..65f5ee6eb 100644 --- a/app/src/main/java/io/legado/app/model/ReadAloud.kt +++ b/app/src/main/java/io/legado/app/model/ReadAloud.kt @@ -3,13 +3,13 @@ package io.legado.app.model import android.content.Context import android.content.Intent import io.legado.app.constant.IntentAction -import io.legado.app.constant.PreferKey import io.legado.app.data.appDb import io.legado.app.data.entities.HttpTTS +import io.legado.app.help.AppConfig import io.legado.app.service.BaseReadAloudService import io.legado.app.service.HttpReadAloudService import io.legado.app.service.TTSReadAloudService -import io.legado.app.utils.getPrefLong +import io.legado.app.utils.StringUtils import splitties.init.appCtx object ReadAloud { @@ -17,13 +17,17 @@ object ReadAloud { var httpTTS: HttpTTS? = null private fun getReadAloudClass(): Class<*> { - val spId = appCtx.getPrefLong(PreferKey.speakEngine) - httpTTS = appDb.httpTTSDao.get(spId) - return if (httpTTS != null) { - HttpReadAloudService::class.java - } else { - TTSReadAloudService::class.java + val ttsEngine = AppConfig.ttsEngine + if (ttsEngine.isNullOrBlank()) { + return TTSReadAloudService::class.java } + if (StringUtils.isNumeric(ttsEngine)) { + httpTTS = appDb.httpTTSDao.get(ttsEngine.toLong()) + if (httpTTS != null) { + return HttpReadAloudService::class.java + } + } + return TTSReadAloudService::class.java } fun upReadAloudClass() { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudConfigDialog.kt index 8c1d0b371..db7a661b7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudConfigDialog.kt @@ -14,14 +14,13 @@ import io.legado.app.base.BasePreferenceFragment import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.data.appDb +import io.legado.app.help.AppConfig +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.backgroundColor import io.legado.app.model.ReadAloud import io.legado.app.service.BaseReadAloudService -import io.legado.app.utils.getPrefLong -import io.legado.app.utils.postEvent -import io.legado.app.utils.windowSize -import splitties.init.appCtx +import io.legado.app.utils.* class ReadAloudConfigDialog : DialogFragment() { private val readAloudPreferTag = "readAloudPreferTag" @@ -61,15 +60,20 @@ class ReadAloudConfigDialog : DialogFragment() { private val speakEngineSummary: String get() { - val eid = appCtx.getPrefLong(PreferKey.speakEngine) - val ht = appDb.httpTTSDao.get(eid) - return ht?.name ?: getString(R.string.system_tts) + val ttsEngine = AppConfig.ttsEngine + ?: return getString(R.string.system_tts) + if (StringUtils.isNumeric(ttsEngine)) { + return appDb.httpTTSDao.getName(ttsEngine.toLong()) + ?: getString(R.string.system_tts) + } + return GSON.fromJsonObject>(ttsEngine)?.title + ?: getString(R.string.system_tts) } override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_aloud) upPreferenceSummary( - findPreference(PreferKey.speakEngine), + findPreference(PreferKey.ttsEngine), speakEngineSummary ) } @@ -91,8 +95,7 @@ class ReadAloudConfigDialog : DialogFragment() { override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { - PreferKey.speakEngine -> - SpeakEngineDialog().show(childFragmentManager, "speakEngine") + PreferKey.ttsEngine -> childFragmentManager.showDialog(SpeakEngineDialog()) } return super.onPreferenceTreeClick(preference) } @@ -107,7 +110,7 @@ class ReadAloudConfigDialog : DialogFragment() { postEvent(EventBus.MEDIA_BUTTON, false) } } - PreferKey.speakEngine -> { + PreferKey.ttsEngine -> { upPreferenceSummary(findPreference(key), speakEngineSummary) ReadAloud.upReadAloudClass() } 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 2ed613f23..ebc44b1e0 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 @@ -14,13 +14,13 @@ import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter -import io.legado.app.constant.PreferKey import io.legado.app.data.appDb import io.legado.app.data.entities.HttpTTS import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.databinding.DialogHttpTtsEditBinding import io.legado.app.databinding.DialogRecyclerViewBinding import io.legado.app.databinding.ItemHttpTtsBinding +import io.legado.app.help.AppConfig import io.legado.app.help.DirectLinkUpload import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert @@ -34,7 +34,6 @@ import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch -import splitties.init.appCtx class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { @@ -43,7 +42,7 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener private val viewModel: SpeakEngineViewModel by viewModels() private val ttsUrlKey = "ttsUrlKey" private val adapter by lazy { Adapter(requireContext()) } - private var engineId = appCtx.getPrefLong(PreferKey.speakEngine) + private var ttsEngine: String? = AppConfig.ttsEngine private val importDocResult = registerForActivityResult(HandleFileContract()) { it?.let { viewModel.importLocal(it) @@ -101,7 +100,7 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener } tvOk.visible() tvOk.setOnClickListener { - putPrefLong(PreferKey.speakEngine, engineId) + AppConfig.ttsEngine = ttsEngine dismissAllowingStateLoss() } tvCancel.visible() @@ -150,8 +149,9 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener SelectItem(it.label, it.name) } context?.selector(R.string.system_tts, ttsItems) { _, item, _ -> - item.value - removePref(PreferKey.speakEngine) + AppConfig.ttsEngine = GSON.toJson(item) + ttsEngine = null + adapter.notifyItemRangeChanged(0, adapter.itemCount) dismissAllowingStateLoss() } } @@ -225,7 +225,7 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener ) { binding.apply { cbName.text = item.name - cbName.isChecked = item.id == engineId + cbName.isChecked = item.id.toString() == ttsEngine } } @@ -233,7 +233,7 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener binding.run { cbName.setOnClickListener { getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS -> - engineId = httpTTS.id + ttsEngine = httpTTS.id.toString() notifyItemRangeChanged(getHeaderCount(), itemCount) } }