pull/921/head
gedoor 4 years ago
parent f1e35ca9ed
commit fc03db2ebb
  1. 36
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt
  2. 36
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt

@ -36,12 +36,15 @@ import splitties.init.appCtx
class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
private val binding by viewBinding(DialogRecyclerViewBinding::bind)
private val ttsUrlKey = "ttsUrlKey"
lateinit var adapter: Adapter
private val viewModel: SpeakEngineViewModel by viewModels()
private var httpTTSData: LiveData<List<HttpTTS>>? = null
var engineId = appCtx.getPrefLong(PreferKey.speakEngine)
private var engineId = appCtx.getPrefLong(PreferKey.speakEngine)
private val importDoc = registerForActivityResult(FilePicker()) {
it?.let {
viewModel.importLocal(it)
}
}
override fun onStart() {
@ -112,18 +115,36 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
allowExtensions = arrayOf("txt", "json")
)
)
R.id.menu_import_onLine -> alert(R.string.import_on_line) {
val alertBinding = DialogEditTextBinding.inflate(layoutInflater)
R.id.menu_import_onLine -> importAlert()
}
return true
}
private fun importAlert() {
val aCache = ACache.get(requireContext(), cacheDir = false)
val cacheUrls: MutableList<String> = aCache
.getAsString(ttsUrlKey)
?.splitNotBlank(",")
?.toMutableList() ?: mutableListOf()
alert(R.string.import_on_line) {
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editView.setFilterValues(cacheUrls)
editView.delCallBack = {
cacheUrls.remove(it)
aCache.put(ttsUrlKey, cacheUrls.joinToString(","))
}
}
customView { alertBinding.root }
okButton {
alertBinding.editView.text?.toString()?.let { url ->
if (!cacheUrls.contains(url)) {
cacheUrls.add(0, url)
aCache.put(ttsUrlKey, cacheUrls.joinToString(","))
}
viewModel.importOnLine(url)
}
}
}.show()
}
return true
}
@SuppressLint("InflateParams")
@ -190,7 +211,6 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
}
}
}
}

@ -1,12 +1,12 @@
package io.legado.app.ui.book.read.config
import android.app.Application
import android.net.Uri
import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb
import io.legado.app.data.entities.HttpTTS
import io.legado.app.help.DefaultData
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.*
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toText
@ -21,9 +21,19 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application
fun importOnLine(url: String) {
execute {
RxHttp.get(url).toText("utf-8").await().let { json ->
GSON.fromJsonArray<HttpTTS>(json)?.let {
appDb.httpTTSDao.insert(*it.toTypedArray())
import(json)
}
}.onSuccess {
toastOnUi("导入成功")
}.onError {
toastOnUi("导入失败")
}
}
fun importLocal(uri: Uri) {
execute {
uri.readText(context)?.let {
import(it)
}
}.onSuccess {
toastOnUi("导入成功")
@ -32,4 +42,22 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application
}
}
fun import(text: String) {
when {
text.isJsonArray() -> {
GSON.fromJsonArray<HttpTTS>(text)?.let {
appDb.httpTTSDao.insert(*it.toTypedArray())
}
}
text.isJsonObject() -> {
GSON.fromJsonObject<HttpTTS>(text)?.let {
appDb.httpTTSDao.insert(it)
}
}
else -> {
throw Exception("格式不对")
}
}
}
}
Loading…
Cancel
Save