pull/1327/head
gedoor 3 years ago
parent 35b4fe01f3
commit eb47b3d6c1
  1. 3
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 3
      app/src/main/java/io/legado/app/data/dao/HttpTTSDao.kt
  3. 6
      app/src/main/java/io/legado/app/help/AppConfig.kt
  4. 20
      app/src/main/java/io/legado/app/model/ReadAloud.kt
  5. 25
      app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudConfigDialog.kt
  6. 16
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt

@ -19,9 +19,8 @@ object PreferKey {
const val clickActionBR = "clickActionBottomRight" const val clickActionBR = "clickActionBottomRight"
const val hideNavigationBar = "hideNavigationBar" const val hideNavigationBar = "hideNavigationBar"
const val precisionSearch = "precisionSearch" const val precisionSearch = "precisionSearch"
const val speakEngine = "speakEngine"
const val readAloudByPage = "readAloudByPage" const val readAloudByPage = "readAloudByPage"
const val ttsEngine = "ttsEngine" const val ttsEngine = "appTtsEngine"
const val ttsSpeechRate = "ttsSpeechRate" const val ttsSpeechRate = "ttsSpeechRate"
const val prevKeys = "prevKeyCodes" const val prevKeys = "prevKeyCodes"
const val nextKeys = "nextKeyCodes" const val nextKeys = "nextKeyCodes"

@ -19,6 +19,9 @@ interface HttpTTSDao {
@Query("select * from httpTTS where id = :id") @Query("select * from httpTTS where id = :id")
fun get(id: Long): HttpTTS? fun get(id: Long): HttpTTS?
@Query("select name from httpTTS where id = :id")
fun getName(id: Long): String?
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg httpTTS: HttpTTS) fun insert(vararg httpTTS: HttpTTS)

@ -222,6 +222,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
appCtx.putPrefBoolean(PreferKey.changeSourceCheckAuthor, value) appCtx.putPrefBoolean(PreferKey.changeSourceCheckAuthor, value)
} }
var ttsEngine: String?
get() = appCtx.getPrefString(PreferKey.ttsEngine)
set(value) {
appCtx.putPrefString(PreferKey.ttsEngine, value)
}
val autoChangeSource: Boolean val autoChangeSource: Boolean
get() = appCtx.getPrefBoolean(PreferKey.autoChangeSource, true) get() = appCtx.getPrefBoolean(PreferKey.autoChangeSource, true)

