Skip to content

Instantly share code, notes, and snippets.

@Na0ki
Created August 17, 2018 12:23
Show Gist options
  • Save Na0ki/68d4620f591fa8a626edb02f6eae13b6 to your computer and use it in GitHub Desktop.
Save Na0ki/68d4620f591fa8a626edb02f6eae13b6 to your computer and use it in GitHub Desktop.
@Throws(ClassCastException::class, InvalidClassException::class)
inline fun <reified T : Any> get(id: Int): T {
return get(sApp.getString(id))
}
@Throws(ClassCastException::class, InvalidClassException::class)
inline fun <reified T : Any> get(key: String): T {
return when (T::class) {
String::class -> sPrefs.getString(key, "") as T? ?: "" as T
Set::class -> sPrefs.getStringSet(key, emptySet()) as T? ?: emptySet<String>() as T
Int::class -> sPrefs.getInt(key, 0) as T
Long::class -> sPrefs.getLong(key, 0L) as T
Float::class -> sPrefs.getFloat(key, 0.toFloat()) as T
Boolean::class -> sPrefs.getBoolean(key, false) as T
else -> throw InvalidClassException("${T::class} is not supported")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment