pull/94/head
kunfei 5 years ago
parent bef1aea9e0
commit 112f8482de
  1. 63
      app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt

@ -2,6 +2,7 @@ package io.legado.app.ui.config
import android.app.Activity.RESULT_OK import android.app.Activity.RESULT_OK
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
@ -33,7 +34,7 @@ import org.jetbrains.anko.toast
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
class BackupConfigFragment : PreferenceFragmentCompat(), class BackupConfigFragment : PreferenceFragmentCompat(),
Preference.OnPreferenceChangeListener, SharedPreferences.OnSharedPreferenceChangeListener,
CoroutineScope { CoroutineScope {
private lateinit var job: Job private lateinit var job: Job
private val oldDataRequestCode = 11 private val oldDataRequestCode = 11
@ -45,24 +46,17 @@ class BackupConfigFragment : PreferenceFragmentCompat(),
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
job = Job() job = Job()
fun bindPreferenceSummaryToValue(preference: Preference?) {
preference?.apply {
onPreferenceChangeListener = this@BackupConfigFragment
onPreferenceChange(this, context.getPrefString(key))
}
}
addPreferencesFromResource(R.xml.pref_config_backup) addPreferencesFromResource(R.xml.pref_config_backup)
findPreference<EditTextPreference>(PreferKey.webDavUrl)?.let { findPreference<EditTextPreference>(PreferKey.webDavUrl)?.let {
it.setOnBindEditTextListener { editText -> it.setOnBindEditTextListener { editText ->
ATH.setTint(editText, requireContext().accentColor) ATH.setTint(editText, requireContext().accentColor)
} }
bindPreferenceSummaryToValue(it)
} }
findPreference<EditTextPreference>(PreferKey.webDavAccount)?.let { findPreference<EditTextPreference>(PreferKey.webDavAccount)?.let {
it.setOnBindEditTextListener { editText -> it.setOnBindEditTextListener { editText ->
ATH.setTint(editText, requireContext().accentColor) ATH.setTint(editText, requireContext().accentColor)
} }
bindPreferenceSummaryToValue(it)
} }
findPreference<EditTextPreference>(PreferKey.webDavPassword)?.let { findPreference<EditTextPreference>(PreferKey.webDavPassword)?.let {
it.setOnBindEditTextListener { editText -> it.setOnBindEditTextListener { editText ->
@ -70,9 +64,11 @@ class BackupConfigFragment : PreferenceFragmentCompat(),
editText.inputType = editText.inputType =
InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
} }
bindPreferenceSummaryToValue(it)
} }
bindPreferenceSummaryToValue(findPreference(PreferKey.backupPath)) upPreferenceSummary(PreferKey.webDavUrl, getPrefString(PreferKey.webDavUrl))
upPreferenceSummary(PreferKey.webDavAccount, getPrefString(PreferKey.webDavAccount))
upPreferenceSummary(PreferKey.webDavPassword, getPrefString(PreferKey.webDavPassword))
upPreferenceSummary(PreferKey.backupPath, getPrefString(PreferKey.backupPath))
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -80,41 +76,58 @@ class BackupConfigFragment : PreferenceFragmentCompat(),
ATH.applyEdgeEffectColor(listView) ATH.applyEdgeEffectColor(listView)
} }
override fun onResume() {
super.onResume()
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
super.onPause()
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
job.cancel() job.cancel()
} }
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (preference?.key) { key?.let {
upPreferenceSummary(it, getPrefString(it))
}
}
private fun upPreferenceSummary(preferenceKey: String, value: String?) {
val preference = findPreference<Preference>(preferenceKey) ?: return
when (preferenceKey) {
PreferKey.webDavUrl -> PreferKey.webDavUrl ->
if (newValue == null) { if (value == null) {
preference.summary = getString(R.string.web_dav_url_s) preference.summary = getString(R.string.web_dav_url_s)
} else { } else {
preference.summary = newValue.toString() preference.summary = value.toString()
} }
PreferKey.webDavAccount -> PreferKey.webDavAccount ->
if (newValue == null) { if (value == null) {
preference.summary = getString(R.string.web_dav_account_s) preference.summary = getString(R.string.web_dav_account_s)
} else { } else {
preference.summary = newValue.toString() preference.summary = value.toString()
} }
PreferKey.webDavPassword -> PreferKey.webDavPassword ->
if (newValue == null) { if (value == null) {
preference.summary = getString(R.string.web_dav_pw_s) preference.summary = getString(R.string.web_dav_pw_s)
} else { } else {
preference.summary = "*".repeat(newValue.toString().length) preference.summary = "*".repeat(value.toString().length)
} }
else -> else -> {
if (preference is ListPreference) { if (preference is ListPreference) {
val index = preference.findIndexOfValue(newValue?.toString()) val index = preference.findIndexOfValue(value)
// Set the summary to reflect the new value. // Set the summary to reflect the new value.
preference.setSummary(if (index >= 0) preference.entries[index] else null) preference.summary = if (index >= 0) preference.entries[index] else null
} else { } else {
preference?.summary = newValue?.toString() preference.summary = value
} }
}
} }
return false
} }
override fun onPreferenceTreeClick(preference: Preference?): Boolean { override fun onPreferenceTreeClick(preference: Preference?): Boolean {
@ -346,7 +359,6 @@ class BackupConfigFragment : PreferenceFragmentCompat(),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
AppConfig.backupPath = uri.toString() AppConfig.backupPath = uri.toString()
findPreference<Preference>(PreferKey.backupPath)?.summary = uri.toString()
launch { launch {
Backup.backup(requireContext(), uri.toString()) Backup.backup(requireContext(), uri.toString())
toast(R.string.backup_success) toast(R.string.backup_success)
@ -360,7 +372,6 @@ class BackupConfigFragment : PreferenceFragmentCompat(),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
AppConfig.backupPath = uri.toString() AppConfig.backupPath = uri.toString()
findPreference<Preference>(PreferKey.backupPath)?.summary = uri.toString()
launch { launch {
Restore.restore(requireContext(), uri) Restore.restore(requireContext(), uri)
toast(R.string.restore_success) toast(R.string.restore_success)

Loading…
Cancel
Save