Preferences
-
Cmn
abstract class Preferences
MutablePreferences |
Mutable version of |
Preferences and MutablePreferences are a lot like a generic Map and MutableMap keyed by the Preferences.Key class. These are intended for use with DataStore. Construct a DataStore<Preferences> instance using PreferenceDataStoreFactory.create.
Summary
Nested types |
|---|
class Preferences.Key<T : Any?>Key for values stored in Preferences. |
class Preferences.Pair<T : Any?>Key Value pairs for Preferences. |
Public functions |
||
|---|---|---|
abstract Map<Preferences.Key<*>, Any> |
asMap()Retrieve a map of all key preference pairs. |
Cmn
|
abstract operator Boolean |
<T : Any?> contains(key: Preferences.Key<T>)Returns true if this Preferences contains the specified key. |
Cmn
|
Preferences |
copy(block: (MutablePreferences) -> Unit)Creates a new read-only Preferences with the changes from |
Cmn
|
abstract operator T? |
<T : Any?> get(key: Preferences.Key<T>)Get a preference with a key. |
Cmn
|
MutablePreferences |
Gets a mutable copy of Preferences which contains all the preferences in this Preferences. |
Cmn
|
Preferences |
Gets a read-only copy of Preferences which contains all the preferences in this Preferences. |
Cmn
|
Public functions
asMap
abstract fun asMap(): Map<Preferences.Key<*>, Any>
Retrieve a map of all key preference pairs. The returned map is unmodifiable, and attempts to mutate it will throw runtime exceptions.
| Returns | |
|---|---|
Map<Preferences.Key<*>, Any> |
a map containing all the preferences in this Preferences |
contains
abstract operator fun <T : Any?> contains(key: Preferences.Key<T>): Boolean
Returns true if this Preferences contains the specified key.
| Parameters | |
|---|---|
key: Preferences.Key<T> |
the key to check for |
copy
fun copy(block: (MutablePreferences) -> Unit): Preferences
Creates a new read-only Preferences with the changes from block.
The block runs on a MutablePreferences copy of this Preferences object, so any changes inside the block will be present in the returned Preferences object.
Example:
val newPrefs = prefs.copy { preferences ->
preferences[COUNTER_KEY] = 1
}
| Parameters | |
|---|---|
block: (MutablePreferences) -> Unit |
The lambda to apply to a mutable copy of these preferences. |
| Returns | |
|---|---|
Preferences |
A new read-only |
get
abstract operator fun <T : Any?> get(key: Preferences.Key<T>): T?
Get a preference with a key. If the key is not set, returns null.
If T is Set
Use MutablePreferences.set to change the value of a preference (inside a DataStore
| Parameters | |
|---|---|
<T : Any?> |
the type of the preference |
key: Preferences.Key<T> |
the key for the preference |
| Throws | |
|---|---|
ClassCastException |
if there is something stored with the same name as |
toMutablePreferences
fun toMutablePreferences(): MutablePreferences
Gets a mutable copy of Preferences which contains all the preferences in this Preferences. This can be used to update your preferences without building a new Preferences object from scratch in DataStore.updateData.
This is similar to Map.toMutableMap.
| Returns | |
|---|---|
MutablePreferences |
a MutablePreferences with all the preferences from this Preferences |
toPreferences
fun toPreferences(): Preferences
Gets a read-only copy of Preferences which contains all the preferences in this Preferences.
This is similar to Map.toMap.
| Returns | |
|---|---|
Preferences |
a copy of this Preferences |