优化
This commit is contained in:
+4
-2
@@ -116,10 +116,12 @@ android {
|
|||||||
sourceCompatibility JavaVersion.VERSION_11
|
sourceCompatibility JavaVersion.VERSION_11
|
||||||
targetCompatibility JavaVersion.VERSION_11
|
targetCompatibility JavaVersion.VERSION_11
|
||||||
}
|
}
|
||||||
|
|
||||||
packagingOptions {
|
packagingOptions {
|
||||||
exclude "META-INF/INDEX.LIST"
|
resources {
|
||||||
|
excludes += ['META-INF/INDEX.LIST']
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
// Adds exported schema location as test app assets.
|
// Adds exported schema location as test app assets.
|
||||||
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
|
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ data class Server(
|
|||||||
) : Parcelable {
|
) : Parcelable {
|
||||||
|
|
||||||
enum class TYPE {
|
enum class TYPE {
|
||||||
WEBDAV, ALIYUN, GOOGLEYUN
|
WEBDAV
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
override fun hashCode(): Int {
|
||||||
|
|||||||
@@ -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 -> {
|
R.id.menu_save -> getServer()?.let {
|
||||||
val data = getConfigData()
|
viewModel.save(it) {
|
||||||
if (data.isEmpty()) {
|
dismissAllowingStateLoss()
|
||||||
appDb.serverDao.delete(10001)
|
|
||||||
} else {
|
|
||||||
appDb.serverDao.insert(
|
|
||||||
Server(
|
|
||||||
id = 10001,
|
|
||||||
config = GSON.toJson(data)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
dismissAllowingStateLoss()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun upConfigView(server: Server?) {
|
private fun upConfigView(server: Server?) {
|
||||||
val data = appDb.serverDao.get(10001)?.getConfigJsonObject()
|
binding.etId.setText(server?.id?.toString())
|
||||||
serverUi.forEachIndexed { index, rowUi ->
|
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()
|
||||||
|
|||||||
@@ -4,18 +4,38 @@ import android.app.Application
|
|||||||
import io.legado.app.base.BaseViewModel
|
import io.legado.app.base.BaseViewModel
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.Server
|
import io.legado.app.data.entities.Server
|
||||||
|
import io.legado.app.utils.toastOnUi
|
||||||
|
|
||||||
class ServerConfigViewModel(application: Application): BaseViewModel(application) {
|
class ServerConfigViewModel(application: Application): BaseViewModel(application) {
|
||||||
|
|
||||||
var server: Server? = null
|
var mServer: Server? = null
|
||||||
|
|
||||||
fun init(id: Long?, onSuccess: () -> Unit) {
|
fun init(id: Long?, onSuccess: () -> Unit) {
|
||||||
execute {
|
execute {
|
||||||
if (server == null && id != null) {
|
if (mServer == null && id != null) {
|
||||||
server = appDb.serverDao.get(id)
|
mServer = appDb.serverDao.get(id)
|
||||||
|
mServer
|
||||||
|
} else {
|
||||||
|
null
|
||||||
}
|
}
|
||||||
|
}.onSuccess {
|
||||||
|
if (it != null) {
|
||||||
|
onSuccess.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun save(server: Server, onSuccess: () -> Unit) {
|
||||||
|
execute {
|
||||||
|
mServer?.let {
|
||||||
|
appDb.serverDao.delete(it)
|
||||||
|
}
|
||||||
|
mServer = server
|
||||||
|
appDb.serverDao.insert(server)
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
onSuccess.invoke()
|
onSuccess.invoke()
|
||||||
|
}.onError {
|
||||||
|
context.toastOnUi("保存出错\n${it.localizedMessage}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,10 +28,61 @@
|
|||||||
app:popupTheme="@style/AppTheme.PopupOverlay"
|
app:popupTheme="@style/AppTheme.PopupOverlay"
|
||||||
app:titleTextAppearance="@style/ToolbarTitle" />
|
app:titleTextAppearance="@style/ToolbarTitle" />
|
||||||
|
|
||||||
|
<io.legado.app.ui.widget.text.TextInputLayout
|
||||||
|
android:id="@+id/til_id"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:hint="ID"
|
||||||
|
tools:ignore="HardcodedText">
|
||||||
|
|
||||||
|
<io.legado.app.lib.theme.view.ThemeEditText
|
||||||
|
android:id="@+id/et_id"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
|
||||||
|
</io.legado.app.ui.widget.text.TextInputLayout>
|
||||||
|
|
||||||
|
<io.legado.app.ui.widget.text.TextInputLayout
|
||||||
|
android:id="@+id/til_name"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:hint="@string/name">
|
||||||
|
|
||||||
|
<io.legado.app.lib.theme.view.ThemeEditText
|
||||||
|
android:id="@+id/et_name"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
|
||||||
|
</io.legado.app.ui.widget.text.TextInputLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:gravity="center_vertical">
|
||||||
|
|
||||||
|
<io.legado.app.ui.widget.text.AccentTextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:padding="6dp"
|
||||||
|
android:text="TYPE"
|
||||||
|
tools:ignore="HardcodedText" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatSpinner
|
||||||
|
android:id="@+id/sp_type"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:entries="@array/server_type"
|
||||||
|
app:theme="@style/Spinner"
|
||||||
|
tools:ignore="TouchTargetSizeCheck" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
<androidx.core.widget.NestedScrollView
|
<androidx.core.widget.NestedScrollView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:overScrollMode="ifContentScrolls">
|
android:overScrollMode="ifContentScrolls"
|
||||||
|
tools:ignore="SpeakableTextPresentCheck">
|
||||||
|
|
||||||
<com.google.android.flexbox.FlexboxLayout
|
<com.google.android.flexbox.FlexboxLayout
|
||||||
android:id="@+id/flexbox"
|
android:id="@+id/flexbox"
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources xmlns:tools="http://schemas.android.com/tools">
|
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<string-array name="server_type">
|
||||||
|
<item>WEBDAV</item>
|
||||||
|
</string-array>
|
||||||
|
|
||||||
<string-array name="book_type">
|
<string-array name="book_type">
|
||||||
<item>Text</item>
|
<item>Text</item>
|
||||||
<item>Audio</item>
|
<item>Audio</item>
|
||||||
|
|||||||
Reference in New Issue
Block a user