Better SharedPreferences wrapper, like SwiftyUserDefaults
KPreferences is a Kotlin wrapper for Android SharedPreferences, to make it simpler and safer (avoiding null problems at compile time thanks to Kotlin)
val stringPrefs = KStringKey("name", "def")
val str = prefs[stringPrefs] // This is a string
val intPrefs = KIntKey("name", 1)
val int = prefs[stringPrefs] // This is an integerval stringPrefs = KStringKey("name", "def")
prefs[stringPrefs] = "foo" // Calls SharedPreferences.Editor#apply() immediately
prefs.edit {
this[stringPrefs] = "bar"
} // Calls SharedPreferences.Editor#apply() after block executedval stringPrefs = KStringKey("name", "def")
if (stringPrefs in prefs) {
// Check if SharedPreferences contains this key
}
if ("keyString" in prefs) {
// Check if SharedPreferences contains this key
}val nullableStringPrefs = KNullableStringKey("name", null)
val str = prefs[nullableStringPrefs] // This is a nullable stringclass KDateKey(key: String, def: Date) : KSimpleKey<Date>(key, def) {
override fun write(editor: SharedPreferences.Editor, value: Date): Boolean {
editor.putLong(key, value.time)
return true
}
override fun read(preferences: SharedPreferences): Date {
return Date(preferences.getLong(key, def.time))
}
}
val datePrefs = KDateKey("name", Date())
val date = prefs[datePrefs] // This is a date objectAdd JitPack repository in your build.gradle file
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Add this library
dependencies {
compile 'com.github.mariotaku:KPreferences:(latest version)'
}
KPreferences is available under the Apache license, version 2. See the LICENSE file for more info.
Donation methods
PayPal & AliPay: val email = "[email protected]"
Bitcoin: 1Ag37rPeVUKPHZa6RrsnbkCCz1Envx8xxZ
Buy me a bread game or anything you want :)