Preferences
public 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 |
|---|
public final class Preferences.Key<T extends Object>Key for values stored in Preferences. |
public final class Preferences.Pair<T extends Object>Key Value pairs for Preferences. |
Public methods |
|
|---|---|
abstract @NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object> |
asMap()Retrieve a map of all key preference pairs. |
abstract boolean |
<T extends Object> contains(@NonNull Preferences.Key<@NonNull T> key)Returns true if this Preferences contains the specified key. |
final @NonNull Preferences |
copy(@NonNull Function1<@NonNull MutablePreferences, Unit> block)Creates a new read-only Preferences with the changes from |
abstract T |
<T extends Object> get(@NonNull Preferences.Key<@NonNull T> key)Get a preference with a key. |
final @NonNull MutablePreferences |
Gets a mutable copy of Preferences which contains all the preferences in this Preferences. |
final @NonNull Preferences |
Gets a read-only copy of Preferences which contains all the preferences in this Preferences. |
Public methods
asMap
public abstract @NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object> asMap()
Retrieve a map of all key preference pairs. The returned map is unmodifiable, and attempts to mutate it will throw runtime exceptions.
contains
public abstract boolean <T extends Object> contains(@NonNull Preferences.Key<@NonNull T> key)
Returns true if this Preferences contains the specified key.
| Parameters | |
|---|---|
@NonNull Preferences.Key<@NonNull T> key |
the key to check for |
copy
public final @NonNull Preferences copy(@NonNull Function1<@NonNull MutablePreferences, Unit> block)
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 | |
|---|---|
@NonNull Function1<@NonNull MutablePreferences, Unit> block |
The lambda to apply to a mutable copy of these preferences. |
| Returns | |
|---|---|
@NonNull Preferences |
A new read-only |
get
public abstract T <T extends Object> get(@NonNull Preferences.Key<@NonNull T> key)
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 extends Object> |
the type of the preference |
@NonNull Preferences.Key<@NonNull T> key |
the key for the preference |
| Throws | |
|---|---|
ClassCastException |
if there is something stored with the same name as |
toMutablePreferences
public final @NonNull MutablePreferences toMutablePreferences()
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 | |
|---|---|
@NonNull MutablePreferences |
a MutablePreferences with all the preferences from this Preferences |
toPreferences
public final @NonNull Preferences toPreferences()
Gets a read-only copy of Preferences which contains all the preferences in this Preferences.
This is similar to Map.toMap.
| Returns | |
|---|---|
@NonNull Preferences |
a copy of this Preferences |