pull/1395/head
gedoor 3 years ago
parent 7a548e48d8
commit 3948bb42fd
  1. 10
      app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditDialog.kt
  2. 74
      app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditViewModel.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt
  4. 40
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineEditViewModel.kt
  5. 10
      app/src/main/res/menu/speak_engine_edit.xml

@ -21,7 +21,7 @@ import io.legado.app.ui.widget.dialog.TextDialog
import io.legado.app.utils.* import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding 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 { Toolbar.OnMenuItemClickListener {
constructor(id: Long) : this() { 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 binding by viewBinding(DialogHttpTtsEditBinding::bind)
private val viewModel by viewModels<SpeakEngineEditViewModel>() private val viewModel by viewModels<HttpTtsEditViewModel>()
override fun onStart() { override fun onStart() {
super.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_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<AppLogDialog>() R.id.menu_log -> showDialogFragment<AppLogDialog>()
R.id.menu_help -> help() R.id.menu_help -> help()
} }

@ -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<HttpTTS>(text1)
}
text1.isJsonArray() -> {
GSON.fromJsonArray<HttpTTS>(text1)?.firstOrNull()
}
else -> {
throw NoStackTraceException("格式不对")
}
}
}.onSuccess {
it?.let { httpTts ->
onSuccess.invoke(httpTts)
} ?: context.toastOnUi("格式不对")
}.onError {
context.toastOnUi(it.localizedMessage)
}
}
}

@ -111,7 +111,7 @@ class SpeakEngineDialog : BaseDialogFragment(R.layout.dialog_recycler_view),
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) { when (item?.itemId) {
R.id.menu_add -> showDialogFragment<SpeakEngineEditDialog>() R.id.menu_add -> showDialogFragment<HttpTtsEditDialog>()
R.id.menu_default -> viewModel.importDefault() R.id.menu_default -> viewModel.importDefault()
R.id.menu_import_local -> importDocResult.launch { R.id.menu_import_local -> importDocResult.launch {
mode = HandleFileContract.FILE mode = HandleFileContract.FILE
@ -199,7 +199,7 @@ class SpeakEngineDialog : BaseDialogFragment(R.layout.dialog_recycler_view),
} }
ivEdit.setOnClickListener { ivEdit.setOnClickListener {
val id = getItemByLayoutPosition(holder.layoutPosition)!!.id val id = getItemByLayoutPosition(holder.layoutPosition)!!.id
showDialogFragment(SpeakEngineEditDialog(id)) showDialogFragment(HttpTtsEditDialog(id))
} }
ivMenuDelete.setOnClickListener { ivMenuDelete.setOnClickListener {
getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS -> getItemByLayoutPosition(holder.layoutPosition)?.let { httpTTS ->

@ -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()
}
}
}

@ -25,6 +25,16 @@
android:title="@string/del_login_header" android:title="@string/del_login_header"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/menu_copy_source"
android:title="@string/copy_source"
app:showAsAction="never" />
<item
android:id="@+id/menu_paste_source"
android:title="@string/paste_source"
app:showAsAction="never" />
<item <item
android:id="@+id/menu_log" android:id="@+id/menu_log"
android:title="@string/log" android:title="@string/log"

Loading…
Cancel
Save