Giter Site home page Giter Site logo

androidx-prefs's Introduction

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();

androidx-prefs's People

Contributors

bod avatar wrozwad avatar

Stargazers

 avatar

Watchers

 avatar  avatar

androidx-prefs's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.