1 Star 0 Fork 0

李冉 / Treasure

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Treasure

Download Android Arsenal

English document

Treasure是一个Android平台上基于SharePreferences的偏好存储库,只需要定义接口,无需编写实现。运行时0反射,不仅使用方便而且性能和原生写法几乎无差别。

使用方法

1、添加依赖

Gradle

compile 'com.baoyz.treasure:treasure:0.3.1'
provided 'com.baoyz.treasure:treasure-compiler:0.3.1'
2、定义接口
@Preferences
public interface SimplePreferences {

    String getUsername();

    void setUsername(String username);

}

我们定义了一个interface,需要使用@Preferences注解进行声明。然后可以定义一系列的getset方法,用于获取和设置值。方法名会作为存储的key,例如getUsername()setUsername()key就是username,也就是通过setUsername()设置的value可以通过getUsername()获取到,因为他们的key是一样的。

3、实例化
SimplePreferences preferences = Treasure.get(context, SimplePreferences.class);
preferences.setUsername("Hello Treasure!");
preferences.getUsername(); // return "Hello Treasure!"

通过Treasure.get()方法可以获取指定的Preferences对象,之后可以调用set方法设置值,通过对应的get方法获取值。

高级用法

多文件

可以为一个Preferences生成多个文件,例如多账号管理,不同账号有不同Preferences

Treasure.get(context, SimplePreferences.class, "id_one");
Treasure.get(context, SimplePreferences.class, "id_two");

Treasure提供了一个重载的get方法,可以传入一个String类型的ID,不同ID返回的Preferences对象不同,保存的文件也不同。(感谢好基友zzz40500提出的建议)

默认值

@Default注解可以指定get方法的默认值。

@Default("Hello Treasure!")
String getUsername();

@Default("false")
boolean isLogin();

// default is 1 hour
@Default("1000 * 60 * 60")
long getTimeout();

@Default({"hello", "world", "!"})
Set<String> getStringSet();

如果没有指定@Default那么默认值见下表。

返回值类型 默认值
int 0
float 0f
long 0l
boolean false
String null
Set<String> null

提交类型

设置值的默认提交类型是edit().apply(),如果想使用edit().commit()有三种方式。

1、全局
@Preferences(edit = Preferences.Edit.COMMIT)
public interface SimplePreferences

全局指定提交方式之后,所有的set方法都会以commit()方法提交数据。

2、注解指定方法
@Commit
void setUsername(String username);

使用@Commit注解只对当前方法有效。

3、指定方法返回boolean
boolean setUsername(String username);

无论上面两种方式有没有设置,只要set方法的返回值是boolean,那么这个方法就会以commit()方法提交,并且返回commit()的结果。

移除数据

@Remove
void removeUsername();

@Remove
void deleteTimeout();

使用@Remove注解修饰方法,调用方法移除对应key的数据。

清空数据

@Clear
void clear();

可以声明一个方法,使用@Clear注解修饰,那么调用这个方法就会清空整个Preferences的数据。

关于方法名

如果方法名以getsetputisremovedelete开头,那么会忽略这些前缀并且全部小写作为key,如果不包含这些前缀,那么方法名全部小写会作为key

Proguard

Treasure运行时0反射,不需要添加Proguard配置。

致谢

Favor(灵感来源)

javapoet

License

The MIT License (MIT)

Copyright (c) 2015 baoyongzhang <baoyz94@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

空文件

简介

非常简单好用的 Android SharePreferences 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/liran/Treasure.git
git@gitee.com:liran/Treasure.git
liran
Treasure
Treasure
master

搜索帮助