diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineEditDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditDialog.kt similarity index 92% rename from app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineEditDialog.kt rename to app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditDialog.kt index bda1ad1fb..56767fba0 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditDialog.kt @@ -21,7 +21,7 @@ import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding -class SpeakEngineEditDialog() : BaseDialogFragment(R.layout.dialog_http_tts_edit), +class HttpTtsEditDialog() : BaseDialogFragment(R.layout.dialog_http_tts_edit), Toolbar.OnMenuItemClickListener { constructor(id: Long) : this() { @@ -31,7 +31,7 @@ class SpeakEngineEditDialog() : BaseDialogFragment(R.layout.dialog_http_tts_edit } private val binding by viewBinding(DialogHttpTtsEditBinding::bind) - private val viewModel by viewModels() + private val viewModel by viewModels() override fun onStart() { super.onStart() @@ -100,6 +100,12 @@ class SpeakEngineEditDialog() : BaseDialogFragment(R.layout.dialog_http_tts_edit } } R.id.menu_del_login_header -> dataFromView().removeLoginHeader() + R.id.menu_copy_source -> dataFromView().let { + context?.sendToClip(GSON.toJson(it)) + } + R.id.menu_paste_source -> viewModel.importFromClip { + initView(it) + } R.id.menu_log -> showDialogFragment() R.id.menu_help -> help() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditViewModel.kt new file mode 100644 index 000000000..1c8e48580 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditViewModel.kt @@ -0,0 +1,74 @@ +package io.legado.app.ui.book.read.config + +import android.app.Application +import android.os.Bundle +import io.legado.app.base.BaseViewModel +import io.legado.app.data.appDb +import io.legado.app.data.entities.HttpTTS +import io.legado.app.model.NoStackTraceException +import io.legado.app.model.ReadAloud +import io.legado.app.utils.* + +class HttpTtsEditViewModel(app: Application) : BaseViewModel(app) { + + var id: Long? = null + + fun initData(arguments: Bundle?, success: (httpTTS: HttpTTS) -> Unit) { + execute { + if (id == null) { + id = arguments?.getLong("id") + val httpTTS = id?.let { + return@let appDb.httpTTSDao.get(it) + } + return@execute httpTTS + } + return@execute null + }.onSuccess { + it?.let { + success.invoke(it) + } + } + } + + fun save(httpTTS: HttpTTS, success: (() -> Unit)? = null) { + execute { + appDb.httpTTSDao.insert(httpTTS) + ReadAloud.upReadAloudClass() + }.onSuccess { + success?.invoke() + } + } + + fun importFromClip(onSuccess: (httpTTS: HttpTTS) -> Unit) { + val text = context.getClipText() + if (text.isNullOrBlank()) { + context.toastOnUi("剪贴板为空") + } else { + importSource(text, onSuccess) + } + } + + fun importSource(text: String, onSuccess: (httpTTS: HttpTTS) -> Unit) { + val text1 = text.trim() + execute { + when { + text1.isJsonObject() -> { + GSON.fromJsonObject(text1) + } + text1.isJsonArray() -> { + GSON.fromJsonArray(text1)?.firstOrNull() + } + else -> { + throw NoStackTraceException("格式不对") + } + } + }.onSuccess { + it?.let { httpTts -> + onSuccess.invoke(httpTts) + } ?: context.toastOnUi("格式不对") + }.onError { + context.toastOnUi(it.localizedMessage) + } + } + +} \ No newline at end of file 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 b9db433e1..285cef3c6 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 @@ -111,7 +111,7 @@ class SpeakEngineDialog : BaseDialogFragment(R.layout.dialog_recycler_view), override fun onMenuItemClick(item: MenuItem?): Boolean { when (item?.itemId) { - R.id.menu_add -> showDialogFragment() + R.id.menu_add -> showDialogFragment() R.id.menu_default -> viewModel.importDefault() R.id.menu_import_local -> importDocResult.launch { mode = HandleFileContract.FILE @@ -199,7 +199,7 @@ class SpeakEngineDialog : BaseDialogFragment(R.layout.dialog_recycler_view), } ivEdit.setOnClickListener { val id = getItemByLayoutPosition(holder.layoutPosition)!!.id - showDialogFragment(SpeakEngineEditDialog(id)) + showDialogFragment(HttpTtsEditDialog(id)) } ivMenuDelete.setOnClickListener { getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS -> diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineEditViewModel.kt deleted file mode 100644 index 651bfdea5..000000000 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineEditViewModel.kt +++ /dev/null @@ -1,40 +0,0 @@ -package io.legado.app.ui.book.read.config - -import android.app.Application -import android.os.Bundle -import io.legado.app.base.BaseViewModel -import io.legado.app.data.appDb -import io.legado.app.data.entities.HttpTTS -import io.legado.app.model.ReadAloud - -class SpeakEngineEditViewModel(app: Application) : BaseViewModel(app) { - - var id: Long? = null - - fun initData(arguments: Bundle?, success: (httpTTS: HttpTTS) -> Unit) { - execute { - if (id == null) { - id = arguments?.getLong("id") - val httpTTS = id?.let { - return@let appDb.httpTTSDao.get(it) - } - return@execute httpTTS - } - return@execute null - }.onSuccess { - it?.let { - success.invoke(it) - } - } - } - - fun save(httpTTS: HttpTTS, success: (() -> Unit)? = null) { - execute { - appDb.httpTTSDao.insert(httpTTS) - ReadAloud.upReadAloudClass() - }.onSuccess { - success?.invoke() - } - } - -} \ No newline at end of file diff --git a/app/src/main/res/menu/speak_engine_edit.xml b/app/src/main/res/menu/speak_engine_edit.xml index 36f863612..87b1092ad 100644 --- a/app/src/main/res/menu/speak_engine_edit.xml +++ b/app/src/main/res/menu/speak_engine_edit.xml @@ -25,6 +25,16 @@ android:title="@string/del_login_header" app:showAsAction="never" /> + + + +