Skip to content

Extended Shared Preferences. Kotlin library that allows you to save arrays of primitives and any Serializable data classes.

License

Notifications You must be signed in to change notification settings

muddassir235/eprefs

Repository files navigation

Eprefs

Release

Extended Shared Preferences. Kotlin library that allows you to save and load arrays of primitives and any data classes.

Used in https://play.google.com/store/apps/details?id=com.muddassirkhan.quran_android

Add Dependencies

Add the following in your project level build.gradle

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

and the following in your app level build.gradle

dependencies {
    implementation 'com.github.muddassir235:eprefs:1.9'
}

How to Use

Save to SharedPreferences

Save any boolean, int, long, float, string, or any array of (boolean, int, long, float, string) or any data Object in SharedPreferences from within a Context (e.g. Activity, Service, Application e.t.c.). Can also be called using context.save/context.load or sharedPreferences.save/sharedPreferences.load. (Throws unsupported type exception in case the object is not a data object.)

save("mykey", true)                             // Boolean
save("mykey", 1)                                // Int
save("mykey", 1L)                               // Long
save("mykey", 1.0f)                             // Float
save("mykey", "My String")                      // String
save("mykey", arrayOf("my", "strings"))         // Arrays of all primitive types are supported
.
.
.
save("mykey", anyDataObject)            // Any data object can be saved.

Load from SharedPreferences

Load any saved boolean, int, long, float, string, or array of (boolean, int, long, float, string) or any data object from SharedPreferences within a Context. (Throws unsupported type exception in case the object is not a data object.)

val value = load<Boolean>("mykey")
val value = load<Int>("mykey")
val value = load<Long>("mykey")
val value = load<Float>("mykey")
val value = load<String>("mykey")
val value = load<MyDataObject>("mykey")
val value = load<Array<Boolean>>("mykey")
.
.
.
val value = load<Array<MyDataObject>>("mykey")

Delete from SharedPreferences

Load any saved boolean, int, long, float, string, or array of (boolean, int, long, float, string) or any data object from SharedPreferences within a Context. (Throws unsupported type exception in case the object is not data object.)

delete<Boolean>("mykey")
delete<Int>("mykey")
delete<Long>("mykey")
delete<Float>("mykey")
delete<String>("mykey")
delete<MyDataObject>("mykey")
delete<Array<Boolean>>("mykey")
.
.
.
delete<Array<MyDataObject>>("mykey")

Safe Save, Load and Delete

Safe versions of the above save and load functions which avoid/ignore any exceptions.

safeSave("mykey", myObject) // Does nothing if it fails
safeLoad<MyObject>("mykey") // Returns null in case any exception occurs
safeDelete<MyObject>("mykey") // Does nothing if it fails

Get a reference to default shared preferences from within a context (e.g. Activity, Service, Application)

this.prefs

Muddassir Ahmed Links: