autoprefs

autoprefs library for convenient use of SharedPreferences

License

License

Categories

Categories

Auto Application Layer Libs Code Generators
GroupId

GroupId

com.dpforge
ArtifactId

ArtifactId

autoprefs
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

aar
Description

Description

autoprefs
autoprefs library for convenient use of SharedPreferences
Project URL

Project URL

https://github.com/int02h/autoprefs
Source Code Management

Source Code Management

https://github.com/int02h/autoprefs

Download autoprefs

How to add to project

<!-- https://jarcasting.com/artifacts/com.dpforge/autoprefs/ -->
<dependency>
    <groupId>com.dpforge</groupId>
    <artifactId>autoprefs</artifactId>
    <version>1.0.2</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.dpforge/autoprefs/
implementation 'com.dpforge:autoprefs:1.0.2'
// https://jarcasting.com/artifacts/com.dpforge/autoprefs/
implementation ("com.dpforge:autoprefs:1.0.2")
'com.dpforge:autoprefs:aar:1.0.2'
<dependency org="com.dpforge" name="autoprefs" rev="1.0.2">
  <artifact name="autoprefs" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.dpforge', module='autoprefs', version='1.0.2')
)
libraryDependencies += "com.dpforge" % "autoprefs" % "1.0.2"
[com.dpforge/autoprefs "1.0.2"]

Dependencies

compile (1)

Group / Artifact Type Version
com.android.support » support-annotations jar 27.1.1

Project Modules

There are no modules declared in this project.

autoprefs

Build Status Latest release

Autoprefs library based on compile-time code generation lets you eliminate boilerplate code for working with Android SharedPreferences. Generated code is fast as if you would wrote it by yourself. Only one reflection call is used to create instance of generated class.

Example

You need to declare interface annotated with @AutoPrefs annotation to work with SharedPreferences:

@AutoPrefs("current-user")
interface User {
    @DefaultLong(-1L)
    LongPref id();
    
    @DefaultString("")
    StringPref name();
    
    IntPref age();
    
    @PrefKey("access-token")
    StringPref accessToken();
    
    BooleanPref loggedIn();
}

Then get the instance:

User user = PrefsProvider.get(User.class);

and start to use SharedPreferences representing some user.

Preferences name

You are able to specify preferences name with @AutoPrefs annotation argument. By default the name of the interface (without package name) will be used. So keep in mind that two interfaces with the same name but in different packages will represent the same preferences instance.

Preferences keys

Method name defines preference key. You can change it with @PrefKey annotation.

Default values

There are six annotations that can be used to set up preference default value. Each for corresponding data type:

  • DefaultBoolean
  • DefaultFloat
  • DefaultInt
  • DefaultLong
  • DefaultString
  • DefaultStringSet

Clear preferences

Declare void method without arguments and place @ClearMethod annotation on it. When called this method will clear all values in SharedPreferences.

Download

To use latest version of the library just add the following code to your Gradle build script:

implementation 'com.dpforge:autoprefs:1.0.2'
implementation 'com.dpforge:autoprefs-annotation:1.0.2'
annotationProcessor 'com.dpforge:autoprefs-processor:1.0.2'

License

Copyright (c) 2018 Daniil Popov

Licensed under the MIT License.

Versions

Version
1.0.2
1.0.1
1.0.0