RxPersistence是基于面向?qū)ο笤O(shè)計(jì)的快速持久化框架按咒,目的是為了簡(jiǎn)化SharePreferences及本地緩存Cache的使用挖帘,減少代碼的編寫(xiě)瞒窒∞嗥В可以非常快速地保存基本類型和對(duì)象崇裁。RxPersistence是基于APT技術(shù)實(shí)現(xiàn)陵像,在編譯期間實(shí)現(xiàn)代碼的生成,支持混淆寇壳。根據(jù)不同的用戶區(qū)分持久化信息醒颖。
特點(diǎn)
把通過(guò)的Javabean變成SharedPreferences操作類
把通過(guò)的Javabean變成Cache操作類
支持保存基本類型及對(duì)象
支持根據(jù)不同的用戶區(qū)分持久化信息
支持Rxjava
可支持自定義緩存方式,如內(nèi)存緩存,磁盤緩存及二級(jí)緩存
簡(jiǎn)單例子
定義javabean類
@CacheEntity(mode = CacheMode.MEMORY,global = true, memoryMaxCount = 300,diskMaxSize = 400)
public class UserTest {
@CacheField(saveTime = 200,save = true)
private String name;
@CacheField(save = true)
private double num;
private int age;
private Bitmap bitmap;
private Drawable drawable;
private List<String> list;
@CacheField(saveTime = 200,save = true)
@SPEntity(global = false)
public class UserProfile {
@SPField(commit = true,save = true)
private String name;
@SPField(commit = true,save = true,global = false)
private double num;
private int age;
使用方式
//初始化
RxPersistence.init(this, new PersistenceParser() {
@Override
public Object deserialize(Type clazz, String text) {
return new Gson().fromJson(text,clazz);
}
@Override
public String serialize(Object object) {
return new Gson().toJson(object);
}
});
RxPersistence.setUserGroups("用戶組ID");//可選配置
RxPersistence.setUserTokens("用戶ID");//可選配置
// 保存信息
UserTestMemoryCache.get().setName()
UserProfileSP.get().setAge();
UserProfileSP.get().setAgeRx();
// 獲取信息
UserTestMemoryCache.get().getName()
UserProfileSP.get().getAge();
UserProfileSP.get().getAgeRx();
從上面的簡(jiǎn)單例子可以看到,我們需要做SharePreferences持久化壳炎,僅僅定義一個(gè)簡(jiǎn)單的javabean類(UserProfileSP)并添加注解即可泞歉,這個(gè)框架會(huì)根據(jù)javabean生成帶有持久化功能的UserProfile 類,通過(guò)這個(gè)類就可以非常簡(jiǎn)單去保持或者獲取數(shù)據(jù)匿辩,大大簡(jiǎn)化了SharePreferences的使用腰耙,也可以保持對(duì)象。
項(xiàng)目地址
https://github.com/TanZhiL/RxPersistence
一铲球、配置項(xiàng)目
配置項(xiàng)目根目錄 build.gradle
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
配置app build.gradle
apply plugin: 'com.android.application'
//...
dependencies {
implementation 'com.github.TanZhiL.RxPersistence:rxpersistence:0.0.5'
annotationProcessor 'com.github.TanZhiL.RxPersistence:rxpersistence-compiler:0.0.5'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
}
二挺庞、定義持久化Javabean
使用方法非常簡(jiǎn)單,先編寫(xiě)一個(gè)普通帶getter稼病、setter的javabean類选侨,在類頭部添加@SPEntity或者@CacheEntity:
//Sharepreference
@SPEntity(global = false)
public class UserProfile {
@SPField(commit = true,save = true)
private String name;
@SPField(commit = true,save = true,global = false)
private double num;
private int age;
// ...
// get掖鱼、set方法必須寫(xiě)
}
//Cache
@CacheEntity(mode = CacheMode.MEMORY,global = true, memoryMaxCount = 300,diskMaxSize = 400)
public class UserTest {
@CacheField(saveTime = 200,save = true)
private String name;
@CacheField(save = true)
private double num;
private int age;
private Bitmap bitmap;
private Drawable drawable;
三、注解及使用說(shuō)明
請(qǐng)參考注解注釋,及simple
四援制、初始化
使用之前要進(jìn)行初始化戏挡,建議在Application進(jìn)行初始化,需要需要保存對(duì)象晨仑,還需要實(shí)現(xiàn)對(duì)象的解析器褐墅,這里使用Gson作為實(shí)例:
//初始化
RxPersistence.init(this, new PersistenceParser() {
@Override
public Object deserialize(Type clazz, String text) {
return new Gson().fromJson(text,clazz);
}
@Override
public String serialize(Object object) {
return new Gson().toJson(object);
}
});
RxPersistence.setUserGroups("用戶組ID");//可選配置
RxPersistence.setUserTokens("用戶ID");//可選配置
五、根據(jù)不同的用戶設(shè)置
如果app支持多用戶登錄洪己,需要根據(jù)不用的用戶持久化妥凳,可以通過(guò)下面方法配置。再通過(guò)@SPField(global = false)答捕,就可以針對(duì)某個(gè)字段跟隨用戶不同進(jìn)行持久化猾封。
RxPersistence.setUserGroups("用戶組ID");//可選配置
RxPersistence.setUserTokens("用戶ID");//可選配置
六、代碼調(diào)用
// 普通類型保存
SettingsPreferences.get().setUseLanguage("zh");
SettingsPreferences.get().setLastOpenAppTimeMillis(System.currentTimeMillis());
// 對(duì)象類型保存
Settings.LoginUser loginUser = new Settings.LoginUser();
loginUser.setUsername("username");
loginUser.setPassword("password");
SettingsPreferences.get().setLoginUser(loginUser);
// 獲取
String useLanguage = settingsPreference.getUseLanguage();
Settings.LoginUser loginUser1 = settingsPreference.getLoginUser();
boolean openPush = settingsPreference.getPush().isOpenPush();
七噪珊、默認(rèn)值
很多時(shí)候我們需要在沒(méi)有獲取到值時(shí)使用默認(rèn)值,SharedPreferences本身也是具備默認(rèn)值的齐莲,所以我們也是支持默認(rèn)值配置痢站。分析生成的代碼可以看到:
@Override
public long getLastOpenAppTimeMillis() {
return mPreferences.getLong("lastOpenAppTimeMillis", super.getLastOpenAppTimeMillis());
}
如果沒(méi)有獲取到值,會(huì)調(diào)用父類的方法选酗,那么就可以通過(guò)這個(gè)方式配置默認(rèn)值:
@SPEntity
public class Settings {
// 使用commit提交阵难,默認(rèn)是使用apply提交,配置默認(rèn)值
@SPField(commit = true)
private String useLanguage = "zh";
// ...
}
致謝
感謝所有開(kāi)源庫(kù)的大佬
問(wèn)題反饋
歡迎加星芒填,打call https://github.com/TanZhiL/RxPersistence
- email:1071931588@qq.com
關(guān)于作者
譚志龍
開(kāi)源項(xiàng)目
- 快速切面編程開(kāi)源庫(kù) https://github.com/TanZhiL/OkAspectj
- 一款解決使用newInstance創(chuàng)建fragment定義key傳值問(wèn)題的apt框架 https://github.com/TanZhiL/FragmentKey
- 高仿喜馬拉雅聽(tīng)Android客戶端 https://github.com/TanZhiL/Zhumulangma
- 骨架屏彈性塊 https://github.com/TanZhiL/SkeletonBlock
- RxPersistence是基于面向?qū)ο笤O(shè)計(jì)的快速持久化框架 https://github.com/TanZhiL/RxPersistence
License
Copyright 2019 Thomas, 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.