Androidx Preferences Compiler

Maintenance

License

License

GroupId

GroupId

co.windly
ArtifactId

ArtifactId

androidxpreferences-compiler
Last Version

Last Version

1.0.6
Release Date

Release Date

Type

Type

jar
Description

Description

Androidx Preferences Compiler
Maintenance
Project URL

Project URL

https://github.com/tommus/androidx-prefs
Source Code Management

Source Code Management

https://github.com/tommus/androidx-prefs

Download androidxpreferences-compiler

How to add to project

<!-- https://jarcasting.com/artifacts/co.windly/androidxpreferences-compiler/ -->
<dependency>
    <groupId>co.windly</groupId>
    <artifactId>androidxpreferences-compiler</artifactId>
    <version>1.0.6</version>
</dependency>
// https://jarcasting.com/artifacts/co.windly/androidxpreferences-compiler/
implementation 'co.windly:androidxpreferences-compiler:1.0.6'
// https://jarcasting.com/artifacts/co.windly/androidxpreferences-compiler/
implementation ("co.windly:androidxpreferences-compiler:1.0.6")
'co.windly:androidxpreferences-compiler:jar:1.0.6'
<dependency org="co.windly" name="androidxpreferences-compiler" rev="1.0.6">
  <artifact name="androidxpreferences-compiler" type="jar" />
</dependency>
@Grapes(
@Grab(group='co.windly', module='androidxpreferences-compiler', version='1.0.6')
)
libraryDependencies += "co.windly" % "androidxpreferences-compiler" % "1.0.6"
[co.windly/androidxpreferences-compiler "1.0.6"]

Dependencies

runtime (4)

Group / Artifact Type Version
co.windly : androidxpreferences-annotations jar 1.0.6
org.freemarker : freemarker jar 2.3.28
commons-io : commons-io jar 2.6
org.apache.commons : commons-lang3 jar 3.8.1

Project Modules

There are no modules declared in this project.

AndroidX Preferences

This library uses annotation processing to ensure the compile time verification for user-defined shared preferences.

Usage

  1. Add dependencies.

Add dependencies to the Java-based project:

dependencies {
    implementation "co.windly:androidxpreferences:1.0.6"
    annotationProcessor "co.windly:androidxpreferences-compiler:1.0.6"
}

Add dependencies to the Kotlin-based project:

dependencies {
    implementation "co.windly:androidxpreferences:1.0.6"
    kapt "co.windly:androidxpreferences-compiler:1.0.6"
}
  1. Define shared preferences.

Use the @Prefs annotation on any POJO. All (non static) fields will be considered a preference.

For example:

@Prefs(value = "UserCachePreferences", fileMode = MODE_PRIVATE)
public class UserCache {

    //region Basic

    @DefaultLong(0L)
    Long id;

    @DefaultString("")
    String firstName;

    @DefaultString("")
    String lastName;

    @DefaultString("")
    String password;

    @DefaultBoolean(true)
    Boolean active;

    //endregion
}

Accepted shared preference field types are:

  • Boolean
  • Float
  • Integer
  • Long
  • String
  • Set<String>
  1. Use generated wrapper class.

A class named <YourClassName>Prefs will be generated in the same package (at compile time). Use it like this:

    final UserCachePrefs cache = UserCachePrefs.get(this);

    // Put a single value (apply() is automatically called).
    cache
      .putId(1L);

    // Put several values in one transaction.
    cache
      .edit()
      .putFirstName("John")
      .putLastName("Snow")
      .putPassword("WinterIsComing")
      .putActive(true)
      .apply();

    // Check if a value is set.
    if (cache.containsFirstName()) {
      Log.d(TAG, "First name is set.");
    };

    // Access preferences one by one.
    Log.d(TAG, "id -> " + cache.getId());
    Log.d(TAG, "first name -> " + cache.getFirstName());
    Log.d(TAG, "last name -> " + cache.getLastName());
    Log.d(TAG, "password -> " + cache.getPassword());
    Log.d(TAG, "active -> " + cache.isActive());

    // Access all preferences.
    Log.d(TAG, "cache -> " + cache.getAll());

    // Remove a value.
    cache.removeFirstName();

    // Clear all preferences.
    cache.clear();

Versions

Version
1.0.6
1.0.5