Skip to content

Instantly share code, notes, and snippets.

@sergei-mikhailovskii-idf
Created November 13, 2022 19:59
Show Gist options
  • Save sergei-mikhailovskii-idf/48640ebfc1355ff11dc26bcf39c25c25 to your computer and use it in GitHub Desktop.
Save sergei-mikhailovskii-idf/48640ebfc1355ff11dc26bcf39c25c25 to your computer and use it in GitHub Desktop.
actual class PreferencesRepository actual constructor(actual val di: DI) {
private val context: Context by di.instance()
val preferences by lazy {
context.getSharedPreferences("fitnestapp", Context.MODE_PRIVATE)
}
actual fun <T> saveValue(key: String, value: T?) {
println("Saved with Android: key=$key, value=$value")
val editor = preferences.edit()
when (value) {
is String -> editor.putString(key, value)
}
editor.apply()
}
actual inline fun <reified T> getValue(key: String, defaultValue: T?): T? {
return when (T::class) {
String::class -> {
preferences.getString(key, defaultValue as String?) as T?
}
else -> null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment