AptPreferences是基於面向對象設計的快速持久化框架,目的是爲了簡化SharePreferences的使用,減小代碼的編寫。能夠很是快速地保存基本類型和對象。AptPreferences是基於APT技術實現,在編譯期間實現代碼的生成,支持混淆。根據不一樣的用戶區分持久化信息。java
@AptPreferences public class Settings { private long loginTime; private LoginUser loginUser; // get、set方法 }
//初始化 AptPreferencesManager.init(this, null); // 保存信息 SettingsPreference.get().setLoginTime(System.currentTimeMillis()); SettingsPreference.get().set(new LoginUser("Wiki")); // 獲取信息 long loginTime = SettingsPreference.get().getLoginTime(); LoginUser loginUser = SettingsPreference.get().getLoginUser();
從上面的簡單例子能夠看到,咱們須要作SharePreferences持久化,僅僅定義一個簡單的javabean類(Settings)並添加註解便可,這個框架會根據javabean生成帶有持久化功能的SettingsPreference類,經過這個類就能夠很是簡單去保持或者獲取數據,大大簡化了SharePreferences的使用,也能夠保持對象。android
https://github.com/joyrun/AptPreferencesgit
buildscript { repositories { jcenter() } dependencies { classpath "com.android.tools.build:gradle:1.5.0" classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } allprojects { repositories { jcenter() maven { url "https://jitpack.io" } } }
apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt' //... dependencies { compile 'com.github.joyrun.AptPreferences:aptpreferences:0.4.4' apt 'com.github.joyrun.AptPreferences:aptpreferences-compiler:0.4.4' }
使用方法很是簡單,先編寫一個普通帶getter、setter的javabean類,在類頭部添加@AptPreferences便可:github
@AptPreferences public class Settings { private long lastOpenAppTimeMillis; // 使用commit提交,默認是使用apply提交,配置默認值 @AptField(commit = true) private String useLanguage = "zh"; // 使用preferences的方式保存 @AptField(preferences = true) private Push push; // 使用對象的方式保存 private LoginUser loginUser; // 不持久化該字段,僅僅保留在內存 @AptField(save = false) private long lastActionTimeMillis; // ... // get、set方法必須寫 }
咱們提供了兩個註解@AptPreferences和@AptField(commit = false,save = true,preferences = false)。express
被註解的javabean必須爲字段實現setter和getter方法;apache
AptField有三個參數能夠配置。json
commit:能夠配置使用commit仍是apply持久化,默認是apply,須要在一些須要馬上保存到文件的可使用commit方式,好比在退出APP時保存退出的時間。app
save:用來聲明是否須要持久化這個字段。框架
global:默認是true,若是設置爲false時,和AptPreferencesManager.setUserInfo()配合,能夠爲不一樣的用戶進行持久化,達到每一個用戶有不用的設置。less
使用以前要進行初始化,建議在Application進行初始化,須要須要保存對象,還須要實現對象的解析器,這裏使用Fastjson做爲實例:
public class MyApplication extends Application{ @Override public void onCreate() { super.onCreate(); AptPreferencesManager.init(this, new AptParser() { @Override public Object deserialize(Class clazz, String text) { return JSON.parseObject(text,clazz); } @Override public String serialize(Object object) { return JSON.toJSONString(object); } }); } }
若是app支持多用戶登陸,須要根據不用的用戶持久化,能夠經過下面方法配置。再經過@AptField(global = false),就能夠針對某個字段跟隨用戶不一樣進行持久化。
AptPreferencesManager.setUserInfo("uid");
// 普通類型保存 SettingsPreferences.get().setUseLanguage("zh"); SettingsPreferences.get().setLastOpenAppTimeMillis(System.currentTimeMillis()); // 對象類型保存 Settings.LoginUser loginUser = new Settings.LoginUser(); loginUser.setUsername("username"); loginUser.setPassword("password"); SettingsPreferences.get().setLoginUser(loginUser); // 對象類型帶 @AptField(preferences = true) 註解的保存,至關於把 push相關的放在一個分類 SettingsPreferences.get().getPush().setOpenPush(true); // 獲取 String useLanguage = settingsPreference.getUseLanguage(); Settings.LoginUser loginUser1 = settingsPreference.getLoginUser(); boolean openPush = settingsPreference.getPush().isOpenPush();
不少時候咱們須要在沒有獲取到值時使用默認值,SharedPreferences自己也是具有默認值的,因此咱們也是支持默認值配置。分析生成的代碼能夠看到:
@Override public long getLastOpenAppTimeMillis() { return mPreferences.getLong("lastOpenAppTimeMillis", super.getLastOpenAppTimeMillis()); }
若是沒有獲取到值,會調用父類的方法,那麼就能夠經過這個方式配置默認值:
@AptPreferences public class Settings { // 使用commit提交,默認是使用apply提交,配置默認值 @AptField(commit = true) private String useLanguage = "zh"; // ... }
@AptPreferences public class Settings { private long lastOpenAppTimeMillis; // 使用commit提交,默認是使用apply提交,配置默認值 @AptField(commit = true) private String useLanguage = "zh"; // 使用preferences的方式保存 @AptField(preferences = true) private Push push; // 使用對象的方式保存 private LoginUser loginUser; // 不持久化該字段,僅僅保留在內存 @AptField(save = false) private long lastActionTimeMillis; public long getLastActionTimeMillis() { return lastActionTimeMillis; } public void setLastActionTimeMillis(long lastActionTimeMillis) { this.lastActionTimeMillis = lastActionTimeMillis; } public LoginUser getLoginUser() { return loginUser; } public void setLoginUser(LoginUser loginUser) { this.loginUser = loginUser; } public long getLastOpenAppTimeMillis() { return lastOpenAppTimeMillis; } public void setLastOpenAppTimeMillis(long lastOpenAppTimeMillis) { this.lastOpenAppTimeMillis = lastOpenAppTimeMillis; } public String getUseLanguage() { return useLanguage; } public void setUseLanguage(String useLanguage) { this.useLanguage = useLanguage; } public Push getPush() { return push; } public void setPush(Push push) { this.push = push; } public static class Push { private boolean openPush; private boolean vibrate; private boolean voice; public boolean isOpenPush() { return openPush; } public void setOpenPush(boolean openPush) { this.openPush = openPush; } public boolean isVibrate() { return vibrate; } public void setVibrate(boolean vibrate) { this.vibrate = vibrate; } public boolean isVoice() { return voice; } public void setVoice(boolean voice) { this.voice = voice; } } public static class LoginUser implements Serializable{ public String username; public String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } }
實際上就是根據上面的代碼自動生成帶有持久化的代碼,能夠在這裏能夠找到
app/build/generated/source/apt/debug
public final class SettingsPreferences extends Settings { public static final Map<String, SettingsPreferences> sMap = new java.util.HashMap<>(); private final SharedPreferences.Editor mEdit; private final SharedPreferences mPreferences; private final String mName; public SettingsPreferences(String name) { mPreferences = AptPreferencesManager.getContext().getSharedPreferences("Settings_" + name, 0); mEdit = mPreferences.edit(); this.mName = name; this.setPush(new PushPreferences()); } @Override public Settings.LoginUser getLoginUser() { String text = mPreferences.getString("loginUser", null); Object object = null; if (text != null) { object = AptPreferencesManager.getAptParser().deserialize(com.thejoyrun.aptpreferences.Settings.LoginUser.class, text); } if (object != null) { return (com.thejoyrun.aptpreferences.Settings.LoginUser) object; } return super.getLoginUser(); } @Override public void setLoginUser(Settings.LoginUser loginUser) { mEdit.putString("loginUser", AptPreferencesManager.getAptParser().serialize(loginUser)).apply(); } @Override public long getLastOpenAppTimeMillis() { return mPreferences.getLong("lastOpenAppTimeMillis", super.getLastOpenAppTimeMillis()); } @Override public void setLastOpenAppTimeMillis(long lastOpenAppTimeMillis) { mEdit.putLong("lastOpenAppTimeMillis", lastOpenAppTimeMillis).apply(); } @Override public String getUseLanguage() { return mPreferences.getString("useLanguage", super.getUseLanguage()); } @Override public void setUseLanguage(String useLanguage) { mEdit.putString("useLanguage", useLanguage).commit(); } public static SettingsPreferences get(String name) { if (sMap.containsKey(name)) { return sMap.get(name); } synchronized (sMap) { if (!sMap.containsKey(name)) { SettingsPreferences preferences = new SettingsPreferences(name); sMap.put(name, preferences); } } return sMap.get(name); } public static SettingsPreferences get() { return get(""); } public void clear() { mEdit.clear().commit(); sMap.remove(mName); } public static void clearAll() { java.util.Set<String> keys = sMap.keySet(); for (String key : keys) { sMap.get(key).clear(); } } private class PushPreferences extends Settings.Push { @Override public boolean isOpenPush() { return mPreferences.getBoolean("Push.openPush", super.isOpenPush()); } @Override public void setOpenPush(boolean openPush) { mEdit.putBoolean("Push.openPush", openPush).apply(); } @Override public boolean isVibrate() { return mPreferences.getBoolean("Push.vibrate", super.isVibrate()); } @Override public void setVibrate(boolean vibrate) { mEdit.putBoolean("Push.vibrate", vibrate).apply(); } @Override public boolean isVoice() { return mPreferences.getBoolean("Push.voice", super.isVoice()); } @Override public void setVoice(boolean voice) { mEdit.putBoolean("Push.voice", voice).apply(); } } }
Copyright 2016 Joyrun, Inc. 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 http://www.apache.org/licenses/LICENSE-2.0 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.