Skip to content

Instantly share code, notes, and snippets.

@hardik-trivedi
Created August 1, 2017 09:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hardik-trivedi/e4bfe315ff88e292bd45186775082c36 to your computer and use it in GitHub Desktop.
Save hardik-trivedi/e4bfe315ff88e292bd45186775082c36 to your computer and use it in GitHub Desktop.
Kotlin : SharedPreferences using delegated property
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import com.hardiktrivedi.gdg_pune_kotlin_workshop.R
import kotlin.reflect.KProperty
class PreferenceExtension<T>(val context: Context, val key: String, val defaultValue: T) {
val prefs: SharedPreferences by lazy { context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE) }
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
return findPreferences(key, defaultValue)
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
savePreference(key, value)
}
@Suppress("UNCHECKED_CAST")
private fun findPreferences(key: String, defaultValue: T): T {
with(prefs)
{
val result: Any = when (defaultValue) {
is Boolean -> getBoolean(key, defaultValue)
is Int -> getInt(key, defaultValue)
is Long -> getLong(key, defaultValue)
is Float -> getFloat(key, defaultValue)
is String -> getString(key, defaultValue)
else -> throw IllegalArgumentException()
}
return result as T
}
}
@SuppressLint("CommitPrefEdits")
private fun savePreference(key: String, value: T) {
with(prefs.edit())
{
when (value) {
is Boolean -> putBoolean(key, value)
is Int -> putInt(key, value)
is Long -> putLong(key, value)
is Float -> putFloat(key, value)
is String -> putString(key, value)
else -> throw IllegalArgumentException()
}.apply()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment