An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

        

[![CircleCI](https://circleci.com/gh/Codeblin/ObjectPreference.svg?style=svg)](https://circleci.com/gh/Codeblin/ObjectPreference/tree/master)
![Bintray](https://img.shields.io/bintray/v/stamatisstiliatis/ObjectPreferencesCompiler/ObjectPreferencesCompiler)
[![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-ObjectPreference-green.svg?style=flat )]( 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()
```