From fc03db2ebba843d95d5ad4a0c74ddf1be74b66d9 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 30 Mar 2021 21:59:45 +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 --- .../ui/book/read/config/SpeakEngineDialog.kt | 46 +++++++++++++------ .../book/read/config/SpeakEngineViewModel.kt | 38 +++++++++++++-- 2 files changed, 66 insertions(+), 18 deletions(-) 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 d24989f8b..de629e355 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 @@ -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>? = 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,20 +115,38 @@ 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) - customView { alertBinding.root } - okButton { - alertBinding.editView.text?.toString()?.let { url -> - viewModel.importOnLine(url) - } - } - }.show() - + R.id.menu_import_onLine -> importAlert() } return true } + private fun importAlert() { + val aCache = ACache.get(requireContext(), cacheDir = false) + val cacheUrls: MutableList = 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() + } + @SuppressLint("InflateParams") private fun editHttpTTS(v: HttpTTS? = null) { val httpTTS = v?.copy() ?: HttpTTS() @@ -190,7 +211,6 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener } } - } } \ No newline at end of file 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 644325ad3..613b77eaa 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 @@ -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(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(text)?.let { + appDb.httpTTSDao.insert(*it.toTypedArray()) + } + } + text.isJsonObject() -> { + GSON.fromJsonObject(text)?.let { + appDb.httpTTSDao.insert(it) + } + } + else -> { + throw Exception("格式不对") + } + } + } + } \ No newline at end of file