https://github.com/Codeblin/ObjectPreference
Fast and easy Shared Preferences managing with object mapping annotations for simple or complex class structures
https://github.com/Codeblin/ObjectPreference
android code-generation data dataclasses easy-to-use localstorage mapping-annotations sharedpreferences sharedpreferences-easy sharedpreferences-helper sharedpreferences-manager
Last synced: 3 months ago
JSON representation
Fast and easy Shared Preferences managing with object mapping annotations for simple or complex class structures
- Host: GitHub
- URL: https://github.com/Codeblin/ObjectPreference
- Owner: Codeblin
- Archived: true
- Created: 2020-01-08T15:40:25.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-30T10:13:19.000Z (over 5 years ago)
- Last Synced: 2024-11-07T10:41:27.502Z (8 months ago)
- Topics: android, code-generation, data, dataclasses, easy-to-use, localstorage, mapping-annotations, sharedpreferences, sharedpreferences-easy, sharedpreferences-helper, sharedpreferences-manager
- Language: Kotlin
- Size: 184 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-list - Codeblin/ObjectPreference - Fast and easy Shared Preferences managing with object mapping annotations for simple or complex class structures (Kotlin)
README
[](https://circleci.com/gh/Codeblin/ObjectPreference/tree/master)

[]( https://android-arsenal.com/details/1/8030 )
# ObjectPreference
Fast and easy Shared Preferences managing with object mapping annotations for simple or complex class structures### How to use
* app build.gradle
```gradle
implementation 'com.codeblin.annotations:ObjectPreferences:'
kapt 'com.codeblin.compiler:ObjectPreferencesCompiler:'
```ObjectPreferences uses java 8 so you need to set compile options to target Java 8
```
android{
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
```* First create you model and annotate with `@Document` Annotation
_Example_
``` kotlin
@Document
data class User(
val id: Int,
val firstName: String,
val lastName: String,
val age: Int,
val transactions: List
)data class Transaction(
val id: Int,
val date: Date,
val amount: Double
)
```* **Build project**. Now initialize `SharedPrefManager` at your Application class
```kotlin
SharedPrefManager.initialize(this)
```This will generate the class you will be using which is the name of your class with the suffix 'StoreModel'
```kotlin
class UserStoreModel(
private val value: User
) {
fun save() {
com.codeblin.objectpreference.SharedPrefManager.saveObject(
"User",
value
)
}fun get(): User =
com.codeblin.objectpreference.SharedPrefManager.getObject(
"User"
)fun delete() {
com.codeblin.objectpreference.SharedPrefManager.delete("User")
}
}
```### :sparkles::sparkles:That's it!:sparkles::sparkles:
#### Features
Use \StoreModel class to operate
* Save
```kotlin
val user = UserStoreModel(User(..))
user.save()
```
* Get```kotlin
user.get()
```
* Delete```kotlin
user.delete()
```
* Create an instance with the default empty constructor to reuse your StoreModel across your app```kotlin
user = UserStoreModel()
```* Clear all
```kotlin
SharedPrefManager.clear()
```