Fiberglass
Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties
Install / Use
/learn @mathcamp/FiberglassREADME
Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties
Idea
Delegated properties in Kotlin allow you to execute arbitrary code whenever a field is accessed. The cool thing about delegated properties is that we can package up the code and reuse it.
Let's package up the notion of getting from SharedPreferences and writing to SharedPreferences.
Usage
Just stick metadata in your classes with specific delegated properties
class User(val ctx: Context) {
companion object {
private val namespace = "user"
}
var email by StringSavable(namespace, ctx)
var lastSaved by DateSavable(namespace, ctx)
var phone by LongSavable(namespace, ctx)
}
Get and set just like normal field properties
val u = User(this)
/* ... */
// Get
val greetings = "Hello ${u.name}"
/* ... */
// Set
u.name = "Brandon"
If you want to save custom a custom data type, you just have to write-to and read-from SharedPreferences
private object DateGetterSetter {
// make it a lazy object to reuse the same inner-class for each savable
val dates by lazy {
object: GetterSetter<Date> {
override fun get(prefs: SharedPreferences, name: String): Date =
Date(prefs.getLong(name, System.currentTimeMillis()))
override fun put(edit: SharedPreferences.Editor, name: String, value: Date) {
edit.putLong(name, value.time)
}
}
}
}
fun DateSavable(namespace: String, ctx: Context): Savable<Date> =
Savable(namespace, ctx, DateGetterSetter.dates)
Example App
See the app module for a sample app
![]()

Default values
Shared preferences requires default values when the data is missing. The default values for the builtin savables are:
| Type | Default Value | | ------------- | ------------- | | StringSavable | "" | | IntSavable | Int.MIN_VALUE | | LongSavable | Long.MIN_VALUE | | DoubleSavable | Double.MIN_VALUE | | FloatSavable | Float.MIN_VALUE | | BooleanSavable | false | | DateSavable | Date now |
Consistency
Savable field writes are eventually consistent (implemented with apply() on the shared preferences editor).
It would make sense to also support consistent writes (implemented with commit() on the shared preferences editor) somehow. Please send a PR or open an issue with ideas.
Related Skills
node-connect
352.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.5kCreate 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
352.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
