项目作者: calintat

项目描述 :
Android preferences made easy
高级语言: Kotlin
项目地址: git://github.com/calintat/alps.git
创建时间: 2017-03-14T23:04:38Z
项目社区:https://github.com/calintat/alps

开源协议:MIT License

下载


Alps

Alps is a minimal Android library for reading and writing to your app’s shared preferences.

Installation

Add JitPack to the list of repositories:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url "https://jitpack.io" }
  5. }
  6. }

Add Alps as a dependency in the app build file:

  1. dependencies {
  2. compile "com.github.calintat.alps:alps-core:2.0.2"
  3. }

Features

Get & put methods

The easiest way to access your app’s preferences is with the get and put methods:

  1. String preference = getString("key", "default_value");
  2. preference = "updated_value"; putString("key", preference);

Property delegates

In Kotlin, you can instead use property delegates, for example:

  1. var preference by stringPref("key", "default_value")
  2. preference = "updated_value" // will also update the preference value

Populate with preferences

Usually your settings activity will contain a PreferenceFragment inside a container.

With Alps, you can easily add a PreferenceFragment generated from an XML preference file:

  1. public class SettingsActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. ...
  5. populateWithPreferences(R.id.container, R.xml.preferences);
  6. }
  7. }

Define preferences with the Alps DSL

In Kotlin, you can also use a DSL to define your preferences and add them to your settings activity:

  1. dependencies {
  2. compile "com.github.calintat.alps:alps-dsl:2.0.2"
  3. }
  1. class SettingsActivity : Activity() {
  2. override onCreate(savedInstanceState: Bundle?) {
  3. ...
  4. populateWithPreferences(R.id.container) {
  5. preferenceCategory {
  6. title = "Alps"
  7. switchPreference(key = "pref_alps_dsl") {
  8. defaultValue = true
  9. title = "Use the Alps DSL"
  10. summary = "Kotlin domain-specific language for defining preferences"
  11. }
  12. }
  13. }
  14. }
  15. }