• Stars
    star
    121
  • Rank 292,239 (Top 6 %)
  • Language
    Java
  • License
    MIT License
  • Created almost 10 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code generation library to manage key-value data for Android

KVS Schema

KVS Schema is a library to manage key-value data for Android. This library generates accessor methods of SharedPreferences from schema class in compile time.

How to use

Create Schema

Let's say you will store user id, You have to create a schema class like below.

@Table(name = "example")
public class ExamplePrefsSchema {
    @Key(name = "user_id") int userId;
}

Class name should be *Schema.

KVS Schema generates ExamplePrefs from ExamplePrefsSchema. You can use generated accessors below.

ExamplePrefs prefs = ExamplePrefs.get(context);
prefs.putUserId(userId);
prefs.putUserId(userId, defaultValue);
prefs.getUserId();
prefs.hasUserId();
prefs.removeUserId();

KVS supports boolean String float int long String set.

Default Values

You can specify default values for the case of that a given key doesn't be contained in a SharedPreferences.

prefs.getUserId(defaultValue);

You can also specify default values through a schema class. KVS Schema can read right values that become a constant in compile time.

@Table(name = "example")
public abstract class ExamplePrefsSchema {
    @Key(name = "user_id") final int userId = -1;
}

Using the mechanism, you don't have to specify default values when you set right values that is a final field initialized to a compile time constant.

Serializer

Sometimes you want to put model classes into SharedPreferences. For that cases, KVS Schema can store data using serializer class.

// Schema class
@Table(name = "example")
public abstract class ExamplePrefsSchema {
    @Key(name = "user", serializer = UserPrefsSerializer.class) String user;
}

// Serializer class
public class UserSerializer implements PrefsSerializer<User, String> {
    @Override public String serialize(User src) { return GSON.toJson(src); }
    @Override public User deserialize(String src) { return GSON.fromJson(src, User.class); }
}

// Usage
prefs.putUser(user);

Builder

You can specify builder class to use custom SharedPreferences.

// Schema class
@Table(name = "example", builder = ExamplePrefsBuilder.class)
public abstract class ExamplePrefsSchema {
    @Key(name = "user_id") int userId;
}

// Builder class
public class ExamplePrefsBuilder implements PrefsBuilder<ExamplePrefs>γ€€{
    @Override
    public ExamplePrefs build(Context context) {
        ...
        return new ExamplePrefs(...); // You can pass your SharedPreferences here
    }
}

Using from Kotlin

KVS Schema generates set* method as an alias of put* to utilize property syntax of Kotlin. So you can read/write values like below when you use Kotlin.

prefs.userId = "Kotlin"
prefs.userId // => Kotlin

Installation

Add dependencies to your build.gradle.

annotationProcessor 'com.rejasupotaro:kvs-schema-compiler:5.1.0'
compile 'com.rejasupotaro:kvs-schema:5.1.0'

Migration

Even if you have already used SharedPreferences directly in your existing app, migration is easy. KVS Schema simply maps the structure of SharedPreferences.

For example, if you are using default SharedPreferences like below,

prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("user_id", "1");
editor.putString("user_name", "Smith");
editor.apply();

your data is saved on path/to/app/shared_prefs/package_name_preferences.xml. The schema class becomes like below.

@Table(name = "package_name_preferences")
public abstract class ExamplePrefsSchema {
    @Key(name = "user_id") int userId;
    @Key(name = "user_name") String userName;
}

More Repositories

1

MaterialDesignSample

Java
351
star
2

Rebuild

Rebuild.fm for Android
Java
178
star
3

octodroid

Android toolkit for the GitHub API
Java
94
star
4

AsyncRssClient

[Deprecated] Simple Asyncronous RSS 2.0 reader library for Android.
Java
27
star
5

cref

Commit message search tool for non-native English speakers
Rust
22
star
6

amazon-product-search

Python
20
star
7

RobotGirl

RobotGirl is a fixtures replacement library for ActiveAndroid
Java
17
star
8

logic-gate

Show how to train and how to run trained model using TensorFlow Mobile/Lite
Kotlin
12
star
9

rxjava-android-example

Java
8
star
10

Hybridge

Hybrid application framework that provides JSON-RPC based communication protocol
Java
7
star
11

katanuki

A template application that includes tons of great open source tools and frameworks.
Shell
5
star
12

gimei-rust

Random Japanese name and address generator (Port of gem's gimei to Rust).
Rust
5
star
13

arxiv-reader

Android client for https://arxiv.org/
Kotlin
4
star
14

metaknight

metaknight is the hero of your test
Java
4
star
15

typeform-ruby

Typeform I/O client for Ruby.
Ruby
3
star
16

KinMozaViewer

A refugee camp for alice lovers
Java
2
star
17

android-dev-env-cookbook

Setup android dev env for mac os x using chef
Ruby
2
star
18

mediumite

Simple wrapper for the Medium API written in Ruby
Ruby
2
star
19

master-thesis

Study on Feature Interactions in Multiple Field Document Ranking
Jupyter Notebook
2
star
20

amazon-product-search-demo

Python
2
star
21

android-dev-env-serverspec

Setup android dev env for mac os x using serverspec
Ruby
1
star
22

altria-connected_instrument_test

Ruby
1
star
23

flip-preferences-tables

Java
1
star
24

imadoko

JavaScript
1
star
25

feature-interactions-in-document-ranking

Jupyter Notebook
1
star