@ -3,13 +3,13 @@ package io.legado.app.model
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.HttpTTS import io.legado.app.data.entities.HttpTTS
import io.legado.app.help.AppConfig
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.HttpReadAloudService import io.legado.app.service.HttpReadAloudService
import io.legado.app.service.TTSReadAloudService import io.legado.app.service.TTSReadAloudService
import io.legado.app.utils.getPrefLong import io.legado.app.utils.StringUtils
import splitties.init.appCtx import splitties.init.appCtx
object ReadAloud { object ReadAloud {
@ -17,13 +17,17 @@ object ReadAloud {
var httpTTS: HttpTTS? = null var httpTTS: HttpTTS? = null
private fun getReadAloudClass(): Class<*> { private fun getReadAloudClass(): Class<*> {
val spId = appCtx.getPrefLong(PreferKey.speakEngine) val ttsEngine = AppConfig.ttsEngine
httpTTS = appDb.httpTTSDao.get(spId) if (ttsEngine.isNullOrBlank()) {
return if (httpTTS != null) { return TTSReadAloudService::class.java
HttpReadAloudService::class.java
} else {
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() { fun upReadAloudClass() {

@ -14,14 +14,13 @@ import io.legado.app.base.BasePreferenceFragment
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb 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.ATH
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.model.ReadAloud import io.legado.app.model.ReadAloud
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.utils.getPrefLong import io.legado.app.utils.*
import io.legado.app.utils.postEvent
import io.legado.app.utils.windowSize
import splitties.init.appCtx
class ReadAloudConfigDialog : DialogFragment() { class ReadAloudConfigDialog : DialogFragment() {
private val readAloudPreferTag = "readAloudPreferTag" private val readAloudPreferTag = "readAloudPreferTag"
@ -61,15 +60,20 @@ class ReadAloudConfigDialog : DialogFragment() {
private val speakEngineSummary: String private val speakEngineSummary: String
get() { get() {
val eid = appCtx.getPrefLong(PreferKey.speakEngine) val ttsEngine = AppConfig.ttsEngine
val ht = appDb.httpTTSDao.get(eid) ?: return getString(R.string.system_tts)
return ht?.name ?: getString(R.string.system_tts) if (StringUtils.isNumeric(ttsEngine)) {
return appDb.httpTTSDao.getName(ttsEngine.toLong())
?: getString(R.string.system_tts)
}
return GSON.fromJsonObject<SelectItem<String>>(ttsEngine)?.title
?: getString(R.string.system_tts)
} }
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.pref_config_aloud) addPreferencesFromResource(R.xml.pref_config_aloud)
upPreferenceSummary( upPreferenceSummary(
findPreference(PreferKey.speakEngine), findPreference(PreferKey.ttsEngine),
speakEngineSummary speakEngineSummary
) )
} }
@ -91,8 +95,7 @@ class ReadAloudConfigDialog : DialogFragment() {
override fun onPreferenceTreeClick(preference: Preference?): Boolean { override fun onPreferenceTreeClick(preference: Preference?): Boolean {
when (preference?.key) { when (preference?.key) {
PreferKey.speakEngine -> PreferKey.ttsEngine -> childFragmentManager.showDialog(SpeakEngineDialog())
SpeakEngineDialog().show(childFragmentManager, "speakEngine")
} }
return super.onPreferenceTreeClick(preference) return super.onPreferenceTreeClick(preference)
} }
@ -107,7 +110,7 @@ class ReadAloudConfigDialog : DialogFragment() {
postEvent(EventBus.MEDIA_BUTTON, false) postEvent(EventBus.MEDIA_BUTTON, false)
} }
} }
PreferKey.speakEngine -> { PreferKey.ttsEngine -> {
upPreferenceSummary(findPreference(key), speakEngineSummary) upPreferenceSummary(findPreference(key), speakEngineSummary)
ReadAloud.upReadAloudClass() ReadAloud.upReadAloudClass()
} }

@ -14,13 +14,13 @@ import io.legado.app.R
import io.legado.app.base.BaseDialogFragment import io.legado.app.base.BaseDialogFragment
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.HttpTTS import io.legado.app.data.entities.HttpTTS
import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.databinding.DialogEditTextBinding
import io.legado.app.databinding.DialogHttpTtsEditBinding import io.legado.app.databinding.DialogHttpTtsEditBinding
import io.legado.app.databinding.DialogRecyclerViewBinding import io.legado.app.databinding.DialogRecyclerViewBinding
import io.legado.app.databinding.ItemHttpTtsBinding import io.legado.app.databinding.ItemHttpTtsBinding
import io.legado.app.help.AppConfig
import io.legado.app.help.DirectLinkUpload import io.legado.app.help.DirectLinkUpload
import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.SelectItem
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
@ -34,7 +34,6 @@ import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import splitties.init.appCtx
class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
@ -43,7 +42,7 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
private val viewModel: SpeakEngineViewModel by viewModels() private val viewModel: SpeakEngineViewModel by viewModels()
private val ttsUrlKey = "ttsUrlKey" private val ttsUrlKey = "ttsUrlKey"
private val adapter by lazy { Adapter(requireContext()) } 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()) { private val importDocResult = registerForActivityResult(HandleFileContract()) {
it?.let { it?.let {
viewModel.importLocal(it) viewModel.importLocal(it)
@ -101,7 +100,7 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
} }
tvOk.visible() tvOk.visible()
tvOk.setOnClickListener { tvOk.setOnClickListener {
putPrefLong(PreferKey.speakEngine, engineId) AppConfig.ttsEngine = ttsEngine
dismissAllowingStateLoss() dismissAllowingStateLoss()
} }
tvCancel.visible() tvCancel.visible()
@ -150,8 +149,9 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
SelectItem(it.label, it.name) SelectItem(it.label, it.name)
} }
context?.selector(R.string.system_tts, ttsItems) { _, item, _ -> context?.selector(R.string.system_tts, ttsItems) { _, item, _ ->
item.value AppConfig.ttsEngine = GSON.toJson(item)
removePref(PreferKey.speakEngine) ttsEngine = null
adapter.notifyItemRangeChanged(0, adapter.itemCount)
dismissAllowingStateLoss() dismissAllowingStateLoss()
} }
} }
@ -225,7 +225,7 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
) { ) {
binding.apply { binding.apply {
cbName.text = item.name 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 { binding.run {
cbName.setOnClickListener { cbName.setOnClickListener {
getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS -> getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS ->
engineId = httpTTS.id ttsEngine = httpTTS.id.toString()
notifyItemRangeChanged(getHeaderCount(), itemCount) notifyItemRangeChanged(getHeaderCount(), itemCount)
} }
} }

Loading…
Cancel
Save