Skip to content
💾 Local settings library for JVM and Android
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
art
buildSrc
defaults-android
defaults-annotations
defaults-compiler
defaults
demo
gradle/wrapper
website
.gitignore
LICENSE
README.md
ROADMAP.md
build.gradle.kts
gradle.properties
gradlew
gradlew.bat
settings.gradle.kts

README.md

bintray download license

Defaults

icon

Local settings library that runs in plain Java and Android. Comes with optional annotation processor to bind properties with existing settings.

JVM
val defaults = preferences.toDefaults()
val username = defaults["username"]
Android
val defaults = toDefaults() // if this is context/activity, or use `sharedPreferences.toDefaults `
val username = defaults["username"]

Download

All artifacts should be linked to JCenter, otherwise add maven url https://dl.bintray.com/hendraanggrian/defaults.

repositories {
    jcenter()
}
dependencies {
    compile "com.hendraanggrian.defaults:defaults:$version"
    api "com.hendraanggrian.defaults:defaults-android:$version" // for Android

    // optional property binding, use kapt when necessary
    annotationProcessor "com.hendraanggrian.defaults:defaults-compiler:$version"
}

Usage

Manual get/set

Create defaults instance from File, or SharedPreferences in Android.

import com.hendraanggrian.defaults.toDefaults

// file defaults can set/get
val fileDefaults = file.toDefaults()
val name = fileDefaults["name"]
val age = fileDefaults.getInt("age", 0)
fileDefaults["name"] = "Hendra"
fileDefaults["age"] = 25

// shared preferences must open to set
val androidDefaults = context.toDefaults()
androidDefaults {
    it["name"] = "Hendra"
    it["age"] = 25
}

Bind properties

With optional annotation processor, bind these local settings to local variables.

import com.hendraanggrian.defaults.DefaultsSaver
import com.hendraanggrian.defaults.BindDefault
import com.hendraanggrian.defaults.bindDefaults

@BindDefault lateinit var name: String
@BindDefault @JvmField var age: Int = 0

lateinit var saver: DefaultsSaver

init {
    saver = file.bindDefaults(this)
}

fun applyChanges(name: String, age: Int) {
    this.name = name
    this.age = age
    saver.saveAsync()
}

License

Copyright 2019 Hendra Anggrian

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.
You can’t perform that action at this time.