/EasySP

Auto generate SharedPreferences operating code via AnnotationProcessor

Primary LanguageJava

EasySP

Build Status Download
AnnotationProdessor 编译时期自动生成 SharedPreference操作的代码

  • Eliminate SharedPreferences operating code by using @EasySP on pojo class
  • All the code generate at compile time automaticaly
  • Support any type SharedPreferences supports, eg: String, int, Set<String> etc.
  • This library does not package to your apk, so it will not increase your method count and apk size

Example

@EasySP
class UserPref {
  @DefaultValue("this is default string")
  String userName;
  int age;
  @DefaultValue("123F")
  float thisIsFloat;
  @DefaultValue("123L")
  long thisIsLong;
  
  //getters and setters
}  

class ExampleActivity extends Activity {
  @Override 
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    
    UserPref pref = EasySPUserPref.create(this);//EasySPUserPref is auto generate at compile time
    //EasySPUserPref.create(this, "customFileName");
    pref.setUserName("thisIsUserName");
    //pref.getUserName() returns "thisIsUserName"
    
    //above code is equivalent to below code
    SharedPreferences sp = getSharedPreferences("UserPref", Activity.MODE_PRIVATE);
    Editor editor = sp.edit();
    editor.putString("UserName", "thisIsUserName").apply()
    sp.getString("UserName", "this is default string");
  }
}

Download

dependencies {
    provided "com.zzb.easysp:easysp-annotations:0.7"
    annotationProcessor "com.zzb.easysp:easysp-compiler:0.7"
}