Created
April 23, 2020 13:34
-
-
Save isfaaghyth/15c7087e8d7facd43e6cd65dcfa04f3b to your computer and use it in GitHub Desktop.
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
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