No description or website provided.
Java
Latest commit aa84c2f Dec 14, 2016 @eiliyaabedini Change readme
Permalink
Failed to load latest commit information.
gradle/wrapper First commit Dec 14, 2016
library Undo delete manifest Dec 14, 2016
.gitignore First commit Dec 14, 2016
README.md Change readme Dec 14, 2016
build.gradle First commit Dec 14, 2016
gradle.properties First commit Dec 14, 2016
gradlew First commit Dec 14, 2016
gradlew.bat First commit Dec 14, 2016
settings.gradle First commit Dec 14, 2016

README.md

SharedDataAccess

SharedDataAccess library is for simple use of SharedPreferences in android

all thing you need is import library, and create interface with setter and getter methods

Also seeMy Android learning blog

Add it to your build.gradle with:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

and:

dependencies {
    compile 'com.github.eiliyaabedini:lib-sharedDataAccess:1.0.2'
}

How to use

Create an interface with getter and setter methods like this

public interface SharedModel {
    String getToken();
    void setToken(String token);
}

Then you need to create an instance of SharedDataAccess in your Application class

SharedModel sharedModel = new SharedDataAccess(this).create(SharedModel.class);

Now you can use your method every where you want

set data to SharedPreferences

sharedModel.setToken("MyToken");

get data from SharedPreferences

String token = sharedModel.getToken();

Note

  • Interface methods must start with set or get and then with a name which used for key.
  • Double not supported by SharedPreferences
  • Supported type is String, int, long, float, boolean
  • you can also set and get a custom Object with supported types fields.

Save an Object

  • To save an Object in SharedPreferences your class must have supported type fields.
  • Only public fields can access by SharedDataAccess and store data
  • Your class must have a default empty constructor

My custom Object:

public class MyObject {
    public int id;
    public String name;

    public MyObject(){

    }

    public MyObject(int id,String name){
        this.id = id;
        this.name = name;
    }
}

Interface:

public interface SharedModel {
    MyObject getMyObject();
    void setMyObject(MyObject myObject);
}

Create instance of SharedDataAccess

SharedModel sharedModel = new SharedDataAccess(this).create(SharedModel.class);

set data to SharedPreferences

sharedModel.setMyObject(new MyObject(1,"Test Data"));

get data from SharedPreferences

MyObject myObject = sharedModel.getMyObject();

More

Your interface can extends from SharedDefaultValues to have default value methods

public interface SharedModel extends SharedDefaultValues {
    String getToken();
    void setToken(String token);
}