Skip to content

Instantly share code, notes, and snippets.

@isfaaghyth
Created April 23, 2020 13:34
Show Gist options
  • Save isfaaghyth/15c7087e8d7facd43e6cd65dcfa04f3b to your computer and use it in GitHub Desktop.
Save isfaaghyth/15c7087e8d7facd43e6cd65dcfa04f3b to your computer and use it in GitHub Desktop.
internal class CacheManager(context: Context?) {
val preferences: SharedPreferences? by lazy(LazyThreadSafetyMode.NONE) {
sharedPreferences(context)
}
inline fun <reified T> entry(key: String, obj: T) {
val objString = Gson().toJson(obj, T::class.java)
preferences?.edit()?.putString(key, objString)
}
inline fun <reified T> read(key: String): T? {
val obj = preferences?.getString(key, null)
return Gson().fromJson(obj, T::class.java)
}
companion object {
const val KEY_TEMP_SETTING = "temp_setting"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment