mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
优化
This commit is contained in:
parent
a6c9b05d52
commit
1d0f35b0d2
@ -116,10 +116,12 @@ android {
|
||||
sourceCompatibility JavaVersion.VERSION_11
|
||||
targetCompatibility JavaVersion.VERSION_11
|
||||
}
|
||||
|
||||
packagingOptions {
|
||||
exclude "META-INF/INDEX.LIST"
|
||||
resources {
|
||||
excludes += ['META-INF/INDEX.LIST']
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
// Adds exported schema location as test app assets.
|
||||
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
|
||||
|
@ -23,7 +23,7 @@ data class Server(
|
||||
) : Parcelable {
|
||||
|
||||
enum class TYPE {
|
||||
WEBDAV, ALIYUN, GOOGLEYUN
|
||||
WEBDAV
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
|
@ -10,7 +10,6 @@ import androidx.appcompat.widget.Toolbar
|
||||
import androidx.fragment.app.viewModels
|
||||
import io.legado.app.R
|
||||
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.rule.RowUi
|
||||
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.applyTint
|
||||
import io.legado.app.utils.setLayout
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||
import org.json.JSONObject
|
||||
|
||||
class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, true),
|
||||
Toolbar.OnMenuItemClickListener {
|
||||
@ -33,7 +34,7 @@ class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, t
|
||||
private val binding by viewBinding(DialogWebdavServerBinding::bind)
|
||||
private val viewModel by viewModels<ServerConfigViewModel>()
|
||||
|
||||
private val serverUi = listOf(
|
||||
private val webDavServerUi = listOf(
|
||||
RowUi("url"),
|
||||
RowUi("username"),
|
||||
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.setOnMenuItemClickListener(this)
|
||||
viewModel.init(arguments?.getLong("id")) {
|
||||
upConfigView(viewModel.server)
|
||||
upConfigView(viewModel.mServer)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMenuItemClick(item: MenuItem): Boolean {
|
||||
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
|
||||
}
|
||||
|
||||
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) {
|
||||
RowUi.Type.text -> ItemSourceEditBinding.inflate(
|
||||
layoutInflater,
|
||||
@ -86,7 +90,7 @@ class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, t
|
||||
binding.flexbox.addView(it.root)
|
||||
it.root.id = index + 1000
|
||||
it.textInputLayout.hint = rowUi.name
|
||||
it.editText.setText(data?.getString(rowUi.name))
|
||||
it.editText.setText(config?.getString(rowUi.name))
|
||||
}
|
||||
RowUi.Type.password -> ItemSourceEditBinding.inflate(
|
||||
layoutInflater,
|
||||
@ -98,15 +102,34 @@ class ServerConfigDialog() : BaseDialogFragment(R.layout.dialog_webdav_server, t
|
||||
it.textInputLayout.hint = rowUi.name
|
||||
it.editText.inputType =
|
||||
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>()
|
||||
serverUi.forEachIndexed { index, rowUi ->
|
||||
webDavServerUi.forEachIndexed { index, rowUi ->
|
||||
val rowView = binding.root.findViewById<View>(index + 1000)
|
||||
ItemSourceEditBinding.bind(rowView).editText.text?.let {
|
||||
data[rowUi.name] = it.toString()
|
||||
|
@ -4,18 +4,38 @@ import android.app.Application
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.Server
|
||||
import io.legado.app.utils.toastOnUi
|
||||
|
||||
class ServerConfigViewModel(application: Application): BaseViewModel(application) {
|
||||
|
||||
var server: Server? = null
|
||||
var mServer: Server? = null
|
||||
|
||||
fun init(id: Long?, onSuccess: () -> Unit) {
|
||||
execute {
|
||||
if (server == null && id != null) {
|
||||
server = appDb.serverDao.get(id)
|
||||
if (mServer == null && id != null) {
|
||||
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.invoke()
|
||||
}.onError {
|
||||
context.toastOnUi("保存出错\n${it.localizedMessage}")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,10 +28,61 @@
|
||||
app:popupTheme="@style/AppTheme.PopupOverlay"
|
||||
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
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:overScrollMode="ifContentScrolls">
|
||||
android:overScrollMode="ifContentScrolls"
|
||||
tools:ignore="SpeakableTextPresentCheck">
|
||||
|
||||
<com.google.android.flexbox.FlexboxLayout
|
||||
android:id="@+id/flexbox"
|
||||
|
@ -1,5 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<string-array name="server_type">
|
||||
<item>WEBDAV</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="book_type">
|
||||
<item>Text</item>
|
||||
<item>Audio</item>
|
||||
|
Loading…
Reference in New Issue
Block a user