From 3ac6d9807be454203e88b22cd8122654d3aa0ff4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 6 Feb 2020 12:09:09 +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 --- .../main/java/io/legado/app/help/AppConfig.kt | 3 ++ .../app/service/HttpReadAloudService.kt | 4 +- .../book/read/config/ReadAloudConfigDialog.kt | 45 +++++++++---------- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_config_aloud.xml | 2 +- 5 files changed, 26 insertions(+), 29 deletions(-) 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 84de0973c..e1f80dfb5 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -56,6 +56,9 @@ object AppConfig { App.INSTANCE.putPrefInt(PreferKey.ttsSpeechRate, value) } + val ttsSpeechPer: String + get() = App.INSTANCE.getPrefString(PreferKey.ttsSpeechPer) ?: "0" + val isEInkMode: Boolean get() = App.INSTANCE.getPrefBoolean("isEInkMode") diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index 014b1b778..798307652 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -3,7 +3,6 @@ package io.legado.app.service import android.app.PendingIntent import android.media.MediaPlayer import io.legado.app.constant.EventBus -import io.legado.app.constant.PreferKey import io.legado.app.help.AppConfig import io.legado.app.help.IntentHelp import io.legado.app.help.http.HttpHelper @@ -11,7 +10,6 @@ import io.legado.app.help.http.api.HttpPostApi import io.legado.app.service.help.ReadBook import io.legado.app.utils.FileUtils import io.legado.app.utils.LogUtils -import io.legado.app.utils.getPrefString import io.legado.app.utils.postEvent import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Job @@ -114,7 +112,7 @@ class HttpReadAloudService : BaseReadAloudService(), return mapOf( Pair("tex", encodeTwo(content)), Pair("spd", ((AppConfig.ttsSpeechRate + 5) / 10 + 4).toString()), - Pair("per", getPrefString(PreferKey.ttsSpeechPer) ?: "0"), + Pair("per", AppConfig.ttsSpeechPer), Pair("cuid", "baidu_speech_demo"), Pair("idx", "1"), Pair("cod", "2"), 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 7da1ac2e0..294a1c127 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,11 +14,11 @@ import androidx.preference.PreferenceFragmentCompat import io.legado.app.R import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey +import io.legado.app.help.AppConfig import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService import io.legado.app.service.help.ReadAloud import io.legado.app.ui.book.read.Help -import io.legado.app.utils.getPrefString import io.legado.app.utils.postEvent class ReadAloudConfigDialog : DialogFragment() { @@ -59,17 +59,19 @@ class ReadAloudConfigDialog : DialogFragment() { } class ReadAloudPreferenceFragment : PreferenceFragmentCompat(), - SharedPreferences.OnSharedPreferenceChangeListener, - Preference.OnPreferenceChangeListener { + SharedPreferences.OnSharedPreferenceChangeListener { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_aloud) + upPreferenceSummary( + findPreference(PreferKey.ttsSpeechPer), + AppConfig.ttsSpeechPer + ) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) ATH.applyEdgeEffectColor(listView) - bindPreferenceSummaryToValue(findPreference(PreferKey.ttsSpeechPer)) } override fun onResume() { @@ -97,32 +99,25 @@ class ReadAloudConfigDialog : DialogFragment() { ReadAloud.aloudClass = ReadAloud.getReadAloudClass() } PreferKey.ttsSpeechPer -> { + upPreferenceSummary( + findPreference(PreferKey.ttsSpeechPer), + AppConfig.ttsSpeechPer + ) ReadAloud.upTtsSpeechRate(requireContext()) } } } - override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean { - val stringValue = newValue.toString() - - if (preference is ListPreference) { - val index = preference.findIndexOfValue(stringValue) - // Set the summary to reflect the new value. - preference.setSummary(if (index >= 0) preference.entries[index] else null) - } else { - // For all other preferences, set the summary to the value's - preference?.summary = stringValue - } - return false - } - - private fun bindPreferenceSummaryToValue(preference: Preference?) { - preference?.apply { - onPreferenceChangeListener = this@ReadAloudPreferenceFragment - onPreferenceChange( - this, - context.getPrefString(key) - ) + private fun upPreferenceSummary(preference: Preference?, value: String) { + when (preference) { + is ListPreference -> { + val index = preference.findIndexOfValue(value) + // Set the summary to reflect the new value. + preference.summary = if (index >= 0) preference.entries[index] else null + } + else -> { + preference?.summary = value + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 909c9cae5..f08aa7db8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -590,5 +590,6 @@ 自动切换夜间模式 夜间模式跟随系统 上级 + 在线朗读音色 diff --git a/app/src/main/res/xml/pref_config_aloud.xml b/app/src/main/res/xml/pref_config_aloud.xml index d142c526c..d78e3c687 100644 --- a/app/src/main/res/xml/pref_config_aloud.xml +++ b/app/src/main/res/xml/pref_config_aloud.xml @@ -16,7 +16,7 @@