Skip to content

Instantly share code, notes, and snippets.

@SerggioC
Last active October 20, 2019 21:33
Show Gist options
  • Save SerggioC/a8ffa0a5de6360f3306e1e70fb4c3202 to your computer and use it in GitHub Desktop.
Save SerggioC/a8ffa0a5de6360f3306e1e70fb4c3202 to your computer and use it in GitHub Desktop.
import com.the.package.name.extensions.get
import com.the.package.name.extensions.myPreferences
import com.the.package.name.extensions.set
val Context.myPreferences: SharedPreferences
get() = this.getSharedPreferences("${this.packageName} ${this.javaClass.simpleName}", MODE_PRIVATE)
@Suppress("UNCHECKED_CAST")
inline operator fun <reified T> SharedPreferences.get(key: String, defaultValue: T): T {
when (T::class) {
Boolean::class -> return this.getBoolean(key, defaultValue as Boolean) as T
Float::class -> return this.getFloat(key, defaultValue as Float) as T
Int::class -> return this.getInt(key, defaultValue as Int) as T
Long::class -> return this.getLong(key, defaultValue as Long) as T
String::class -> return this.getString(key, defaultValue as String) as T
else -> {
if (defaultValue is Set<*>) {
return this.getStringSet(key, defaultValue as Set<String>) as T
}
}
}
return defaultValue
}
@Suppress("UNCHECKED_CAST")
inline operator fun <reified T> SharedPreferences.set(key: String, value: T): T {
val editor = this.edit()
when (T::class) {
Boolean::class -> editor.putBoolean(key, value as Boolean)
Float::class -> editor.putFloat(key, value as Float)
Int::class -> editor.putInt(key, value as Int)
Long::class -> editor.putLong(key, value as Long)
String::class -> editor.putString(key, value as String)
else -> {
if (value is Set<*>) {
editor.putStringSet(key, value as Set<String>)
}
}
}
editor.commit()
return value
}
private inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
val editor = this.edit()
operation(editor)
editor.apply()
}
val Context.myAppPreferences: SharedPreferences
get() = getSharedPreferences(
"${this.packageName}_${this.javaClass.simpleName}",
MODE_PRIVATE
)
inline fun <reified T : Any> SharedPreferences.getObject(key: String): T? {
return Gson().fromJson<T>(getString(key, null), T::class.java)
}
@Suppress("UNCHECKED_CAST")
inline fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T {
return when (T::class) {
Boolean::class -> getBoolean(key, defaultValue as? Boolean? ?: false) as T
Float::class -> getFloat(key, defaultValue as? Float? ?: 0.0f) as T
Int::class -> getInt(key, defaultValue as? Int? ?: 0) as T
Long::class -> getLong(key, defaultValue as? Long? ?: 0L) as T
String::class -> getString(key, defaultValue as? String? ?: "") as T
else -> {
if (defaultValue is Set<*>) {
getStringSet(key, defaultValue as Set<String>) as T
} else {
val typeName = T::class.java.simpleName
throw Error("Unable to get shared preference with value type '$typeName'. Use getObject")
}
}
}
}
@Suppress("UNCHECKED_CAST")
inline operator fun <reified T : Any> SharedPreferences.set(key: String, value: T) {
with(edit()) {
when (T::class) {
Boolean::class -> putBoolean(key, value as Boolean)
Float::class -> putFloat(key, value as Float)
Int::class -> putInt(key, value as Int)
Long::class -> putLong(key, value as Long)
String::class -> putString(key, value as String)
else -> {
if (value is Set<*>) {
putStringSet(key, value as Set<String>)
} else {
val json = Gson().toJson(value)
putString(key, json)
}
}
}
commit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment