Created
March 5, 2019 14:16
-
-
Save JakeSteam/0dd41b8ffeedbc8d0d21b182f9e60357 to your computer and use it in GitHub Desktop.
Creating a SharedPreferences utility
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
val prefHelper = PreferenceHelper(this) | |
val myBoolean = prefHelper.getBooleanPref(PreferenceHelper.BooleanPref.setting1) | |
prefHelper.setBooleanPref(PreferenceHelper.BooleanPref.setting1, false) | |
val myString = prefHelper.getStringPref(PreferenceHelper.StringPref.setting1) | |
prefHelper.setStringPref(PreferenceHelper.StringPref.setting1, "abc") |
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
import android.content.Context | |
import android.preference.PreferenceManager | |
class PreferenceHelper(val context: Context) { | |
val prefs = PreferenceManager.getDefaultSharedPreferences(context) | |
enum class BooleanPref(val prefId: Int, val defaultId: Int) { | |
setting1(R.string.pref_boolean1, R.bool.pref_boolean1_default), | |
setting2(R.string.pref_boolean2, R.bool.pref_boolean2_default) | |
} | |
fun getBooleanPref(pref: BooleanPref) = | |
prefs.getBoolean(context.getString(pref.prefId), context.resources.getBoolean(pref.defaultId)) | |
fun setBooleanPref(pref: BooleanPref, value: Boolean) = | |
prefs.edit().putBoolean(context.getString(pref.prefId), value).commit() | |
enum class StringPref(val prefId: Int, val defaultId: Int) { | |
setting1(R.string.pref_string1, R.string.pref_string1_default), | |
setting2(R.string.pref_string2, R.string.pref_string2_default) | |
} | |
fun getStringPref(pref: StringPref) = | |
prefs.getString(context.getString(pref.prefId), context.getString(pref.defaultId))!! | |
fun setStringPref(pref: StringPref, value: String) = | |
prefs.edit().putString(context.getString(pref.prefId), value).commit() | |
enum class IntPref(val prefId: Int, val defaultId: Int) { | |
setting1(R.string.pref_int1, R.integer.pref_int1_default), | |
setting2(R.string.pref_int2, R.integer.pref_int2_default) | |
} | |
fun getIntPref(pref: IntPref) = | |
prefs.getInt(context.getString(pref.prefId), context.resources.getInteger(pref.defaultId)) | |
fun setIntPref(pref: IntPref, value: Int) = prefs.edit().putInt(context.getString(pref.prefId), value).commit() | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<!-- Booleans --> | |
<string name="pref_boolean1">boolean1</string> | |
<bool name="pref_boolean1_default">false</bool> | |
<string name="boolean1_title">First boolean</string> | |
<string name="boolean1_desc">This toggles the first boolean</string> | |
<string name="pref_boolean2">boolean2</string> | |
<bool name="pref_boolean2_default">false</bool> | |
<string name="boolean2_title">Second boolean</string> | |
<string name="boolean2_desc">This toggles the second boolean</string> | |
<!-- Strings --> | |
<string name="pref_string1">string1</string> | |
<string name="pref_string1_default">FirstDefault</string> | |
<string name="string1_title">String 1</string> | |
<string name="string1_desc">Set the first string</string> | |
<string name="pref_string2">string2</string> | |
<string name="pref_string2_default">SecondDefault</string> | |
<string name="string2_title">String 2</string> | |
<string name="string2_desc">Set the second string</string> | |
<!-- Int --> | |
<string name="pref_int1">int1</string> | |
<integer name="pref_int1_step">1</integer> | |
<integer name="pref_int1_min">0</integer> | |
<integer name="pref_int1_max">10</integer> | |
<integer name="pref_int1_default">10</integer> | |
<string name="int1_title">Int 1</string> | |
<string name="int1_desc">The first integer</string> | |
<string name="pref_int2">int2</string> | |
<integer name="pref_int2_step">1</integer> | |
<integer name="pref_int2_min">0</integer> | |
<integer name="pref_int2_max">10</integer> | |
<integer name="pref_int2_default">2</integer> | |
<string name="int2_title">Int 2</string> | |
<string name="int2_desc">The second integer</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment