Preferences

sealed class Preferences : SerialFormat

Serializes and deserializes class properties into SharedPreferences consisting of string keys and primitive type values.

@Serializable
data class Person(val name: String, val age: Int)

val preferences = Preferences(sharedPreferences)
val abby = Person("Abby", 20)

preferences.encode("person", abby)

assertEquals("Abby", sharedPreferences.getString("person.name", null))
assertEquals(20, sharedPreferences.getInt("person.age", 0))

Functions

Link copied to clipboard
fun <T> decode(deserializer: DeserializationStrategy<T>, tag: String): T

Decodes and deserializes from the SharedPreferences at the specified tag to the value of type T using the given deserializer

Link copied to clipboard
fun <T> decodeOrDefault(deserializer: DeserializationStrategy<T>, tag: String, default: T): T

Decodes and deserializes from the SharedPreferences at the specified tag to the value of type T using the given deserializer

Link copied to clipboard
fun <T> encode(serializer: SerializationStrategy<T>, tag: String, value: T)

Serializes and encodes the given value into the SharedPreferences at the specified tag using the given serializer.

Properties

Link copied to clipboard

Contains all serializers registered by format user for Contextual and Polymorphic serialization.

Extensions

Link copied to clipboard
fun <T> Preferences.asProperty(serializer: KSerializer<T>, tag: String? = null): ReadWriteProperty<Any?, T>
fun <T> Preferences.asProperty(serializer: KSerializer<T>, tag: String? = null, default: T): ReadWriteProperty<Any?, T>
inline fun <T> Preferences.asProperty(tag: String? = null): ReadWriteProperty<Any?, T>
inline fun <T> Preferences.asProperty(tag: String? = null, default: T): ReadWriteProperty<Any?, T>

Encodes changes to the delegated property into the SharedPreferences and decodes the current value from them.

Link copied to clipboard
inline fun <T> Preferences.decode(tag: String): T

Decodes and deserializes from the SharedPreferences at the specified tag to the value of type T using deserializer retrieved from the reified type parameter.

Link copied to clipboard
inline fun <T> Preferences.decodeOrDefault(tag: String, default: T): T

Decodes and deserializes from the SharedPreferences at the specified tag to the value of type T using deserializer retrieved from the reified type parameter.

Link copied to clipboard
inline fun <T> Preferences.encode(tag: String, value: T)

Serializes and encodes the given value into the SharedPreferences at the specified tag using serializer retrieved from the reified type parameter.