Skip to content

Instantly share code, notes, and snippets.

@JakeSteam
Last active February 23, 2023 14:42
Show Gist options
  • Save JakeSteam/2e32518fd026adf5252d8ff18787c97e to your computer and use it in GitHub Desktop.
Save JakeSteam/2e32518fd026adf5252d8ff18787c97e to your computer and use it in GitHub Desktop.
"Generic SharedPreferences Utility Class" for blog.jakelee.co.uk
<application
android:allowBackup="true"
android:fullBackupContent="@xml/backup_rules"
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<include domain="sharedpref" path="com.yourcompany.BACKED_UP.xml" />
</full-backup-content>
class PreferenceUtil(application: Context) {
private val backedUpPreferences: SharedPreferences?
private val nonBackedUpPreferences: SharedPreferences?
init {
backedUpPreferences = application.getSharedPreferences(BACKED_UP_NAME, MODE_PRIVATE)
nonBackedUpPreferences = application.getSharedPreferences(NON_BACKED_UP_NAME, MODE_PRIVATE)
}
fun <T : Any> get(key: String, default: T, fromBackedUp: Boolean = true): T {
val type = default::class.java
val preferences = if (fromBackedUp) backedUpPreferences else nonBackedUpPreferences
if (preferences != null) {
return when (default) {
is String -> type.cast(preferences.getString(key, default))
is Long -> type.cast(preferences.getLong(key, default))
is Int -> type.cast(preferences.getInt(key, default))
is Float -> type.cast(preferences.getFloat(key, default))
is Boolean -> type.cast(preferences.getBoolean(key, default))
else -> default
}
}
return default
}
fun <T : Any> set(key: String, value: T, toBackedUp: Boolean = true): Boolean {
val preferences = if (toBackedUp) backedUpPreferences else nonBackedUpPreferences
if (preferences != null && !TextUtils.isEmpty(key)) {
val editor = preferences.edit()
when (value) {
is String -> editor.putString(key, value)
is Long -> editor.putLong(key, value)
is Int -> editor.putInt(key, value)
is Float -> editor.putFloat(key, value)
is Boolean -> editor.putBoolean(key, value)
else -> return false
}
return editor.commit()
}
return false
}
fun getStringPreference(key: String, defaultValue: String = ""): String = get(key, defaultValue)
fun getLongPreference(key: String, defaultValue: Long = 0L): Long = get(key, defaultValue)
fun getIntegerPreference(key: String, defaultValue: Int = 0): Int = get(key, defaultValue)
fun getBooleanPreference(key: String, defaultValue: Boolean = false): Boolean =
get(key, defaultValue)
fun setStringPreference(key: String, value: String): Boolean = set(key, value)
fun setLongPreference(key: String, value: Long): Boolean = set(key, value)
fun setIntegerPreference(key: String, value: Int): Boolean = set(key, value)
fun setBooleanPreference(key: String, value: Boolean): Boolean = set(key, value)
companion object {
private const val BACKED_UP_NAME = "com.yourcompany.BACKED_UP"
private const val NON_BACKED_UP_NAME = "com.yourcompany.NON_BACKED_UP"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment