AnyPref

Project Url: NashLegend/AnyPref
Introduction: SharedPreferences 实用工具类,可直接保存与读取实例对象,SharedPreferences 中的 ORM
More: Author   ReportBugs   
Tags:

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

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

在使用 AnyPref 的模块中添加:

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

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

    AnyPref.init(this);

1. 读写实例对象

假设有一个 Sample 类(所有注解都是非必须的)

    @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 会被保存,必须要有泛型信息,且不能是基本类型的,其限制与 PrefModel 相同
    }

保存数据:

    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");

PS,对于实例对象的读写:

  1. 保存的对象必须支持无参构造函数,它是写代码时用到的 Model 对象或者一组 Setting 等,不是用来保存一些系统对象比如 String,View 的;
  2. 保存的对象的字段们中只保存 SharedPreferences 支持的以及标注了@PrefSub@PrefArrayList的字段;
  3. 标注了@PrefSub@PrefArrayList的类型要求同第一条
  4. 只会保存修饰符为public的字段,staticfinal的字段均不会保存;
  5. 不要有循环引用,标注了@PrefSub的对象中不要包含标注了@PrefSub的父对象的类,@PrefArrayList同理,否则会导致向下无限读取
如果使用了 ProGuard,在要保护的类上添加注解@PrefModel,然后在 proguard 配置文件中添加
-keepattributes Signature
-keep class net.nashlegend.anypref.annotations.PrefModel
-keepclasseswithmembernames @net.nashlegend.anypref.annotations.PrefModel class * {
    public <fields>;
}

2. 读写任意数据

    AnyPref.getPrefs("sample")//或者 new SharedPrefs("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", ""));
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools