-
-
Save JCarlosR/7f457aeebd9c3670933221e97116c4c6 to your computer and use it in GitHub Desktop.
Helper for shared preferences management - Kotlin version
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
package com.programacionymas.helpers | |
import android.content.Context | |
import android.content.SharedPreferences | |
import android.preference.PreferenceManager | |
object PreferenceHelper { | |
fun defaultPrefs(context: Context): SharedPreferences | |
= PreferenceManager.getDefaultSharedPreferences(context) | |
fun customPrefs(context: Context, name: String): SharedPreferences | |
= context.getSharedPreferences(name, Context.MODE_PRIVATE) | |
private inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) { | |
val editor = this.edit() | |
operation(editor) | |
editor.apply() | |
} | |
/** | |
* puts a value for the given [key]. | |
*/ | |
operator fun SharedPreferences.set(key: String, value: Any?) | |
= when (value) { | |
is String? -> edit { it.putString(key, value) } | |
is Int -> edit { it.putInt(key, value) } | |
is Boolean -> edit { it.putBoolean(key, value) } | |
is Float -> edit { it.putFloat(key, value) } | |
is Long -> edit { it.putLong(key, value) } | |
else -> throw UnsupportedOperationException("Not yet implemented") | |
} | |
/** | |
* finds a preference based on the given [key]. | |
* [T] is the type of value | |
* @param defaultValue optional defaultValue - will take a default defaultValue if it is not specified | |
*/ | |
inline operator fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T | |
= when (T::class) { | |
String::class -> getString(key, defaultValue as? String ?: "") as T | |
Int::class -> getInt(key, defaultValue as? Int ?: -1) as T | |
Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T | |
Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T | |
Long::class -> getLong(key, defaultValue as? Long ?: -1) as T | |
else -> throw UnsupportedOperationException("Not yet implemented") | |
} | |
} |
@JCarlosR I used it like below, to get the Prefs object globally:
Prefs.kt
object Prefs {
private const val APP_PREFERENCES = "app_preferences"
lateinit var preferences: SharedPreferences
fun init(context: Context) {
preferences = context.getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE)
}
private inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
val editor = this.edit()
operation(editor)
editor.apply()
}
operator fun set(key: String, value: Any?) =
when (value) {
is String? -> preferences.edit { it.putString(key, value) }
is Int -> preferences.edit { it.putInt(key, value) }
is Boolean -> preferences.edit { it.putBoolean(key, value) }
is Float -> preferences.edit { it.putFloat(key, value) }
is Long -> preferences.edit { it.putLong(key, value) }
else -> throw UnsupportedOperationException("Not yet implemented")
}
inline operator fun <reified T : Any> get(
key: String,
defaultValue: T? = null
): T =
when (T::class) {
String::class -> preferences.getString(key, defaultValue as String? ?: "") as T
Int::class -> preferences.getInt(key, defaultValue as? Int ?: -1) as T
Boolean::class -> preferences.getBoolean(key, defaultValue as? Boolean ?: false) as T
Float::class -> preferences.getFloat(key, defaultValue as? Float ?: -1f) as T
Long::class -> preferences.getLong(key, defaultValue as? Long ?: -1) as T
else -> throw UnsupportedOperationException("Not yet implemented")
}
}
App.kt
class App : Application() {
override fun onCreate() {
super.onCreate()
Prefs.init(this)
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great. @ivangarzab do you mind sharing your combined solution?