|
@@ -10,7 +10,6 @@ import androidx.appcompat.widget.Toolbar
|
|
|
import androidx.fragment.app.viewModels
|
|
import androidx.fragment.app.viewModels
|
|
|
import io.legado.app.R
|
|
import io.legado.app.R
|
|
|
import io.legado.app.base.BaseDialogFragment
|
|
import io.legado.app.base.BaseDialogFragment
|
|
|
-import io.legado.app.data.appDb
|
|
|
|
|
import io.legado.app.data.entities.Server
|
|
import io.legado.app.data.entities.Server
|
|
|
import io.legado.app.data.entities.rule.RowUi
|
|
import io.legado.app.data.entities.rule.RowUi
|
|
|
import io.legado.app.databinding.DialogWebdavServerBinding
|
|
import io.legado.app.databinding.DialogWebdavServerBinding
|
|
@@ -19,7 +18,9 @@ import io.legado.app.lib.theme.primaryColor
|
|
|
import io.legado.app.utils.GSON
|
|
import io.legado.app.utils.GSON
|
|
|
import io.legado.app.utils.applyTint
|
|
import io.legado.app.utils.applyTint
|
|
|
import io.legado.app.utils.setLayout
|
|
import io.legado.app.utils.setLayout
|
|
|
|
|
+import io.legado.app.utils.toastOnUi
|
|
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
|
|
|
|
+import org.json.JSONObject
|
|
|
|
|
|
|
|
class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, true),
|
|
class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, true),
|
|
|
Toolbar.OnMenuItemClickListener {
|
|
Toolbar.OnMenuItemClickListener {
|
|
@@ -33,7 +34,7 @@ class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, t
|
|
|
private val binding by viewBinding(DialogWebdavServerBinding::bind)
|
|
private val binding by viewBinding(DialogWebdavServerBinding::bind)
|
|
|
private val viewModel by viewModels<ServerConfigViewModel>()
|
|
private val viewModel by viewModels<ServerConfigViewModel>()
|
|
|
|
|
|
|
|
- private val serverUi = listOf(
|
|
|
|
|
|
|
+ private val webDavServerUi = listOf(
|
|
|
RowUi("url"),
|
|
RowUi("url"),
|
|
|
RowUi("username"),
|
|
RowUi("username"),
|
|
|
RowUi("password", RowUi.Type.password)
|
|
RowUi("password", RowUi.Type.password)
|
|
@@ -50,33 +51,36 @@ class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, t
|
|
|
binding.toolBar.menu.applyTint(requireContext())
|
|
binding.toolBar.menu.applyTint(requireContext())
|
|
|
binding.toolBar.setOnMenuItemClickListener(this)
|
|
binding.toolBar.setOnMenuItemClickListener(this)
|
|
|
viewModel.init(arguments?.getLong("id")) {
|
|
viewModel.init(arguments?.getLong("id")) {
|
|
|
- upConfigView(viewModel.server)
|
|
|
|
|
|
|
+ upConfigView(viewModel.mServer)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
override fun onMenuItemClick(item: MenuItem): Boolean {
|
|
override fun onMenuItemClick(item: MenuItem): Boolean {
|
|
|
when (item.itemId) {
|
|
when (item.itemId) {
|
|
|
- R.id.menu_save -> {
|
|
|
|
|
- val data = getConfigData()
|
|
|
|
|
- if (data.isEmpty()) {
|
|
|
|
|
- appDb.serverDao.delete(10001)
|
|
|
|
|
- } else {
|
|
|
|
|
- appDb.serverDao.insert(
|
|
|
|
|
- Server(
|
|
|
|
|
- id = 10001,
|
|
|
|
|
- config = GSON.toJson(data)
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ R.id.menu_save -> getServer()?.let {
|
|
|
|
|
+ viewModel.save(it) {
|
|
|
|
|
+ dismissAllowingStateLoss()
|
|
|
}
|
|
}
|
|
|
- dismissAllowingStateLoss()
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return true
|
|
return true
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private fun upConfigView(server: Server?) {
|
|
private fun upConfigView(server: Server?) {
|
|
|
- val data = appDb.serverDao.get(10001)?.getConfigJsonObject()
|
|
|
|
|
- serverUi.forEachIndexed { index, rowUi ->
|
|
|
|
|
|
|
+ binding.etId.setText(server?.id?.toString())
|
|
|
|
|
+ binding.etName.setText(server?.name)
|
|
|
|
|
+ binding.spType.setSelection(
|
|
|
|
|
+ when (server?.type) {
|
|
|
|
|
+ else -> 0
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ when (server?.type) {
|
|
|
|
|
+ else -> upWebDavServerUi(server?.getConfigJsonObject())
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private fun upWebDavServerUi(config: JSONObject?) {
|
|
|
|
|
+ webDavServerUi.forEachIndexed { index, rowUi ->
|
|
|
when (rowUi.type) {
|
|
when (rowUi.type) {
|
|
|
RowUi.Type.text -> ItemSourceEditBinding.inflate(
|
|
RowUi.Type.text -> ItemSourceEditBinding.inflate(
|
|
|
layoutInflater,
|
|
layoutInflater,
|
|
@@ -86,7 +90,7 @@ class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, t
|
|
|
binding.flexbox.addView(it.root)
|
|
binding.flexbox.addView(it.root)
|
|
|
it.root.id = index + 1000
|
|
it.root.id = index + 1000
|
|
|
it.textInputLayout.hint = rowUi.name
|
|
it.textInputLayout.hint = rowUi.name
|
|
|
- it.editText.setText(data?.getString(rowUi.name))
|
|
|
|
|
|
|
+ it.editText.setText(config?.getString(rowUi.name))
|
|
|
}
|
|
}
|
|
|
RowUi.Type.password -> ItemSourceEditBinding.inflate(
|
|
RowUi.Type.password -> ItemSourceEditBinding.inflate(
|
|
|
layoutInflater,
|
|
layoutInflater,
|
|
@@ -98,15 +102,34 @@ class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, t
|
|
|
it.textInputLayout.hint = rowUi.name
|
|
it.textInputLayout.hint = rowUi.name
|
|
|
it.editText.inputType =
|
|
it.editText.inputType =
|
|
|
InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
|
|
InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
|
|
|
- it.editText.setText(data?.getString(rowUi.name))
|
|
|
|
|
|
|
+ it.editText.setText(config?.getString(rowUi.name))
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private fun getConfigData(): Map<String, String> {
|
|
|
|
|
|
|
+ private fun getServer(): Server? {
|
|
|
|
|
+ val id = kotlin.runCatching {
|
|
|
|
|
+ binding.etId.text.toString().toLong()
|
|
|
|
|
+ }.getOrNull()
|
|
|
|
|
+ if (id == null) {
|
|
|
|
|
+ toastOnUi("id不能为空")
|
|
|
|
|
+ return null
|
|
|
|
|
+ }
|
|
|
|
|
+ val server = viewModel.mServer?.copy(id = id) ?: Server(id = id)
|
|
|
|
|
+ server.name = binding.etName.text.toString()
|
|
|
|
|
+ server.type = when (binding.spType.selectedItemPosition) {
|
|
|
|
|
+ else -> Server.TYPE.WEBDAV
|
|
|
|
|
+ }
|
|
|
|
|
+ server.config = when (server.type) {
|
|
|
|
|
+ else -> GSON.toJson(getWebDavConfig())
|
|
|
|
|
+ }
|
|
|
|
|
+ return server
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private fun getWebDavConfig(): HashMap<String, String> {
|
|
|
val data = hashMapOf<String, String>()
|
|
val data = hashMapOf<String, String>()
|
|
|
- serverUi.forEachIndexed { index, rowUi ->
|
|
|
|
|
|
|
+ webDavServerUi.forEachIndexed { index, rowUi ->
|
|
|
val rowView = binding.root.findViewById<View>(index + 1000)
|
|
val rowView = binding.root.findViewById<View>(index + 1000)
|
|
|
ItemSourceEditBinding.bind(rowView).editText.text?.let {
|
|
ItemSourceEditBinding.bind(rowView).editText.text?.let {
|
|
|
data[rowUi.name] = it.toString()
|
|
data[rowUi.name] = it.toString()
|