Functions to help with checking your app's online status
This is a Kotlin-only library which relies heavily on generics and type inference to make writing to and reading from Android SharedPreferences as easy as possible. For example:
val prefs = Prefs(this)
prefs.set("key_name", "Flat Circle")
val ourName: String = prefs.get("key_name")
implementation 'io.flatcircle:preferenceshelper:{version}'
You can set and get any primitive or basic custom class into your sharedPreferences via PreferencesHelper. The supported classes are the same as Moshi's supported classes since it uses moshi to serialized. The supported functions are:
Function, PreferencesHelper. | Description | Example |
---|---|---|
set(context, key, value) | Saves a given value to sharedPreferences, where [value] is any primitive, or a custom class that can be serialized by Moshi into Json | Example |
setSync(context, key, value) | Same as set(), but uses preferencesEditor.commit() to keep the writing operation onto the same thread. | Example |
get<Any?>(context, key, defaultValue?) | Returns any value you've saved, with optional DefaultValue. | Example |
getSafely<Any?>(context, key, defaultValue) | Returns any value you've saved, with mandatory DefaultValue for null-safety. | Example |
contains(context, key) | determines whether a value has been saved to sharedPreferences | Example |
setAndEncrypt(context, key, value) | Saves a given string as an encrypted string using the Android KeyStore to perform the encryption. Requires you to set a KeyStore Alias as below | Example |
getEncrypted(context, key) | Returns a decrypted string which was saved as an encrypted string. | Example |
addCustomAdapter(adapter) | Adds a custom moshi adapter for a given class | Example |
If you are using custom classes and adapters and there is nothing to get, you will get Null Pointer exceptions unless you pass a defaultValue or use .getSafely() when you get your custom class.
The PreferencesHelper library also comes with basic KeyStore management for encryption/decryption purposes. The actual encryption works, but uses a currently deprecated method of generating a keypair that needs to be double-checked. You can encrypt and decrypt strings as follows:
Function, KeyStoreHelper. | Description | Example |
---|---|---|
setAlias("YourAppAlias") | Sets a KeyStore alias for your app, in order to use encryption. Do this in your Application{onCreate(){}} | Example |
encryptString(context, string) | Returns an encrypted version of a given string | Example |
decryptString(context, string) | Decrypts a given string that was encrypted with above method. | Example |
You can also use the Prefs(context) class if you don't want to pass the context every time. It requires that you .clear() the Prefs() class at the end of your app/activity lifecycle to avoid memory leaks, so use with care.
You can see an example of the Prefs class being instantiated, used and cleared Here
I know it's tempting to think you can use this as a replacement for a database, but please don't. When you start to serialize lists of custom classes you should really just create a database module to handle that.
You can use delegates to set up a variable which automatically loads from sharedPreferences and automatically saves to SharedPreferences every time that it changes.
val prefs = Prefs(this)
var name = ObservablePreferences(prefs, "name_key", "Jack")
println(name) // prints "Smith", unless your SharedPreferences is empty, then it prints "Jack"
name = "Smith"
println(name) //prints "Smith"