优化 #5565
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package io.legado.app.ui.association
|
package io.legado.app.ui.association
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import androidx.core.net.toUri
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import io.legado.app.R
|
import io.legado.app.R
|
||||||
import io.legado.app.base.BaseViewModel
|
import io.legado.app.base.BaseViewModel
|
||||||
@@ -16,6 +17,9 @@ import io.legado.app.help.http.text
|
|||||||
import io.legado.app.utils.isAbsUrl
|
import io.legado.app.utils.isAbsUrl
|
||||||
import io.legado.app.utils.isJsonArray
|
import io.legado.app.utils.isJsonArray
|
||||||
import io.legado.app.utils.isJsonObject
|
import io.legado.app.utils.isJsonObject
|
||||||
|
import io.legado.app.utils.isUri
|
||||||
|
import io.legado.app.utils.readText
|
||||||
|
import splitties.init.appCtx
|
||||||
|
|
||||||
class ImportHttpTtsViewModel(app: Application) : BaseViewModel(app) {
|
class ImportHttpTtsViewModel(app: Application) : BaseViewModel(app) {
|
||||||
|
|
||||||
@@ -79,12 +83,19 @@ class ImportHttpTtsViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
allSources.add(it)
|
allSources.add(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
text.isJsonArray() -> HttpTTS.fromJsonArray(text).getOrThrow().let { items ->
|
text.isJsonArray() -> HttpTTS.fromJsonArray(text).getOrThrow().let { items ->
|
||||||
allSources.addAll(items)
|
allSources.addAll(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
text.isAbsUrl() -> {
|
text.isAbsUrl() -> {
|
||||||
importSourceUrl(text)
|
importSourceUrl(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
text.isUri() -> {
|
||||||
|
importSourceAwait(text.toUri().readText(appCtx))
|
||||||
|
}
|
||||||
|
|
||||||
else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
|
else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ class SpeakEngineDialog() : BaseDialogFragment(R.layout.dialog_recycler_view),
|
|||||||
private val callBack: CallBack? get() = parentFragment as? CallBack
|
private val callBack: CallBack? get() = parentFragment as? CallBack
|
||||||
private val importDocResult = registerForActivityResult(HandleFileContract()) {
|
private val importDocResult = registerForActivityResult(HandleFileContract()) {
|
||||||
it.uri?.let { uri ->
|
it.uri?.let { uri ->
|
||||||
viewModel.importLocal(uri)
|
showDialogFragment(ImportHttpTtsDialog(uri.toString()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private val exportDirResult = registerForActivityResult(HandleFileContract()) {
|
private val exportDirResult = registerForActivityResult(HandleFileContract()) {
|
||||||
|
|||||||
@@ -1,17 +1,9 @@
|
|||||||
package io.legado.app.ui.book.read.config
|
package io.legado.app.ui.book.read.config
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.net.Uri
|
|
||||||
import android.speech.tts.TextToSpeech
|
import android.speech.tts.TextToSpeech
|
||||||
import io.legado.app.base.BaseViewModel
|
import io.legado.app.base.BaseViewModel
|
||||||
import io.legado.app.data.appDb
|
|
||||||
import io.legado.app.data.entities.HttpTTS
|
|
||||||
import io.legado.app.exception.NoStackTraceException
|
|
||||||
import io.legado.app.help.DefaultData
|
import io.legado.app.help.DefaultData
|
||||||
import io.legado.app.utils.isJsonArray
|
|
||||||
import io.legado.app.utils.isJsonObject
|
|
||||||
import io.legado.app.utils.readText
|
|
||||||
import io.legado.app.utils.toastOnUi
|
|
||||||
|
|
||||||
class SpeakEngineViewModel(application: Application) : BaseViewModel(application) {
|
class SpeakEngineViewModel(application: Application) : BaseViewModel(application) {
|
||||||
|
|
||||||
@@ -28,34 +20,4 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun importLocal(uri: Uri) {
|
|
||||||
execute {
|
|
||||||
import(uri.readText(context))
|
|
||||||
}.onSuccess {
|
|
||||||
context.toastOnUi("导入成功")
|
|
||||||
}.onError {
|
|
||||||
context.toastOnUi("导入失败\n${it.localizedMessage}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun import(text: String) {
|
|
||||||
when {
|
|
||||||
text.isJsonArray() -> {
|
|
||||||
HttpTTS.fromJsonArray(text).getOrThrow().let {
|
|
||||||
appDb.httpTTSDao.insert(*it.toTypedArray())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
text.isJsonObject() -> {
|
|
||||||
HttpTTS.fromJson(text).getOrThrow().let {
|
|
||||||
appDb.httpTTSDao.insert(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
throw NoStackTraceException("格式不对")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user