Skip to content

paz-lavi/Prefy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome To Prefy!

License GitHub forks GitHub stars GitHub issues

Table of Contents

Usage

Prefy is an android library that will make your life with sharedPreferences easy than ever! Just choose your preferred one, the regular sharedPreferences, or the encrypted sharedPreferences with AES256.

What's New

V1.0.5:

  • New API methos to put value only if the key is not exist.
  • New API methos to get the value and remove the key.
  • New return type. more info here.

Integration

Add it in your root build.gradle at the end of repositories:

allprojects {

repositories {

...

maven { url 'https://jitpack.io' }

}

}

Add the dependency

dependencies {

implementation 'com.github.paz-lavi:Prefy:1.0.4'

}

How To Use

1. Initialize Prefy in your Application class

init(Context context, boolean encrypted)

For example:

public  class  MyApp  extends  Application {

@Override

public  void  onCreate() {

super.onCreate();

Prefy.init(this,true);

}

}

2. Get a Prefy instanse

Prefy  prefy = Prefy.getInstance();

or whenever you need just use Prefy.getInstance() for example:

Prefy.getInstance().getInt("myKey" , 0);

API

String

public  void  putString(String key, String value);
public  boolean  putStringSync(String key, String value);
public  String  getString(String key, String defValue);
public  boolean  putStringIfKeyNotExist(String key, String value);
public  PrefyMsg  putStringSyncIfKeyNotExist(String key, String value);
public  String  getStringAndRemoveKey(String key, String defValue);

example:

prefy.putString("key1", "prefy is ");

prefy.putStringSync("key2", "awesome");

prefy.getString("key1", "");

prefy.getString("key2", "");

Boolean

public  void  putBoolean(String key, boolean value);
public  boolean  putBooleanSync(String key, boolean value);
public  boolean  getBoolean(String key, boolean defValue);
public  boolean  putBooleanIfKeyNotExist(String key, boolean value);
public  PrefyMsg  putBooleanSyncIfKeyNotExist(String key, boolean value);
public  boolean  getBooleanAndRemoveKey(String key, boolean defValue);

example:

prefy.putBoolean("key1", true);

prefy.putBooleanSync("key2", false);

prefy.getBoolean("key1", false);

prefy.getBoolean("key2", true);

Integer

public  void  putInt(String key, int value)
public  boolean  putIntSync(String key, int value)
public  int  getInt(String key, int defValue)
public  boolean  putIntIfKeyNotExist(String key, int value)
public  PrefyMsg  putIntSyncIfKeyNotExist(String key, int value)
public  int  getIntAndRemoveKey(String key, int defValue)

example:

prefy.putInt("key1", 1);

prefy.putIntSync("key2", 96);

prefy.getInt("key1", 29);

prefy.getInt("key2", 18);

Float

public  void  putFloat(String key, float value);
public  boolean  putFloatSync(String key, float value);
public  float  getFloat(String key, float defValue);
public  boolean  putFloatIfKeyNotExist(String key, float value);
public  PrefyMsg  putFloatSyncIfKeyNotExist(String key, float value);
public  float  getFloatAndRemoveKey(String key, float defValue);

example:

prefy.putFloat("key1", 1.03f);

prefy.putFloatSync("key2", 96.65f);

prefy.getFloat("key1", 0f);

prefy.getFloat("key2", 0f);

Long

public  void  putLong(String key, long value)
public  boolean  putLongSync(String key, long value)
public  long  getLong(String key, long defValue)
public  boolean  putLongIfKeyNotExist(String key, long value)
public  PrefyMsg  putLongSyncIfKeyNotExist(String key, long value)
public  long  getLongAndRemoveKey(String key, long defValue)

example:

prefy.putLong("key1", 66656466565541454L);

prefy.putLongSync("key2", 12121L);

prefy.getLong("key1", 0L);

prefy.getLong("key2", 0L);

Double

public  void  putDouble(String key, double value);
public  boolean  putDoubleSync(String key, double value);
public  double  getDouble(String key, double defValue);
public  boolean  putDoubleIfKeyNotExist(String key, double value);
public  PrefyMsg  putDoubleSyncIfKeyNotExist(String key, double value);
public  double  getDoubleAndRemoveKey(String key, double defValue);

example:

prefy.putDouble("key1", 64.40);

prefy.putDoubleSync("key2", 6.23);

prefy.getDouble("key1", 29.01);

prefy.getDouble("key2", 0.0);

String Set

public  void  putStringSet(String key, Set<String> value)
public  boolean  putStringSetSync(String key, Set<String> value)
public  Set<String> getStringSet(String key, Set<String> defValue)
public  boolean  putStringSetIfKeyNotExist(String key, Set<String> value)
public  PrefyMsg  putStringSetSyncIfKeyNotExist(String key, Set<String> value)
public  Set<String> getStringSetAndRemoveKey(String key, Set<String> defValue)

example:

Set<String> s1 = new  HashSet<String>();

Set<String> s2 = new  HashSet<String>();

s1.add("prefy");

s1.add("is");

s2.add("awesome");

s2.add("!");

prefy.getStringSet("key1", s1);

prefy.getStringSet("key2", s2);

prefy.getStringSet("key1", new  HashSet<String>());

