Giter Club home page Giter Club logo

retropreference's Introduction

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.create(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.

retropreference's People

Contributors

twiceyuan avatar

Watchers

薛晋伟 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.