Created
August 5, 2020 02:56
-
-
Save jso8910/183e18be50be782f495f15b5c1d000ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
companion object { | |
private fun bindPreferenceSummaryToValue(preference: Preference) { | |
preference.onPreferenceChangeListener = sBindPreferenceSummaryToValueListener | |
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(preference.context) | |
if (preference is CheckBoxPreference) { | |
sBindPreferenceSummaryToValueListener.onPreferenceChange( | |
preference, | |
sharedPreferences | |
.getBoolean(preference.key, false) | |
) | |
println(sharedPreferences.getBoolean(preference.key, false)) | |
} else if (preference is EditTextPreference){ | |
sBindPreferenceSummaryToValueListener.onPreferenceChange( | |
preference, | |
sharedPreferences | |
.getString(preference.key, "") | |
) | |
println(sharedPreferences.getString(preference.key, "")) | |
// editor.putString(preference.key, preference.text) | |
} | |
// editor.apply() | |
} | |
private val sBindPreferenceSummaryToValueListener = Preference.OnPreferenceChangeListener { preference, newValue -> | |
val stringValue = newValue.toString() | |
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(preference.context) | |
val editor = sharedPreferences.edit() | |
if (preference is ListPreference) { | |
// For list preferences, look up the correct display value in | |
// the preference's 'entries' list. | |
val index = preference.findIndexOfValue(stringValue) | |
// Set the summary to reflect the new value. | |
preference.value = | |
if (index >= 0) | |
preference.entries[index] as String? | |
else | |
null | |
} else if (preference is EditTextPreference) { | |
if (preference.getKey() == "signature") { | |
// update the changed gallery name to summary filed | |
preference.summaryProvider = null | |
preference.text = stringValue | |
} | |
} else if (preference is CheckBoxPreference) { | |
preference.isChecked = stringValue == "true" | |
println("STRINGVALUE $stringValue") | |
println("PREFERENCE: ${preference.isChecked}") | |
editor.putBoolean(preference.key, preference.isChecked) | |
editor.apply() | |
println(sharedPreferences.getBoolean(preference.key, false)) | |
} else if (preference is SwitchPreferenceCompat) { | |
preference.isChecked = stringValue == "true" | |
editor.putBoolean(preference.key, preference.isChecked) | |
editor.apply() | |
} | |
else { | |
preference.summary = stringValue | |
} | |
true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment