https://github.com/futuremind/prefs-delegates
https://github.com/futuremind/prefs-delegates
android kotlin rxjava rxjava2 sharedpreferences
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/futuremind/prefs-delegates
- Owner: FutureMind
- License: mit
- Created: 2019-05-06T11:20:29.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-11-04T07:15:52.000Z (over 5 years ago)
- Last Synced: 2024-11-15T20:29:30.583Z (over 1 year ago)
- Topics: android, kotlin, rxjava, rxjava2, sharedpreferences
- Language: Kotlin
- Size: 202 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://jitpack.io/#FutureMind/prefs-delegates)
# Prefs delegates
Yet one more library leveraging kotlin magic for elegant shared prefs handling. With some extra rx sugar on top.
## Features
- Handles all primitive types that Shared Preferences do.
- ...but adds hassle-free `enum` serialization.
- ...and any arbitrary `Serializable` structure via `moshi` json serializer.
- Takes care of proper handling of nullable and default values.
- Provides `RxJava` observability.
## Usage
### Basic usage
You can use the delegates directly like you would use a regular variable:
```kotlin
var someFlag by prefs.boolean("some_flag", false)
textView.text = "The flag is currently: $someFlag" //loaded from shared preferences
someFlag = true //saved to shared preferences
```
**The magic part is that every time you read or write the variable, it's loaded from shared prefs or saved to them.**
### Nullability and default values
```kotlin
var booleanImplicit by prefs.boolean("key1", false)
var booleanExplicit: Boolean by prefs.boolean("key2", false)
var booleanNullableImplicit by prefs.boolean("key3")
var booleanNullableExplicit: Boolean? by prefs.boolean("key4")
var booleanNullableExplicitDefault: Boolean? by prefs.boolean("key5", false)
```
### Observable
```kotlin
val someObservableInt = prefs.observableInt("some_observable_int")
someObservableInt.observable().subscribe {
//will return the current value from shared prefs upon subscription
//and then while it's subscribed, every value subsequently written to it.
}
//for the observable to work you need to save via the same instance
someObservableInt.save(21)
```
## Supported classes
* `Boolean`
* `Int`
* `Long`
* `Float`
* `Double`
* `String`
* `StringSet`
* `Enum` (saved as a string name of the enum)
* and other classes that can be serialized to json via `moshi`
**NOTE** Prefer [moshi-codegen](https://github.com/square/moshi#codegen) for json serialization. Reflection is much slower and deserializing large structures upon app startup may slow it down significantly.
## Installation
Add it in your root build.gradle at the end of repositories:
```groovy
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
```
Add the dependency with current JitPack version: [](https://jitpack.io/#FutureMind/prefs-delegates)
```groovy
dependencies {
implementation 'com.github.FutureMind:prefs-delegates:0.10.0'
// or if you need only one of the library modules:
implementation 'com.github.FutureMind.prefs-delegates:preferencesdelegates:0.10.0'
implementation 'com.github.FutureMind.prefs-delegates:rxpreferencesdelegates:0.10.0'
}
```