ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - mathcamp/fiberglass: Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties
Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties - mathcamp/fiberglass
Visit Site

GitHub - mathcamp/fiberglass: Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties

GitHub - mathcamp/fiberglass: Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties

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.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory