All Projects → yshrsmz → simple-preferences

yshrsmz / simple-preferences

Licence: Apache-2.0 license
Android Library to simplify SharedPreferences use with code generation.

Programming Languages

java
68154 projects - #9 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to simple-preferences

Preferenceroom
🚚 Android processing library for managing SharedPreferences persistence efficiently and structurally.
Stars: ✭ 341 (+610.42%)
Mutual labels:  sharedpreferences, annotations, annotation-processor
Kripton
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.
Stars: ✭ 110 (+129.17%)
Mutual labels:  sharedpreferences, annotations, annotation-processor
WinAnalytics
A light-weight android library that can be quickly integrated into any app to use analytics tools.
Stars: ✭ 23 (-52.08%)
Mutual labels:  annotations, annotation-processor
Zerocell
Simple, efficient Excel to POJO library for Java
Stars: ✭ 53 (+10.42%)
Mutual labels:  annotations, annotation-processor
Kotlin Builder Annotation
A minimal viable replacement for the Lombok @Builder plugin for Kotlin code
Stars: ✭ 67 (+39.58%)
Mutual labels:  annotations, annotation-processor
ColdStorage
Lightweight data loading and caching library for android
Stars: ✭ 39 (-18.75%)
Mutual labels:  annotations, annotation-processor
AutoBindings
Set of annotations that aims to make your Android development experience easier along with lint checks.
Stars: ✭ 15 (-68.75%)
Mutual labels:  annotations, annotation-processor
Kpoet
An expressive DSL built on top of JavaPoet to make writing code almost as easy as writing the code yourself.
Stars: ✭ 58 (+20.83%)
Mutual labels:  annotations, annotation-processor
Gsonpath
A Java annotation processor library which generates gson type adapters using basic JsonPath style annotations
Stars: ✭ 54 (+12.5%)
Mutual labels:  annotations, annotation-processor
memo
Android processing and secured library for managing SharedPreferences as key-value elements efficiently and structurally.
Stars: ✭ 18 (-62.5%)
Mutual labels:  sharedpreferences, annotations
aptk
A toolkit project to enable you to build annotation processors more easily
Stars: ✭ 28 (-41.67%)
Mutual labels:  annotations, annotation-processor
AnnotationProcessorStarter
Project to set up basics of a Java annotation processor
Stars: ✭ 19 (-60.42%)
Mutual labels:  annotations, annotation-processor
AnnotationProcessing
✔️ㅤ[ARTICLE] Writing your own Annotation Processors in Android
Stars: ✭ 47 (-2.08%)
Mutual labels:  annotations, annotation-processor
dagger2-ktx
Kotlin extension bridge library for Dagger2 (proof-of-concept)
Stars: ✭ 41 (-14.58%)
Mutual labels:  annotations, annotation-processor
Placeholderview
This library provides advance views for lists and stacks. Some of the views are build on top of RecyclerView and others are written in their own. Annotations are compiled by annotation processor to generate bind classes. DOCS -->
Stars: ✭ 2,104 (+4283.33%)
Mutual labels:  annotations, annotation-processor
Bulldog
Android library to simplify reading and writing to SharedPreferences, never write code like this anymore prefs.edit().putString("someKey","someString").apply()
Stars: ✭ 133 (+177.08%)
Mutual labels:  sharedpreferences, annotation-processor
Oksharedprefs
通过注解生成SharedPreferences包装类,解决跨进程数据访问不同步的问题。
Stars: ✭ 183 (+281.25%)
Mutual labels:  sharedpreferences, annotations
navigator
Annotation processor that eliminates navigation and Bundle boilerplate
Stars: ✭ 13 (-72.92%)
Mutual labels:  annotation-processor
graphql-metadata
Annotate your graphql schema with lightweight directives
Stars: ✭ 28 (-41.67%)
Mutual labels:  annotations
MethodScope
Reduce repetitive inheritance works in OOP world using @MethodScope.
Stars: ✭ 33 (-31.25%)
Mutual labels:  annotation-processor

Simple Preferences

Android Arsenal Bintray

Simple Preference is a SharedPreference Manager generator.

The library generates code for managing SharedPreferences at compile time with Annotation Processor.
So you don't need to manage SharedPreferences' key string by your hand anymore.

