pull/34/head
kunfei 5 years ago
parent 27188c5b72
commit c0b6fd8eed
  1. 3
      app/src/main/java/io/legado/app/lib/theme/ATH.kt
  2. 172
      app/src/main/java/io/legado/app/lib/theme/prefs/ATEEditTextPreference.kt
  3. 6
      app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt
  4. 6
      app/src/main/res/xml/pref_config_web_dav.xml

@ -129,7 +129,8 @@ object ATH {
}
fun setTint(
view: View, @ColorInt color: Int, isDark:
view: View,
@ColorInt color: Int, isDark:
Boolean = view.context.isNightTheme
) {
TintHelper.setTintAuto(view, color, false, isDark)

@ -1,172 +0,0 @@
package io.legado.app.lib.theme.prefs
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import android.preference.Preference
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.preference.EditTextPreference
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.utils.applyTint
class ATEEditTextPreference(context: Context?, attrs: AttributeSet?) :
EditTextPreference(context, attrs),
DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
private var builder: AlertDialog.Builder? = null
private var dialog: AlertDialog? = null
private var editText: EditText? = null
/** Which button was clicked. */
private var mWhichButtonClicked: Int = 0
override fun onClick() {
if (dialog?.isShowing == true) return
showDialog(null)
}
protected fun showDialog(state: Bundle?) {
val context = context
mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE
builder = AlertDialog.Builder(context)
.setTitle(dialogTitle)
.setIcon(dialogIcon)
.setPositiveButton(positiveButtonText, this)
.setNegativeButton(negativeButtonText, this)
val builder = this.builder!!
val contentView = onCreateDialogView()
if (contentView != null) {
onBindDialogView(contentView)
builder.setView(contentView)
} else {
builder.setMessage(dialogMessage)
}
// Create the dialog
dialog = builder.create()
val dialog = this.dialog!!
if (state != null) {
dialog.onRestoreInstanceState(state)
}
requestInputMethod(dialog)
dialog.setOnDismissListener(this)
dialog.show()
dialog.applyTint()
}
protected fun onCreateDialogView(): View? {
if (dialogLayoutResource == 0) {
return null
}
val inflater = LayoutInflater.from(context)
return inflater.inflate(dialogLayoutResource, null)
}
private fun requestInputMethod(dialog: Dialog?) {
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}
protected fun onBindDialogView(view: View) {
editText = view.findViewById(android.R.id.edit)
checkNotNull(editText) { "Dialog view must contain an EditText with id" + " @android:id/edit" }
view.findViewById<TextView>(android.R.id.message).visibility = View.GONE
editText?.let {
ATH.setTint(it, ThemeStore.accentColor(context))
it.requestFocus()
it.setText(text)
// Place cursor at the end
it.setSelection(it.length())
}
}
override fun onClick(dialog: DialogInterface?, which: Int) {
mWhichButtonClicked = which
}
override fun onDismiss(dialog: DialogInterface?) {
if (mWhichButtonClicked == DialogInterface.BUTTON_POSITIVE) {
text = editText?.text.toString()
callChangeListener(text)
}
}
override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState()
if (dialog == null || !dialog!!.isShowing) {
return superState
}
val myState = SavedState(superState)
myState.isDialogShowing = true
myState.dialogBundle = dialog!!.onSaveInstanceState()
return myState
}
override fun onRestoreInstanceState(state: Parcelable?) {
if (state == null || state.javaClass != SavedState::class.java) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state)
return
}
val myState = state as SavedState?
super.onRestoreInstanceState(myState!!.superState)
if (myState.isDialogShowing) {
showDialog(myState.dialogBundle)
}
}
private class SavedState : Preference.BaseSavedState {
internal var isDialogShowing: Boolean = false
internal var dialogBundle: Bundle? = null
@SuppressLint("ParcelClassLoader")
constructor(source: Parcel) : super(source) {
isDialogShowing = source.readInt() == 1
dialogBundle = source.readBundle()
}
override fun writeToParcel(dest: Parcel, flags: Int) {
super.writeToParcel(dest, flags)
dest.writeInt(if (isDialogShowing) 1 else 0)
dest.writeBundle(dialogBundle)
}
constructor(superState: Parcelable) : super(superState)
companion object {
@JvmField
val CREATOR: Parcelable.Creator<SavedState> = object : Parcelable.Creator<SavedState> {
override fun createFromParcel(`in`: Parcel): SavedState {
return SavedState(`in`)
}
override fun newArray(size: Int): Array<SavedState?> {
return arrayOfNulls(size)
}
}
}
}
}

@ -3,6 +3,7 @@ package io.legado.app.ui.config
import android.os.Bundle
import android.text.InputType
import android.view.View
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
@ -10,7 +11,7 @@ import io.legado.app.R
import io.legado.app.help.storage.Backup
import io.legado.app.help.storage.WebDavHelp
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.prefs.ATEEditTextPreference
import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.getPrefString
class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener {
@ -19,8 +20,9 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference
addPreferencesFromResource(R.xml.pref_config_web_dav)
bindPreferenceSummaryToValue(findPreference("web_dav_url"))
bindPreferenceSummaryToValue(findPreference("web_dav_account"))
findPreference<ATEEditTextPreference>("web_dav_password")?.let {
findPreference<EditTextPreference>("web_dav_password")?.let {
it.setOnBindEditTextListener { editText ->
ATH.setTint(editText, requireContext().accentColor)
editText.inputType =
InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
}

@ -2,19 +2,19 @@
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<io.legado.app.lib.theme.prefs.ATEEditTextPreference
<androidx.preference.EditTextPreference
android:key="web_dav_url"
android:title="WebDav 服务器地址"
android:summary="输入你的服务器地址"
app:iconSpaceReserved="false" />
<io.legado.app.lib.theme.prefs.ATEEditTextPreference
<androidx.preference.EditTextPreference
android:key="web_dav_account"
android:title="WebDav 账号"
android:summary="输入你的WebDav账号"
app:iconSpaceReserved="false" />
<io.legado.app.lib.theme.prefs.ATEEditTextPreference
<androidx.preference.EditTextPreference
android:key="web_dav_password"
android:title="WebDav 密码"
android:summary="@string/web_dav_pw_s"

Loading…
Cancel
Save