Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mathcamp/fiberglass
Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties
https://github.com/mathcamp/fiberglass
Last synced: about 2 months ago
JSON representation
Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties
- Host: GitHub
- URL: https://github.com/mathcamp/fiberglass
- Owner: mathcamp
- Created: 2016-06-27T21:10:51.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-06-27T23:51:17.000Z (over 8 years ago)
- Last Synced: 2024-08-03T03:04:45.448Z (5 months ago)
- Language: Kotlin
- Homepage:
- Size: 299 KB
- Stars: 25
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-kotlin - Fiberglass - Easy lightweight SharedPreferences library with delegated properties. (Libraries / Android)
- awesome-kotlin-libraries-for-android - Fiberglass - Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties. (<a name="utility"></a>Utility <sup>[Back ⇈](#contents)</sup>)
README
> 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
```kotlin
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
```kotlin
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
```kotlin
private object DateGetterSetter {
// make it a lazy object to reuse the same inner-class for each savable
val dates by lazy {
object: GetterSetter {
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 =
Savable(namespace, ctx, DateGetterSetter.dates)
```## Example App
See the `app` module for a sample app
![fiberglass app icon](icon.png)
![screenshot](screenshot.png)
## 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.