AnyPref,可直接在SharedPreferences中保存对象实例的SharedPreferences工具类

项目Github地址 https://github.com/NashLegend/AnyPrefgit


有时候在写代码的时候常常会有一些要持久保存某个对象的需求,这时候若是动用Sqlite又以为过重,使用SharedPreferences保存的话确实是轻量级了,可是还要针对对象的每一个字段都要保存,可能要好多行代码,读取出来又是好多行代码,咱们为何不直接自动保存与读取对象中的字段呢,若是有保存几个不一样对象的需求的话,那就能省下大量的代码了。github


AnyPref是一个SharedPreferences工具类,它能够直接保存某个对象到SharedPreferences中,使用方法:app


在工程根目录build.gradle添加jitpack:maven


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


在使用app/build.gradle中添加:ide


dependencies {
    compile 'com.github.NashLegend:AnyPref:1.2.1'
}


AnyPref的基本原理是使用反射读取字段名,并将类名做为SharedPreferences的name,将字段名做为SharedPreferences中的key保存字段,同时也支持经过注解来自定义SharedPreferences的name和key,默认它会将全部的SharedPreferences支持的public字段保存(static和final修饰的除外),也能够经过注解来排除某些不须要的字段。若是要保存的对象中还包含了另外一个复杂子对象,好比Family类中有一个Son的字段,这时候Son对象默认是不会保存的,若是想同时保存这个子对象,须要添加PrefSub注解。同理若是要保存的对象中还包含一个ArrayList,要想保存这个ArrayList,须要添加PrefArrayList注解。工具


如何保存与读取数据呢?gradle


在应用的Application的onCreate()中添加以下代码(主要是为了省却后面要传入Context参数的麻烦)ui

    AnyPref.init(this);


假设有一个Sample类。this


    @PrefModel("prefName")//可不添加此注解,"prefName"表示保存SharedPreferences的name,可为任意String字符串,若是不写,则为类的全名
    public class Sample {
    
        @PrefField("intFieldKey")//可不添加此注解,"intFieldKey"表示保存此值时的key,可为任意String字符串,若是不写,则为此变量的变量名
        public int intField = 32;
        
        @PrefIgnore//添加此注解表示不保存这个变量
        public float floatField = 1.2345f;
        
        @PrefField(numDef = 110)//表示若是读取不到后使用的默认值
        public long longField = 95789465213L;
        
        public String stringField = "string";
        
        @PrefField(boolDef = true)
        public boolean boolField = false;
        
        @PrefField(value = "setValueWithSpecifiedKey", strDef = {"1", "2", "3", "4"})//默认值是[1,2,3,4]
        public Set<String> setValue = new LinkedHashSet<>(); 
        
        @PrefSub(nullable = false)//nullable表示取子对象的时候,子对象是否能够为null,默认是true
        public SubSample son1;//标注了@PrefSub的变量,虽然不是SharedPreferences支持的类型,可是仍会被保存
        
        @PrefArrayList(nullable = true, itemNullable = true)//nullable同上,itemNullable表示列表中的数据是否能够为null,默认为true
        public ArrayList<SubSample> sampleArrayList;//标注了@PrefArrayList的ArrayList会被保存,可是ArrayList不能是基本类型的
    }


保存数据:url


    AnyPref.put(sample);
    //或者
    AnyPref.put(sample, "your prefName");第二个参数是本身定义的保存此类的sharedPreferences name,不是PrefModel定义的那个name


读取数据


    Sample sample = AnyPref.get(Sample.class);
    //或者
    Sample sample = AnyPref.get(Sample.class, "your prefName");
    //或者
    Sample sample = AnyPref.get(Sample.class, "your prefName", true);//第三个参数表示读取出来的对象是否能够为null,默认不为null


清除数据


    AnyPref.clear(Sample.class);
    //或者
    AnyPref.clear(Sample.class, "your prefName");


就是这么简单~


同时还有一些简化操做SharedPreferences读写任意数据的方法:


    AnyPref.getPrefs("sample")
            .putLong("long", 920394857382L)
            .putInt("int", 63)
            .putString("string", "sample string");

    AnyPref.getPrefs(Sample.class)
            .beginTransaction()
            .putLong("long", 920394857382L)
            .putInt("int", 63)
            .putString("string", "sample string")
            .commit();

    SharedPrefs sharedPrefs = AnyPref.getPrefs("sample");
    System.out.println(sharedPrefs.getInt("int", 0));
    System.out.println(sharedPrefs.getLong("long", 0));
    System.out.println(sharedPrefs.getString("string", ""));


项目Github地址 https://github.com/NashLegend/AnyPref

相关文章
相关标签/搜索