And because getter/setter is generated by Annotation Processor, you can take full advantage of AndroidStudio's auto completion.

Currently the library supports get/set/has/remove operation for each keys(String/int/float/long/Set<String>), and clear operation for SharedPreference itself.

Installation

SimplePreferences is distributed via jCenter. Bintray.
This library requires JDK8 to run annotation processor.

Please note, v2.0.0 only supports AndroidX. If you need to use legacy Android Support libraries, use v1.4.0

dependencies {
  annotationProcessor 'net.yslibrary.simplepreferences:simplepreferences-processor:LATEST_LIBRARY_VERSION'
  implementation 'net.yslibrary.simplepreferences:simplepreferences:LATEST_LIBRARY_VERSION'
}

Usage

define ordinary POJO with @Preferences and @Key.

@Preferences
public class Config {
  // you'd better define these variable as protected to avoid accidental access
  @Key
  protected int userId = 0;

  @Key
  protected String userName = "";

  @Key(omitGetterPrefix = true)
  protected boolean isPremium = false;
}

and following class is generated

public class ConfigPrefs extends Config {
  private final SharedPreferences prefs;

  private ConfigPrefs(@NonNull Context context) {
    prefs = context.getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
  }

  public static ConfigPrefs create(@NonNull Context context) {
    if (context == null) {
      throw new NullPointerException("Context is Null!");
    }
    return new ConfigPrefs(context);
  }

  public void clear() {
    prefs.edit().clear().apply();
  }

  public ConfigPrefs setUserId(int value) {
    prefs.edit().putInt("user_id", value).apply();
    return this;
  }

  public int getUserId() {
    return prefs.getInt("user_id", userId);
  }

  public int getUserId(int defaultValue) {
    return prefs.getInt("user_id", defaultValue);
  }

  public boolean hasUserId() {
    return prefs.contains("user_id");
  }

  public ConfigPrefs removeUserId() {
    prefs.edit().remove("user_id").apply();
    return this;
  }

  public ConfigPrefs setUserName(String value) {
    prefs.edit().putString("user_name", value).apply();
    return this;
  }

  public String getUserName() {
    return prefs.getString("user_name", userName);
  }

  public String getUserName(String defaultValue) {
    return prefs.getString("user_name", defaultValue);
  }

  public boolean hasUserName() {
    return prefs.contains("user_name");
  }

  public ConfigPrefs removeUserName() {
    prefs.edit().remove("user_name").apply();
    return this;
  }

  public ConfigPrefs setIsPremium(boolean value) {
    prefs.edit().putBoolean("is_premium", value).apply();
    return this;
  }

  public boolean isPremium() {
    return prefs.getBoolean("is_premium", isPremium);
  }

  public boolean isPremium(boolean defaultValue) {
    return prefs.getBoolean("is_premium", defaultValue);
  }

  public boolean hasIsPremium() {
    return prefs.contains("is_premium");
  }

  public ConfigPrefs removeIsPremium() {
    prefs.edit().remove("is_premium").apply();
    return this;
  }
}

As you may noticed from generated getter method, the values you set in your POJO become default values for each keys.

Also you can provide custom default value with overloaded method.

Generated classes will be placed at the same package as parent classes.

Annotations

@Preferences

Declare annotated class as SharedPreferences model.

parameter description default vale
value SharedPreferences name empty (class name is converted to lower_snake_case and used as SharedPreferences name)
useDefault use DefaultSharedPreferences or not false
needCommitMethodForClear whether or not create additional clear method which uses SharedPreferences.Editor#commit() false
expose whether to make generated classes public or not true

@key

Declare annotated variable as SharedPreferences key.

parameter description default vale
name preference's key name empty (variable name is converted to lower_snake_case and used as key)
omitGetterPrefix whether or not prepend prefix for getter method false (prepend prefix)
needCommitMethod whether or not create additional setter method which use SharedPreferences.Editor#commit() false (disabled)

omitGetterPrefix is useful when you define boolean value.

needCommitMethodForClear/needCommitMethod is useful when you want to update the backing file synchronously.

License

Copyright 2016-2018 Shimizu Yasuhiro (yshrsmz)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].