Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.idea Colorscheme and icons Jun 27, 2016
app Colorscheme and icons Jun 27, 2016
gradle/wrapper Initial commit Jun 22, 2016
lib Initial commit Jun 22, 2016
.gitignore Initial commit Jun 22, 2016
README.md Fix image Jun 27, 2016
build.gradle Initial commit Jun 22, 2016
gradle.properties Initial commit Jun 22, 2016
gradlew Initial commit Jun 22, 2016
gradlew.bat Initial commit Jun 22, 2016
icon.png Update readme assets Jun 27, 2016
logo.png README updates Jun 27, 2016
screenshot.png Update readme assets Jun 27, 2016
settings.gradle Initial commit Jun 22, 2016

README.md

Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties


Idea

Delegated properties in Kotlin allow you to execute arbitrary code whenever a field is accessed. The cool thing about delegated properties is that we can package up the code and reuse it.

Let's package up the notion of getting from SharedPreferences and writing to SharedPreferences.

Usage

Just stick metadata in your classes with specific delegated properties

class User(val ctx: Context) {
  companion object {
    private val namespace = "user"
  }

  var email by StringSavable(namespace, ctx)
  var lastSaved by DateSavable(namespace, ctx)
  var phone by LongSavable(namespace, ctx)
}

Get and set just like normal field properties

val u = User(this)
/* ... */

// Get
val greetings = "Hello ${u.name}"

/* ... */

// Set
u.name = "Brandon"

If you want to save custom a custom data type, you just have to write-to and read-from SharedPreferences

private object DateGetterSetter {
  // make it a lazy object to reuse the same inner-class for each savable
  val dates by lazy {
    object: GetterSetter<Date> {
      override fun get(prefs: SharedPreferences, name: String): Date =
          Date(prefs.getLong(name, System.currentTimeMillis()))

      override fun put(edit: SharedPreferences.Editor, name: String, value: Date) {
        edit.putLong(name, value.time)
      }
    }
  }
}

fun DateSavable(namespace: String, ctx: Context): Savable<Date> =
    Savable(namespace, ctx, DateGetterSetter.dates)

Example App

See the app module for a sample app

fiberglass app icon

screenshot

Default values

Shared preferences requires default values when the data is missing. The default values for the builtin savables are:

Type Default Value
StringSavable ""
IntSavable Int.MIN_VALUE
LongSavable Long.MIN_VALUE
DoubleSavable Double.MIN_VALUE
FloatSavable Float.MIN_VALUE
BooleanSavable false
DateSavable Date now

Consistency

Savable field writes are eventually consistent (implemented with apply() on the shared preferences editor).

It would make sense to also support consistent writes (implemented with commit() on the shared preferences editor) somehow. Please send a PR or open an issue with ideas.