KPreferences
Better SharedPreferences wrapper, like SwiftyUserDefaults
Install / Use
/learn @mariotaku/KPreferencesREADME
KPreferences
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)
Features
Get value
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 integer
Set value
val stringPrefs = KStringKey("name", "def")
prefs[stringPrefs] = "foo" // Calls SharedPreferences.Editor#apply() immediately
prefs.edit {
this[stringPrefs] = "bar"
} // Calls SharedPreferences.Editor#apply() after block executed
Check existence
val stringPrefs = KStringKey("name", "def")
if (stringPrefs in prefs) {
// Check if SharedPreferences contains this key
}
if ("keyString" in prefs) {
// Check if SharedPreferences contains this key
}
Nullable value
val nullableStringPrefs = KNullableStringKey("name", null)
val str = prefs[nullableStringPrefs] // This is a nullable string
Custom type
class 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 object
Get it
Add 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)'
}
License
KPreferences is available under the Apache license, version 2. See the LICENSE file for more info.
Donation
Donation methods
PayPal & AliPay: val email = "mariotaku.lee@gmail.com"
Bitcoin: 1Ag37rPeVUKPHZa6RrsnbkCCz1Envx8xxZ
Buy me a ~~bread~~ game or anything you want :)
Made with ❤️ by <a href="https://mariotaku.org/"><img src="resources/nyan_sakamoto_icon.png" height="20"></a>
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
