Code Monkey home page Code Monkey logo

auto-preference's Introduction

Auto preference Build Status compiler annotations

Auto generated wrapper over standard SharedPreferences based on @SharedPreference annotation. On each filed with @PreferenceKey("name") annotation will be generated methods:

getName()

getName(defaultValue)

containsName()

removeName()

putName(name)

Also will be generated method removeAll() & isEmpty()

  public void removeAll() {
    SharedPreferences.Editor editor = getSharedPreferences().edit();
    editor.remove("default_id");
    editor.remove("default_name");
    editor.remove("default_enabled");
    editor.commit();
  }

  public boolean isEmpty() {
    SharedPreferences sharedPreferences = getSharedPreferences();
    return !sharedPreferences.contains("default_id") && !sharedPreferences.contains("default_name") && !sharedPreferences.contains("default_enabled") ;}
  

You can choose where to save SharedPreferences: DefaultSharedPreferences or separeate Preference file by setting defaultSharedPreferences to false for separate file or true to use DefaultSharedPreferences (default)

Usage

Create Entity:

      
@SharedPreference(name = "default")
public final class DefaultEntity {

	@PreferenceKey("id") private final int id;
	@PreferenceKey("name") @NonNull private final String name;
	@PreferenceKey("enabled") private final boolean enabled;

	public DefaultEntity(int id, @NonNull String name, boolean enabled) {
		this.id = id;
		this.name = name;
		this.enabled = enabled;
	}

	@Override
	public String toString() {
		return "DefaultEntity{" + "id=" + id + ", name='" + name + '\'' + ", enabled=" + enabled + '}';
	}

	public int getId() {
		return id;
	}

	@NonNull
	public String getName() {
		return name;
	}

	public boolean isEnabled() {
		return enabled;
	}
}

AppPreferences will be generated, based on Entity

      AppPreferences appPreferences = new AppPreferences(context);
      DefaultPreferences defaultPreferences = appPreferences.getDefaultPreferences();
      // Returns immutable entity with values
      DefaultEntity defaultEntity = defaultPreferences.getDefaultEntity();
      
      int id = defaultPreferences.getId();
      int id2 = defaultPreferences.getId(-1);
      
      defaultPreferences.putId(1);
      defaultPreferences.removeId();
      defaultPreferences.containsId();
      
      // Remove all
      defaultPreferences.removeAll();

Download

Gradle:

implementation 'ru.starksoft:preferences-annotations:X.X.X'
annotationProcessor 'ru.starksoft:preferences-compiler:X.X.X'

auto-preference's People

Contributors

starksoft avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.