prefy.getStringSet("key2", new  HashSet<String>());

Generic Object

public <T> void  putObject(String key, T value)
public <T> boolean  putObjectSync(String key, T value)
public <T> T  getObject(String key, T defValue, Class<T> type)
public <T> boolean putObjectIfKeyNotExist(String key, T value)
public <T> PrefyMsg putObjectSyncIfKeyNotExist(String key, T value)
public <T> T  getObjectAndRemoveKey(String key, T defValue, Class<T> type)

example:

Person  p1 = new  Person("prefy", "1234567890");

Person  p2 = new  Person("is awesome", "9876543210");

  

prefy.putObject("key1", p1);

prefy.putObjectSync("key2", p2);

  

Person  p3 = prefy.getObject("key1", new  Person("", ""), Person.class);

Person  p4 = prefy.getObject("key2", new  Person("", ""), Person.class);

Generic Array

public <T> void  putArray(String key, T[] value) ;
public <T> boolean  putArraySync(String key, T[] value) ;
public <T> T[] getArray(String key, T[] defValue, Type type);
public <T> boolean putArrayIfKeyNotExist(String key, T[] value) ;
public <T> PrefyMsg putArraySyncIfKeyNotExist(String key, T[] value) ;
public <T> T[] getArrayAndRemoveKey(String key, T[] defValue, Type type);

example:

String[] array1 = new  String[]{"prefy", "is", "awesome", "!"};

int[] array2 = new  int[]{1, 2, 3, 4, 5};

Person[] persons = new  Person[]{new  Person("prefy", "1234567890"),

new  Person("is awesome", "9876543210")};

prefy.putArray("key1", array1);

prefy.putArraySync("key2", Arrays.stream(array2).boxed().toArray(Integer[]::new));

prefy.putArray("key3", persons);

  

String[] strings;

Integer[] integers;

Person[] peoples;

  

Type  type1 = prefy.getTypeToken(String[].class);

Type  type2 = prefy.getTypeToken(Integer[].class);

Type  type3 = prefy.getTypeToken(Person[].class);

  

Type  type = prefy.getTypeToken(String[].class);

strings = prefy.getArray("key1", new  String[]{"prefy"}, type1);

integers = prefy.getArray("key2", new  Integer[]{0}, type2);

peoples = prefy.getArray("key3", new  Person[20], type3);

ArrayList

public <T> void  putArrayList(String key, ArrayList<T> value);
public <T> ArrayList<T> getArrayList(String key, ArrayList<T> defValue);
public <T> ArrayList<T> getArrayList(String key, ArrayList<T> defValue);
public <T> void  putArrayListAndRemoveKey(String key, ArrayList<T> value);
public <T> ArrayList<T> getArrayListAndRemoveKey(String key, ArrayList<T> defValue);
public <T> ArrayList<T> getArrayListIfKeyNotExist(String key, ArrayList<T> defValue);

example:

ArrayList<String> l1 = new  ArrayList<>();

ArrayList<String> l2 = new  ArrayList<>();

l1.add("prefy");

l1.add("is");

l2.add("awesome");

l2.add("!");

prefy.putArrayList("key1", arrayList);

prefy.putArrayListSync("key2", arrayList);

  

ArrayList<String> l3 = prefy.getArrayList("Key1", new  ArrayList<String>());

ArrayList<String> l4 = prefy.getArrayList("Key2", new  ArrayList<String>());

HashMap

public <E, T> void  putHashMap(String key, HashMap<E, T> value)
public <E, T> boolean  putHashMapSync(String key, HashMap<E, T> value)
public <E, T> HashMap<E, T> getHashMap(String key, HashMap<E, T> defValue)
public <E, T> boolean  putHashMapIfKeyNotExist(String key, HashMap<E, T> value)
public <E, T> PrefyMsg  putHashMapSyncIfKeyNotExist(String key, HashMap<E, T> value)
public <E, T> HashMap<E, T> getHashMapAndRemoveKey(String key, HashMap<E, T> defValue)

example:

HashMap<String, String> map1 = new  HashMap<>();

HashMap<String, Integer> map2 = new  HashMap<>();

map1.put("k1", "prefy");

map2.put("k2", 112);

prefy.putHashMap("key1", map1);

prefy.putHashMapSync("key2", map2);

HashMap<String, String> map3;

HashMap<String, Integer> map4;

map3 = prefy.getHashMap("key1", new  HashMap<String, String>());

map4 = prefy.getHashMap("key2", new  HashMap<String, Integer>());

Remove

public  void  remove(String key);

example:

prefy.putString("key1", "prefy is ");

prefy.putStringSync("key2", "awesome");

prefy.remove("key1");

prefy.remove("key2");

Type

public  Type  getTypeToken(Type _class);

example:

Type  type = prefy.getTypeToken(String[].class);

PrefyMsg

An enum that representing the return on put<TYPE>SyncIfKeyNotExist(String key,<TYPE> defValue); methods. the options are:

  • key_already_exist - the key is already saved to sharedPreferences
  • saved_successfully - the value saved successfully to sharedPreferences
  • not_saved - the value not saved to sharedPreferences

License


Copyright 2020 Paz Lavi

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

https://github.com/paz-lavi/Prefy/blob/master/LICENSE

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.