Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties

Related tags

Kotlin fiberglass
Overview

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.

You might also like...
🚟 Lightweight, and simple scheduling library made for Kotlin (JVM)
🚟 Lightweight, and simple scheduling library made for Kotlin (JVM)

Haru 🚟 Lightweight, and simple scheduling library made for Kotlin (JVM) Why did you build this? I built this library as a personal usage library to h

Lightweight Kotlin DSL dependency injection library
Lightweight Kotlin DSL dependency injection library

Warehouse DSL Warehouse is a lightweight Kotlin DSL dependency injection library this library has an extremely faster learning curve and more human fr

A fast, lightweight, entity component system library written in Kotlin.

Fleks A fast, lightweight, entity component system library written in Kotlin. Motivation When developing my hobby games using LibGDX, I always used As

Kotools Types - a lightweight library that provides commonly used types for Kotlin

Kotools Types is a lightweight library that provides commonly used types for Kotlin

Lightweight compiler plugin intended for Kotlin/JVM library development and symbol visibility control.

Restrikt A Kotlin/JVM compiler plugin to restrict symbols access, from external project sources. This plugin offers two ways to hide symbols: An autom

A Lightweight PDF Viewer Android library which only occupies around 125kb while most of the Pdf viewer occupies up to 16MB space.
A Lightweight PDF Viewer Android library which only occupies around 125kb while most of the Pdf viewer occupies up to 16MB space.

Pdf Viewer For Android A Simple PDF Viewer library which only occupies around 125kb while most of the Pdf viewer occupies upto 16MB space. How to inte

Lightweight data loading and caching library for android

ColdStorage A lightweight data loading and caching library for android Quicklinks Feature requests: Got a new requirement? Request it here and it will

A lightweight, simple, smart and powerful Android routing library.

RxRouter Read this in other languages: δΈ­ζ–‡, English A lightweight, simple, smart and powerful Android routing library. Getting started Setting up the d

Clickstream - A Modern, Fast, and Lightweight Android Library Ingestion Platform.
Clickstream - A Modern, Fast, and Lightweight Android Library Ingestion Platform.

Clickstream is an event agnostic, real-time data ingestion platform. Clickstream allows apps to maintain a long-running connection to send data in real-time.

Owner
null
[Android Library] A SharedPreferences helper library to save and fetch the values easily.

Preference Helper A SharedPreferences helper library to save and fetch the values easily. Featured in Use in your project Add this to your module's bu

Naveen T P 13 Apr 4, 2020
A Kotlin library for reactive and boilerplate-free SharedPreferences in Android

KPreferences A Kotlin library for reactive and boilerplate-free Shared Preferences in Android. With KPreferences you can use Kotlin's marvelous delega

Mohamad Amin Mohamadi 19 Dec 16, 2020
The most complete and powerful data-binding library and persistence infra for Kotlin 1.3, Android & Splitties Views DSL, JavaFX & TornadoFX, JSON, JDBC & SQLite, SharedPreferences.

Lychee (ex. reactive-properties) Lychee is a library to rule all the data. ToC Approach to declaring data Properties Other data-binding libraries Prop

Mike 112 Dec 9, 2022
Kotpref - Android SharedPreferences delegation library for Kotlin

Kotpref Android SharedPreference delegation for Kotlin. Install repositories { mavenCentral() } dependencies { // core implementation 'co

Takao Chiba 684 Dec 22, 2022
Android Library to make SharedPreferences usage easier.

KotlinPreferences Kotlin Android Library, that makes preference usage simple and fun. KotlinPreferences now have a brother. With KotlinPreferences, yo

Marcin MoskaΕ‚a 50 Nov 6, 2022
Adapter library for SharedPreferences

EasyPrefs Adapter library for SharedPreferences which reduces boilerplate needed to store simple data, but open enough to not interfere with your own

Kacper Wojciechowski 6 Nov 23, 2021
Android SharedPreferences Helper

PocketDB Android SharedPreferences Helper This is SharedPreferences Helper like a database noSql. Support AES encryption Latest Version Download depen

Muhammad Utsman 9 Jun 20, 2021
Koi, a lightweight kotlin library for Android Development.

Koi - A lightweight Kotlin library for Android Koi include many useful extensions and functions, they can help reducing the boilerplate code in Androi

Hello World 514 Nov 29, 2022
A lightweight and simple Kotlin library for deep link handling on Android πŸ”—.

A lightweight and simple Kotlin library for deep link handling on Android ??.

Jeziel Lago 101 Aug 14, 2022
A lightweight cache library written in Kotlin

[NEW] Released to Maven Central: 'com.github.yundom:kache:1.x.x' Kache A runtime in-memory cache. Installation Put this in your build.gradle implemen

Dennis 22 Nov 19, 2022