Skip to content

twiceyuan/RetroPreference

Repository files navigation

RetroPreference

SharedPreferences wrapper inspired by Retrofit,类型安全的 SharedPreferences 工具。

  • Type Safe
  • Key 强一致
  • 混淆代码同时也会混淆 SharedPreferences 的文件 name 和 key name,并且不影响使用[1]
  • 代理类实例的缓存
  • Serializable 对象存储
  • 代理类定义合法性预检查

Sample Code

Define:

// 需要调用 clear 方法清空 sp 的话可以继承 Clearable 接口
public interface Settings extends Clearable {

    Preference<Integer> launch_count();

    Preference<Boolean> is_login();

    Preference<Float> user_points();

    Preference<Long> last_login();

    Preference<String> username();

    Preference<Set<String>> user_tags();
}

Save or read:

// 建议在 Application onCreate 时创建唯一实例。
mSettings = RetroPreference.createKt(appContext, Settings.class, Context.MODE_PRIVATE);
// 保存启动次数
mSettings.launche_count().set(7);
// 获得存储的值
mSettings.launche_count().getWithDefault();
// or
mSettings.launche_count().getWithDefault(100); // 100 为默认值

更多用例参考测试用例

引用

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

// module
dependencies {
    compile 'com.github.twiceyuan:RetroPreference:[last-version]'
}

[1] 仅在注解时有效,后续会添加自定义混淆规则的选项。或者在proguard文件中keep相关的SharedPreferences定义项。

License

Copyright 2016 twiceYuan.

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.